How to Connect a Raspberry Pi to a 16 x 2 LCD Display
2018-10-29 | By Maker.io Staff
Computer monitors are great for office work, gaming, and browsing, but these displays can be large and power consuming. One display found in both the commercial and hobby world is the HD44780 16 x 2 display, which can display two lines of alphanumeric characters. In this tutorial, we will learn how to connect one to a Raspberry Pi and code it using the Python programming language!
BOM
SchemeIt
You can see the full Scheme-It schematic here.
Python and libraries
The HD44780 has a number of registers and commands that are used to control the display, but getting the display to work can be somewhat tricky, especially for inexperienced users. This is why most coders will turn to libraries. They include prewritten code that has been proven to work. In this tutorial, we will be relying on two libraries to control the LCD: RPLCD and RPi.GPIO. Before we can continue, we need to install the RPLCD library by using the following command in a terminal window:
sudo pip install RPLCD
First, we need to import the RPi.GPIO library as GPIO. This will allow us to use the GPIO library and refer to it as “GPIO”, which is easier than RPi.GPIO. The next library that we need to include is the RPLCD library, which allows us to use the LCD. Therefore, the first two lines of code in our Python program will be:
import RPi.GPIO as GPIO from RPLCD.gpio import CharLCD #CharLCD is a Python library for accessing Adafruit character LCDs from a Raspberry Pi
CharLCD is a Python library that lets users access Adafruit character LCDs from a Raspberry Pi. Next, we need to tell the RPLCD library what pins we have connected the LCD to. In the Scheme-It schematic above, we connected the LCD pins to the following GPIO (using the BOARD numbering scheme). The Pi has two numbering schemes; BOARD and BCM. The BOARD numbering scheme refers to the I/O header whereas the BCM scheme refers to the GPIO numbers from the Broadcom IC. It is often easier to use BOARD scheme as you can count the pins to determine which one you need to use whereas the BCM scheme requires you to first choose a GPIO and then determine its pin number (BOARD).
Using this information, we can now use the following code to initialize CharLCD.
lcd = CharLCD(pin_rs=19, pin_e=16, pin_rw=None, pins_data=[23, 17, 21, 22], numbering_mode=GPIO.BOARD, cols=16, rows=2, dotsize=8)
Python Program Example
#Include libraries import RPi.GPIO as GPIO import time from RPLCD.gpio import CharLCD # Configure the LCD lcd = (pin_rs = 19, pin_rw = None, pin_e = 16, pins_data = [21,18,23,24], numbering_mode = GPIO.BOARD) # Create a variable ‘number’ number = 0 # Main loop while(True): # Increment the number and then print it to the LCD number = number + 1 lcd.clear() lcd.write_string(“Count: “+ str(number)) time.sleep(1) lcd.close() GPIO.cleanup()
LCD Functions Using RPLCD and code explained
Now that we have configured the LCD, it’s time to do a few tasks with it! The first function that we should learn to use is “write_string()”, which is used to write text to the LCD. Try the following command:
lcd.write_string(“Hello World”)
Writing predefined strings is not entirely helpful, and displaying variables can be very beneficial. Luckily for us, Python allows typecasting, where one variable can be converted into another variable. The example below shows how we can display a counter, which displays an integer on the LCD, along with a simple “Count” message.
lcd.write_string(“Count :” + str(counter))
Next, we’ll learn how to clear the display. Displays like those based around the HD44780 are static, and they remember what you tell them to display. However, this also means that when you want to write new data, you may need to clear the entire display. Therefore, you can use this clear function to clear the display:
lcd.clear()
Sometimes printing text on the top left of the display may not be desirable. In this case, you will need to change the cursor’s position. To do this, we can set the cursor_pos variable to a specific value. The example below sets the cursor position to row 2 (x) and column 0 (y).
lcd.cursor_pos = (2, 0)
At the end of your program, you should include the function “lcd.close()”, as this will free up the GPIO for other programs that may need to use them.
lcd.close()
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum