Question 13.4: You want to display text on an alphanumeric LCD display....

You want to display text on an alphanumeric LCD display.

Step-by-Step
The 'Blue Check Mark' means that this solution was answered by an expert.
Learn more on how do we answer questions.

Use an HD44780-compatible LCD module and wire it to the GPIO connector.

You will find many low-cost LCD modules, such as the one shown connected to a Raspberry Pi in Figure 13-6. These have 16 pins, although fortunately, not all of them have to be connected to a GPIO pin.

These modules are available in a number of sizes and are specified by the number of columns and rows of letters they can display. So, for example, the module used in this recipe is described as 16×2, as it can display two rows each of 16 characters. Other common sizes are 8×1, 16×1, 16×2, 20×2, and 20×4.

To make this recipe, you will need:

• Breadboard and jumper wires (see “Prototyping Equipment” on page 380)

• 16×2 HD44780-compatible LCD module (see “Modules” on page 381)

• Row of 16 header pins (see “Miscellaneous” on page 382)

• 10kΩ trimpot (see “Resistors and Capacitors” on page 380)

Figure 13-7 shows the breadboard layout for connecting the display. The LCD modules are not normally supplied with header pins, so you will need to solder these in place.

The trimpot is used to control the contrast of the display. Note that if it looks like the project has not worked, try moving the knob of the trimpot over its entire range. It may well be that the contrast is so off that the display characters are not visible at all.

The Adafruit Raspberry Pi example code, available from GitHub, includes a library for controlling LCD displays that use the HD44780 driver. Before installing it, follow Recipe 8.3 to install the RPi.GPIO library.

The Adafruit library is not installed as a proper library, so to use it, you first need to download the folder structure. If you don’t have Git installed, install it now with this command (see Recipe 3.19):

$ sudo apt-get install git

Now, you can download the folder structure from GitHub:

$ git clone https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git

Change directory into the Adafruit code using:

$ cd Adafruit-Raspberry-Pi-Python-Code
$ cd Adafruit_CharLCD

There is a test program here that displays the time and IP address of the Raspberry Pi. But first, if you are using a newer Raspberry Pi model B, revision 2, you will need to edit the file Adafruit_CharLCD.py:

$ nano Adafruit_CharLCD.py

Search for the line:

def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22], GPIO = None):

Replace the number 21 with 27 so that the line looks as follows, and then save and exit the file:

def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 27, 22], GPIO = None):

You can now run the example program with the command:

$ sudo python Adafruit_CharLCD_IPclock_example.py

Discussion

These displays can operate either with a four- or eight-bit data bus and also require three control pins. The pins are connected as per Table 13-1.

The Adafruit library file Adafruit_CharLCD.py is responsible for setting the values on the data pins and then sending them to the display module. It provides the following functions, which you can use in your programs:

home()
Move to top left.

clear()
Clear all text off the display.

setCursor(column, row)
Set the cursor position from where text will be written.

cursor()
Turn on cursor display.

noCursor()
Turn off cursor display (default).

message(text)
Write the text at the current cursor position.

The following minimal example program shows how easy it is to display a simple message by using the library:

from Adafruit_CharLCD import Adafruit_CharLCD
from time import sleep
lcd = Adafruit_CharLCD()
lcd.begin(16,2)
i = 0
while Tue:
lcd.clear()
lcd.message(‘Counting: ‘ + str(i))
sleep(1)
i = i + 1

These modules are available in many sizes and are specified by the number of columns and rows of letters they can display. So, for example, the module used in this recipe is described as 16×2, as it can display two rows each of 16 characters. Other common sizes are 8×1, 16×1, 16×2, 20×2, and 20×4.

See Also

This recipe is based on the Adafruit tutorial at the Adafruit learning website.

Adafruit also sells a plug-in shield with an LCD display attached that is compatible with this recipe.

Table A-3. Prototyping equipment
Description Suppliers
M-M jumper wires SparkFun: PRT-08431, Adafruit: 759
M-F jumper wires SparkFun: PRT-09140, Adafruit: 825
F-F jumper wires SparkFun: PRT-08430, Adafruit: 794
Half-sized breadboard SparkFun: PRT-09567 Adafruit: 64
Pi Cobbler Adafruit: 1105
Table A-8. Modules
Raspberry Pi camera module Adafruit: 1367, MCM: 28-17733, CPC: SC13023
Arduino Uno SparkFun: DEV-11021, Adafruit: 50, CPC: A000066
Level converter, four-way SparkFun: BOB-11978, Adafruit: 757
Level converter eight-way Adafruit: 395
LiPo boost converter/charger SparkFun: PRT-11231
PowerSwitch tail Adafruit: 268
16-channel servo controller Adafruit: 815
Motor driver 1A dual SparkFun: ROB-09457
RaspiRobot board Sparkfun: KIT-11561, raspirobot.com
PiFace digital interface board MCM: 83-14472, CPC: SC12827
Humble Pi MCM: 83-14637, CPC: SC12871
Pi Plate Adafruit: 801
Gertboard MCM: 83-14460, CPC: SC12828
Breakout board with paddle terminals MCM: 83-14876, CPC: SC12885
PIR motion detector Adafruit: 189
Venus GPS module SparkFun: GPS-11058
Methane sensor SparkFun: SEN-09404
Gas sensor breakout board SparkFun: BOB-08891
ADXL335 triple-axis accelerometer Adafruit: 163
4×7-segment LED with I2C backpack Adafruit: 878
Bicolor LED square-pixel matrix with I2C backpack Adafruit: 902
PiLite interface board Ciseco, CPC: SC13018
aLaMode interface board Makershed: MKWY1, Seeedstudio: ARD10251P
Freetronics Arduino LCD shield www.freetronics.com
RTC module Adafruit: 264
16 x 2 HD44780 compatible LCD module SparkFun: LCD-00255, Adafruit: 181
Table A-9. Miscellaneous
1200mAh LiPo battery Adafruit: 258
5V relay SparkFun: COM-00100
5V panel meter SparkFun: TOL-10285
Servo motor SparkFun: ROB-09065, Adafruit: 1449
5V 1A power supply Adafruit: 276
Low power 6V DC motor Adafruit: 711
0.1 inch header pins SparkFun: PRT-00116, Adafruit: 392
5 V 5-pin unipolar stepper motor Adafruit: 858
12 V, 4-pin bipolar stepper motor Adafruit: 324
Magician chassis with gearmotors SparkFun: ROB-10825
Tactile push switch SparkFun: COM-00097, Adafruit: 504
Miniature slide switch SparkFun: COM-09609, Adafruit: 805
Rotary encoder Adafruit: 377
4×3 keypad SparkFun: COM-08653
Piezo buzzer SparkFun: COM-07950, Adafruit: 160
Table A-4. Resistors and capacitors
270Ω 0.25W resistor Mouser: 293-270-RC
470Ω 0.25W resistor Mouser: 293-470-RC
1kΩ 0.25W resistor Mouser: 293-1k-RC
3.3kΩ 0.25W resistor Mouser: 293-3.3k-RC
4.7kΩ 0.25W resistor Mouser: 293-4.7k-RC
10 kΩ trimpot Adafruit: 356, SparkFun: COM-09806, Mouser: 652-3362F-1-103LF
Photoresistor Adafruit: 161, SparkFun: SEN-09088
220nF capacitor MCM: 31-0610, Mouser: 80-C322C224M5U5HA
Table 13-1. Raspberry Pi and LCD display pin connections
LCD module pin GPIO pin Notes
1 GND 0V
2 +5V 5V logic supply
3 No connection Contrast control voltage
4 25 RS: Register select
5 GND RW: Read/write (always write)
6 24 EN: Enable
7-10 No connection Only used in eight-bit mode
11 23 D4: Data line 4
12 17 D5: Data line 5
13 21 D6: Data line 6
14 22 D7: Data line 7
15 +5V LED backlight
16 GND LED backlight –
13.6
13.7

Related Answered Questions

Question: 13.3

Verified Answer:

Fit a Pi-Lite onto the GPIO port of your Raspberry...
Question: 13.2

Verified Answer:

Use an I2C LED module, such as the model shown in ...
Question: 13.1

Verified Answer:

Use an I2C LED module, such as the model shown in ...