lcd module python code sunfounder manufacturer
SunFounder is a company focused on STEAM education with products like Open-source robots, Arduino&Raspberry Pi Kits, Display screens, and other smart devices. Learn More
LCD1602 is a character type liquid crystal display, which can display 32 (16*2) characters at the same time. It has 16 pins, of which at least 7 would be used each time. You can use a PCF8574 I2C chip to expand I/O ports so only two GPIO ports would be occupied.
In this experiment, I2C is used to configure LCD so that you can control the LCD1602 to display characters. The I2C slave address of I2C LCD1602 here is 0x27.
LCD1602, also called character type LCD1602, is a dot matrix LCD module that specially used to display letters, figures, symbols, and so on. It consists of 16X2 dot matrixes, and each dot matrix is composed of 5X7 or 5X11 character bit. Each character bit can display a character. There is a dot space between each adjacent character bit. And there is a dot space between each row too. The dot space functions as a character space or a line space, and because of this, LCD1602 cannot display graphics very well. It is widely used in pocket instruments and low power application systems due to its micro power consumption, small size, rich contents display, ultra-thin and lightness.
Pin 3(Vo): used to adjust the contrast of LCD1602, lowest when connected to positive power supply, highest when connected to ground (you can connect a 10K potentiometer to adjust its contrast when using LCD1602);
Pin 5 (R/W): A Read/Write signal pin, read signals when supplied high level (1), write signals when supplied low level (0). Since we only need to write data to LCD1602, we directly connect this pin to ground;
LCD1602 has two operation modes: 4-bit mode and 8-bit mode. When the IOs of the microprocessor (MCU) is nervous, you can choose the 4-bit mode, which only uses D4~D7 pins. After connecting the circuit, you can operate LCD1602 by Raspberry Pi.
Press Enter, and you will see two lines of information being displayed on the LCD1602. The first line displays our logo “SUNFOUNDER” and the second line display “Hello World !:)”. The real effect is shown as follow:
Through this lesson, you have basically mastered the principle and programming implementation for LCD1602 based on Raspberry Pi. I hope you can create many more fun works on the basis of Raspberry Pi.
#!/usr/bin/python # # based on code from lrvick and LiquidCrystal # lrvic – https://github.com/lrvick/raspi-hd44780/blob/master/hd44780.py # LiquidCrystal – https://github.com/arduino/Arduino/blob/master/libraries/LiquidCrystal/LiquidCrystal.cpp # from time import sleep class Adafruit_CharLCD: # commands LCD_CLEARDISPLAY = 0x01 LCD_RETURNHOME = 0x02 LCD_ENTRYMODESET = 0x04 LCD_DISPLAYCONTROL = 0x08 LCD_CURSORSHIFT = 0x10 LCD_FUNCTIONSET = 0x20 LCD_SETCGRAMADDR = 0x40 LCD_SETDDRAMADDR = 0x80 # flags for display entry mode LCD_ENTRYRIGHT = 0x00 LCD_ENTRYLEFT = 0x02 LCD_ENTRYSHIFTINCREMENT = 0x01 LCD_ENTRYSHIFTDECREMENT = 0x00 # flags for display on/off control LCD_DISPLAYON = 0x04 LCD_DISPLAYOFF = 0x00 LCD_CURSORON = 0x02 LCD_CURSOROFF = 0x00 LCD_BLINKON = 0x01 LCD_BLINKOFF = 0x00 # flags for display/cursor shift LCD_DISPLAYMOVE = 0x08 LCD_CURSORMOVE = 0x00 # flags for display/cursor shift LCD_DISPLAYMOVE = 0x08 LCD_CURSORMOVE = 0x00 LCD_MOVERIGHT = 0x04 LCD_MOVELEFT = 0x00 # flags for function set LCD_8BITMODE = 0x10 LCD_4BITMODE = 0x00 LCD_2LINE = 0x08 LCD_1LINE = 0x00 LCD_5x10DOTS = 0x04 LCD_5x8DOTS = 0x00 def __init__(self, pin_rs=14, pin_e=15, pins_db=[17, 18, 27, 22], GPIO = None): # Emulate the old behavior of using RPi.GPIO if we haven’t been given # an explicit GPIO interface to use if not GPIO: import RPi.GPIO as GPIO self.GPIO = GPIO self.pin_rs = pin_rs self.pin_e = pin_e self.pins_db = pins_db self.GPIO.setmode(GPIO.BCM) self.GPIO.setup(self.pin_e, GPIO.OUT) self.GPIO.setup(self.pin_rs, GPIO.OUT) for pin in self.pins_db: self.GPIO.setup(pin, GPIO.OUT) self.write4bits(0x33) # initialization self.write4bits(0x32) # initialization self.write4bits(0x28) # 2 line 5×7 matrix self.write4bits(0x0C) # turn cursor off 0x0E to enable cursor self.write4bits(0x06) # shift cursor right self.displaycontrol = self.LCD_DISPLAYON | self.LCD_CURSOROFF | self.LCD_BLINKOFF self.displayfunction = self.LCD_4BITMODE | self.LCD_1LINE | self.LCD_5x8DOTS self.displayfunction |= self.LCD_2LINE “”” Initialize to default text direction (for romance languages) “”” self.displaymode = self.LCD_ENTRYLEFT | self.LCD_ENTRYSHIFTDECREMENT self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) # set the entry mode self.clear() def begin(self, cols, lines): if (lines > 1): self.numlines = lines self.displayfunction |= self.LCD_2LINE self.currline = 0 def home(self): self.write4bits(self.LCD_RETURNHOME) # set cursor position to zero self.delayMicroseconds(3000) # this command takes a long time! def clear(self): self.write4bits(self.LCD_CLEARDISPLAY) # command to clear display self.delayMicroseconds(3000) # 3000 microsecond sleep, clearing the display takes a long time def setCursor(self, col, row): self.row_offsets = [ 0x00, 0x40, 0x14, 0x54 ] if ( row > self.numlines ): row = self.numlines – 1 # we count rows starting w/0 self.write4bits(self.LCD_SETDDRAMADDR | (col + self.row_offsets[row])) def noDisplay(self): “”” Turn the display off (quickly) “”” self.displaycontrol &= ~self.LCD_DISPLAYON self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol) def display(self): “”” Turn the display on (quickly) “”” self.displaycontrol |= self.LCD_DISPLAYON self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol) def noCursor(self): “”” Turns the underline cursor on/off “”” self.displaycontrol &= ~self.LCD_CURSORON self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol) def cursor(self): “”” Cursor On “”” self.displaycontrol |= self.LCD_CURSORON self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol) def noBlink(self): “”” Turn on and off the blinking cursor “”” self.displaycontrol &= ~self.LCD_BLINKON self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol) def noBlink(self): “”” Turn on and off the blinking cursor “”” self.displaycontrol &= ~self.LCD_BLINKON self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol) def DisplayLeft(self): “”” These commands scroll the display without changing the RAM “”” self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVELEFT) def scrollDisplayRight(self): “”” These commands scroll the display without changing the RAM “”” self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVERIGHT); def leftToRight(self): “”” This is for text that flows Left to Right “”” self.displaymode |= self.LCD_ENTRYLEFT self.write4bits(self.LCD_ENTRYMODESET | self.displaymode); def rightToLeft(self): “”” This is for text that flows Right to Left “”” self.displaymode &= ~self.LCD_ENTRYLEFT self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) def autoscroll(self): “”” This will ‘right justify’ text from the cursor “”” self.displaymode |= self.LCD_ENTRYSHIFTINCREMENT self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) def noAutoscroll(self): “”” This will ‘left justify’ text from the cursor “”” self.displaymode &= ~self.LCD_ENTRYSHIFTINCREMENT self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) def write4bits(self, bits, char_mode=False): “”” Send command to LCD “”” self.delayMicroseconds(1000) # 1000 microsecond sleep bits=bin(bits)[2:].zfill(8) self.GPIO.output(self.pin_rs, char_mode) for pin in self.pins_db: self.GPIO.output(pin, False) for i in range(4): if bits[i] == “1”: self.GPIO.output(self.pins_db[::-1][i], True) self.pulseEnable() for pin in self.pins_db: self.GPIO.output(pin, False) for i in range(4,8): if bits[i] == “1”: self.GPIO.output(self.pins_db[::-1][i-4], True) self.pulseEnable() def delayMicroseconds(self, microseconds): seconds = microseconds / float(1000000) # divide microseconds by 1 million for seconds sleep(seconds) def pulseEnable(self): self.GPIO.output(self.pin_e, False) self.delayMicroseconds(1) # 1 microsecond pause – enable pulse must be > 450ns self.GPIO.output(self.pin_e, True) self.delayMicroseconds(1) # 1 microsecond pause – enable pulse must be > 450ns self.GPIO.output(self.pin_e, False) self.delayMicroseconds(1) # commands need > 37us to settle def message(self, text): “”” Send string to LCD. Newline wraps to second line””” for char in text: if char == ‘\n’: self.write4bits(0xC0) # next line else: self.write4bits(ord(char),True) if __name__ == ‘__main__’: lcd = Adafruit_CharLCD() lcd.clear() #lcd.message(” LCD 1602 Test \n123456789ABCDEF”) lcd.message(” SUNFOUNDER \nHello World ! :)”)
Generally, LCD1602 has parallel ports, that is, it would control several pins at the same time. LCD1602 can be categorized into eight-port and four-port connections. If the eight-port connection is used, then all the digital ports of the SunFounder Uno board are almost completely occupied. If you want to connect more sensors, there will be no ports available. Therefore, the four-port connection is used here for better application.
Pin 3 (Vo): used to adjust the contrast of LCD1602; the level is lowest when it’s connected to a positive power supply, and highest when connected to ground (you can connect a 10K potentiometer to adjust its contrast when using LCD1602)
Pin 4 (RS):register select pin, controlling where in the LCD’s memory you are writing data to. You can select either the data register, which holds what goes on the screen, or an instruction register, where the LCD’s controller looks for instructions on what to do next.
Pin 5 (R/W): to read/write signals; it reads signals when supplied with high level (1), and writes when low level (0) (in this experiment, you only need to write data to LCD1602, so just connect this pin to ground)
A and K:controlling LCD backlight; K connects to GND, and A to 3.3V. Turn the backlight on and you can see the characters displayed clear in a dim environment
LCD1602 has two operation modes: 4-bit and 8-bit. When the IOs of the MCU are insufficient, you can choose the 4-bit mode, under which only pins D4~D7 are used. After connecting the circuit, you can operate LCD1602 by the Raspberry Pi.
Note: After you run the code, characters may not appear on the LCD1602. You need to adjust the contrast of the screen (the gradual change from black to white) by spinning the potentiometer clockwise or anticlockwise, until the screen displays characters clearly.
SunFounder is a company focused on STEAM education with products like Open-source robots, Arduino&Raspberry Pi Kits, Display screens, and other smart devices. Learn More
SunFounder is a company focused on STEAM education with products like Open-source robots, Arduino&Raspberry Pi Kits, Display screens, and other smart devices. Learn More
As soon as your order is packed and shipped, you"ll receive a shipping confirmation email. You will then be able to track your order through the tracking link on the email. If you haven"t received an email yet, please reach out to us atservice@sunfounder.com, our sales staff will contact you ASAP.
If Customs Duty is payable to your territory, you"ll be responsible for paying it to the authorities, so SunFounder isn"t involved in this process. Whether Customs Duty is payable, and by how much, depends on a whole lot of different things. For example, many countries have a "low value threshold" below which they do not charge any Customs Duty.
As soon as your order is packed and shipped, you"ll receive a shipping confirmation email. You will then be able to track your order through the tracking link on the email. If you haven"t received an email yet, please reach out to us atservice@sunfounder.com, our sales staff will contact you ASAP.
If Customs Duty is payable to your territory, you"ll be responsible for paying it to the authorities, so SunFounder isn"t involved in this process. Whether Customs Duty is payable, and by how much, depends on a whole lot of different things. For example, many countries have a "low value threshold" below which they do not charge any Customs Duty.
SunFounder Raphael Kit is a Raspberry Pi Ultimate Kit compatible with Raspberry Pi 400 4B / 3B+ / 3B / 3A+ / 2B / 1B+ / 1A+ / Zero W / Zero. Comes with 337 components and IoT Modules for learning electronics and application programming.
For this SunFounder Ultimate Raspberry Pi Kit, we provide 131 projects, each lesson shows a circuit diagram, an overview of the operating principles, and an explanation of the code used for the project. Please refer to the Detailed Online Tutorials here: raphael-kit.rtfd.io
About SunFounder: SunFounder is staying at the leading edge of Raspberry Pi, Arduino, and IoT Services. From the initial Hard-Copy Manual, to CD Manual, and now Online Tutorials, we have been looking for a better learning experience for users and helping users improve learning programming efficiency.
Supporting 4 programming languages As a new updated exclusive Raspberry Pi advanced Kit, SunFounderRaphael Kit contains 4 programming languages: Python, C, Scratch and Java (processing).
The Most Complete Ultimate Starter Kit with high quality components and 131 projects provided SunFounder Raphael Kit-Exclusive Raspberry Pi Comprehensive Kit with 337 items, 131 projects.
SunFounder Raphael Kit contains almost all the necessary components in total 337pcs, especially camera, speaker and loT modules, which can realize functions such as monitoring system, voice broadcast and smart doorbell. It also contains a lot of commonly used modules as well as some basic electronic components (such as resistors and capacitors), which can provide powerful help for your programming learning.
There are 131 projects for beginners and advanced programmers to learn. Each project introduces the required components, implementation steps, and code explanations, so that even if you have no programming or circuit knowledge, you can complete each project and expand on it. We provide many interesting projects here: Security System, Alarm Bell, Morse Code Generator and Attendance System etc. Giving you more possibility to explore more.
Interesting and practical examples for camera module and speaker Speech Clock Now, let"s make a simple Speech Clock with the raspberry pi. Follow the steps in our online tutorials, after the code runs, the digital tube will display the time, and there will be a voice announcement every hour on the hour. If you have some important dating at specific time, it is suitable as a reminder. Before the experiment, if the local time is different from the real time, don"t forget to set the time zone.
The screen monitor shown above is the 7inch one we"re using, available on our website www.sunfounder.com and our Amazon store. Check out now and use your Raspberry Pi in a most convenient way.
Besides, in Python, pins are usually defined by the physical position on the board. From top to bottom and left to right, the pin is defined as 3V3 (1), 5V0 (2), SDA1 (3), etc., till GPIO21 (40). You"ll get to more details in lessons later.
After you press Enter, with the script build, the source code of wiringPi will be compiled automatically and installed to the appropriate directory of Raspberry Pi OS.
Raspberry Pi.GPIO is a module to control Raspberry Pi GPIO channels. This package provides a class to control the GPIO on a Raspberry Pi. For examples and documents, visit http://sourceforge.net/p/raspberry-gpio-python/wiki/Home/
The advantage of this method is that, you can download the latest code any time you want, and then place the code under the path /home/pi/. But in case of incorrect typing which is possible especially when you"re strange to the commands, you can just enter github.com/sunfounder at the address bar of a web browser, and on the page directed find the code for Super Kit.
Go to /home/pi/ and find the folder Sunfounder_SuperKit_C_code_for_RaspberryPi. Find 01_LED in the folder, right-click on it and select Open in Terminal. Then a window will pop up as shown below. So now you"ve entered the path of the code led.c.
To run the Python code, you don"t need to compile it. Therefore, you can put all the .py files together. Open the downloaded folder Sunfounder_SuperKit_Python_code_for_RaspberryPi and you can see them.
To stop it from running, just click the X button to close it and then you"ll back to the code details. If you modify the code, before clicking Run Module(F5) you need to save it first. Then you can see the results.
The SunFounder Smart Video Car Kit for Raspberry Pi is composed of Raspberry Pi, DC-DC Step-down Voltage Module, USB camera, DC motor driver, and PCA9685-based Servo Controller. From the perspective of software, the smart car is of client/server (C/S) structure. The TCP server program is run on Raspberry Pi for direct control of the car. And the video data are acquired and delivered via the open source software MGPJ-streamer in a real-time manner. The TCP client program is run on PC to send the control command. Both the client and server programs are developed in Python language. The smart car is developed based on the open-source hardware Raspberry Pi and integrates the knowledge of mechanics, electronics, and computer, thus having profound educational significance.
Edit the HOST in cali_client.py or client_App.py with Python IDLE (in Windows, usually, right click the .py file and select "Edit in IDLE"), and select Run => Run Module F5 to run the module.
SunFounder is a technology company focused on Raspberry Pi and Arduino open source community development. Committed to the promotion of open source culture, we strives to bring the fun of electronics making to people all around the world and enable everyone to be a maker. Our products include learning kits, development boards, robots, sensor modules and development tools. In addition to high quality products, SunFounder also offers video tutorials to help you make your own project. If you have interest in open source or making something cool, welcome to join us!
Raspberry Pi 16×2 LCD I2C Interfacing and Python Programming– I have been using 16×2 LCD for quite a long time in different Arduino and IoT related projects. You know we have two types of the 16×2 LCD, the normal one used more wires and the other one is based on the I2C interface which needs only two wires.
The backpack module uses the I-squred-C (or I2C) protocol to communicate with the Raspberry Pi, which uses only two wires: SDA and SCL (data and clock). Please note that the display is a 5 volt device, and it is powered by 5 volts, but due to design of the I2C protocol, and the fact that the Raspberry Pi is the controlling device, it is safe to connect such display to the Raspberry Pi directly.
I suggest using wires of different colors to connect the LCD display. This minimizes the risk of damage due to incorrect connections. For example, I’m using
Before you start using the I2C 16×2 LCD display with Python, you need to make sure that the I2C protocol is enabled on your Raspberry Pi. You can use the sudo raspi-config utility to take care of that. This program is navigated using keyboard arrows, tab and the Enter key. Look for I2C in the interfacing options and enable it. Enabling I2C requires a reboot.
The 27 hexadecimal addresses happen to be the most common, but your display’s address may be different. For example, it could be 3f. This will depend on the chip version of the backpack module. As long as the i2cdetect command shows the display is connected, you are good to go.
The easiest way to program this 16×2 I2C LCD display in Python is by using a dedicated library. There are many to choose from. I like things simple, so the library I recommend is rpi_lcd.
This library has the default 27 address hard-coded. If your display has a different address you will need to change it. You need to find the library on your system and the following command should do that for you.
Python code is provided for the car, and you can also program and debug it with Dragit, a Snap-based graphical interface, by just simple dragging and dropping the code blocks for complex functions.
Blockly and Python Program: Python code is provided for the car, and you can also program and debug it with Dragit. A visual programming language software is provided for easy coding. Even if you don"t have a programming basis, by means of easy code blocks complex functions can also be realized.
DIY Your Robot Car: Examples given to expanding your knowledge. In addition to using the keyboard to control the status of the car, we also wrote a ball tracking code. After the code runs, find a red ball (at least larger than the diameter of the camera) and put it within 10-50cm range in front of the camera to get the car to follow your ball.
Multi-type Device Remote Control: With the app on PC (works on any OS), you can control the car and the cam"s direction on the UI and view the image it takes in real-time; or with the related code, realize the same functions on a web browser, means it can work with any mobile phones, tablets, etc.
SunFounder Raphael Kit is a Raspberry Pi Ultimate Kit compatible with Raspberry Pi 400/4B/3B+/3B/3A+/2B/1B+/1A+/Zero W/Zero. It has 337 components and IoT Modules for learning electronics and application programming. For this SunFounder Ultimate Raspberry Pi Kit, we provide 131 projects. Each lesson shows a circuit diagram, an overview of the operating principles, and an explanation of the code used for the project.
The SunFounder Raphael Kit is an exclusive Raspberry Pi advanced kit with five programming languages: Python, C, Scratch, Nodejs, and Java (processing).
SunFounder Raphael Kit includes 337 pieces and nearly all the necessary components, particularly the camera, speaker, and LoT modules that can enable functions like a monitoring system, voice broadcast, and a smart doorbell. Furthermore, it contains several commonly used modules and some essential electronic components (such as resistors and capacitors), which can be very beneficial for learning programming.
There are 161 projects for beginners and advanced programmers. Each project contains instructions for assembling the required components, implementing the steps, and explaining the code. You can finish each project and extend it even if you don’t have experience in programming or circuits. You’ll find many exciting projects here, such as Security System, Alarm Bell, Morse Code Generator Attendance System, etc.
SunFounder is a company focused on STEM education with products like open-source robots, Arduino&Raspberry Pi Kits, Display screens, and smart devices. SunFounder serves the global market from its headquarter in Shenzhen, China. In SunFounder, we strive to help elementary and middle school students as well as hobbyists, through STEM education, strengthen their hands-on practices and problem-solving abilities.