How to drive a Character LCD display using the Raspberry Pi
In this tutorial we’ll take you through how to connect a 16×2 LCD display up to your Raspberry Pi using GPIO pins. Being able to display a message on the LCD is not only very cool but can be pretty useful too, for example in this tutorial we’ll cover how to get your LCD display to display the IP address of your raspberry Pi.
For this exercise you will need:
- A Raspberry Pi (obviously!)
- A HDD44780 compatible 16×2 LCD Display
- A breadboard
- An Adafruit Pi Cobbler
- Assorted colour breadboarding wire
Step 1 – Assemble the LCD display
For this exercise I have used the Adafruit HDD44780 character LCD display, this should come with 0.1” male header strip and a potentiometer.
First we need to solder the 0.1”header to the LCD board. Break off enough headers (in this case 16) and carefully solder them to the board.
For this exercise we are going to control the LCD display using 4-bit mode. Whilst is is possible to connect to it in other ways using I2C or the UART this is the most direct method. In order to control the display in this way will we need to use 6 pins on the GPIO port, 4 data pins and 2 control pins.
LCD pin layout
- Data pins- We will use pins 11 to 14 (data4-7) to send data to the display by toggling high/low
- Enable – Will be toggled to write data to the registers
- Read/Write – Since we only want to write to the display not read, this will be toggled to low(write)
- Register Select – This toggles the Lcd display between two modes, Command mode (high) and Data mode (low). Command mode gives a Instruction to the LCD. Example – “Clear the display” , “Move cursor to home” etc and Data tells the LCD to display characters.
- Contrast Voltage – This adjusts the contrast on the display and will controlled by the potentiometer when wired up to the circuit.
- VDD – Will be wired to a 5V supply from the Pi’s GPIO port.
Step 2 – Assemble the circuit
Below is a schematic of the circuit and a wiring diagram, taken from the Adafruit website:
In order for the LCD to work we will wire the circuit up in a fashion similar to the diagram above, but hold off connecting everything together for now! The list below tells you exactly what the pins on the LCD connect to:
- Pin #1 (VSS) connects to ground (black wire)
- Pin #2 (VDD) connects to +5V (red wire)
- Pin #3 (VE) connects to the middle leg of the potentiometer (orange wire)
- Pin #4 (RS) connects to the Cobber #25 (yellow wire)
- Pin #5 (RW) goes to ground (black wire)
- Pin #6 (EN) connects to cobber #24 (green wire)
- Skip LCD Pins #7, #8, #9 and #10
- Pin #11 (D4) connects to cobber #23 (blue wire)
- Pin #12 (D5) connects to cobber #17 (violet wire)
- Pin #13 (D6) connects to cobber #21 (gray wire)
- Pin #14 (D7) connects to cobber #22 (white wire)
- Pin #15 (LED +) goes to +5V (red wire)
- Pin #16 (LED -) goes to ground (black wire)
Begin assembling the circuit by inserting the Adafruit cobbler into the breadboard. Remember to straddle the cobbler over the centre of the breadboard so that no two pin is in the same row. Next insert the LCD display into the breadboard. Connect the 5V and GND pins from the cobbler to the top of breadboard and also connect Pins 1, 2, 15 on the LCD and 16 to their respective power rails. Your circuit should look similar to the picture below:
Connect the GPIO ribbon cable from the cobbler to the Pi, if everything is working correctly the back light on the LCD should turn on like on the picture above. If it doesn’t work check everything is wired up correctly
Next wire up the potentiometer. The middle pin of the potentiometer is connected to Pin 3 on the LCD display and the other two pins are connected to ground and 5V (it doesn’t matter which way round). Check the potentiometer is working by twisting the nob until you see boxes appear in the first line of the display like in the picture below:
Finally connect up the remaining pins on the LCD. Your circuit should look similar to the one below:
Step 3 – The Software
In order to utilize the GPIO pins within Python you will need to Install the GPIO python library. Instructions on how to install the GPIO Python library can be found here.
To get the Python code to run the LCD display we are going to ‘grab’ it from adafruit using GitHub. Make sure your Rasp berry Pi is connected to internet and we’ll use the git command to clone the python code. Run the following commands in the terminal to download the files.
For more detail: How to drive a Character LCD display using the Raspberry Pi