After I posted my Raspberry Pi server room rack temperature monitor project, I received many questions on how to integrate LCD displays with the Pi. This video is the first in a series of tutorials:
Here is the wiring that I used to connect the 16×2 display in 4 bit mode to the Pi. Please note that you can use any 6 GPIO pins.
The pin outs of LCD displays can vary as well as the power requirements for the back light so please check your datasheets.
Some LED back lights can be damaged by running them at 5V without a resistor.
Here is the formula to determine the proper resistor: R = (Vdd – VLED) / ILED
R is the resistor value in ohms and Vdd is the supply voltage. VLED and ILED are the typical LCD voltage and current respectively as specified in the LCD display datasheet. Even if you display supports 5V you might want to add a resistor or variable resistor to control the brightness. The same holds true for the contrast pin. I normally use variable resistors for both (see below).
I’ll demonstrate 2 different Python modules for controlling the LCD display. The first is a modified version of lcd.py by Raspberry Pi Spy which can be downloaded below. Here is code to test the module:
You can use any GPIO pins for the LCD RS, E, & D1 – D4. The GPIO to LCD pin mappings are specified in the LCD.py file. If you change from the default below then you have to update the file with your new selections.
UPDATE: the Adafruit library is now installed using pip. It is no longer necessary to use git and the paths are automatically set up.
The Adafruit library has several additional methods. Please note that their syntax has changed since the video.
Here is some sample code (updated 4/23/16) to demonstrate the Adafruit library. As with the LCD.py module, you can use any GPIO pins. You specify the GPIO to LCD pin mappings when you instantiate the Adafruit_CharLCD class.
Contast & Brightness:
A potentiometer can be added to the LCD display C pin to control the contrast. The pin is connected to the wiper of the pot which is usually the middle pin. One of the outside pins is connected to ground and the other to Vcc. It doesn’t matter which outside pin goes to Vcc or ground because the pot is acting as a voltage divider either way. As the dial is turned the wiper’s pin voltage will vary from 0V to 5V and this will cause the contrast to change. 10K Ω is a good value for the pot. This same wiring will also work to control the LCD display brightness. Instead of of the contrast pin, the wiper would be connected to the LED back light anode. Most LCD display back lights will tolerate 5V but please double check so you don’t damage the LED. You can alter the 0V – 5V range by adding a resistor in series with one of the outside pins. This can help fine tune the amount of range provided by the pot. You can use a multimeter to measure the voltage on the wiper and determine how different resistors affect the range. Here is a simple schematic:
IP Clock Example:
The Adafruit IP clock example from the video is no longer compatible with the latest version of the Adafruit LCD_Char library. Here is an updated version of the IP clock program. Please note that sudo is no longer necessary to run the program with the latest version of Raspbian, but you still need the shebang line and chmod +x to give the file executable permissions, if you run it from the command line.