User manual
The LCD controller gives us two ways to send data: in one-byte (8 bit) chunks, or as two
consecutive nibbles (4 bit). The second approach seems more complicated, but very
frequently used. Why? It simplifies the hardware, and lets us talk to the LCD with half as
many data lines. In general, I/O lines are a scarce resource! We use the 4-bit method on
this board, and need only 6 I/O lines (2 control + 4 data) to communicate with the LCD.
Here are the digital I/O connections between our Pi and the LCD controller:
GPIO#
LCD Pin
Function
7
4
Register Select
8
6
Enable
17
11
Bit 0 (Data line D4)
18
12
Bit 1 (Data line D5)
27
13
Bit 2 (Data line D6)
22
14
Bit 3 (Data line D7)
Now we have enough information to program our data lines on the Pi. We need to set up 6
GPIO lines as outputs. I like to give each line a name, rather than just a number
GPIO.setup(LCD_D4,GPIO.OUT)
GPIO.setup(LCD_D5,GPIO.OUT)
GPIO.setup(LCD_D6,GPIO.OUT)
…
If you want to, you can put all the output lines in a list, and then loop through them:
OUTPUTS = [LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7]
for ioLine in OUTPUTS:
GPIO.setup(ioLine,GPIO.OUT)
3) NIBBLES & BYTES
Put on your thinking cap, because it’s time for the hard part: sending data to the LCD
controller. There are 8 bits to each byte, but we can only send 4 bits at a time. And we
have to time them according to the controller’s specifications. Check out the datasheet for
the specific details. The gist is to send the upper 4 bits of the data, toggle the enable pin,
and then send the lower 4 bits. The half-byte chunks are called nibbles.
def SendByte (data):
SendNibble(data) #send upper bits first
PulseEnableLine() #pulse the enable line
data = (data & 0x0F)<< 4 #shift 4 bits to left
SendNibble(data) #send lower bits now
PulseEnableLine() #pulse the enable line
def PulseEnableLine():
#Pulse the LCD Enable line; used for clocking in data
mSec = 0.0005 #use half-millisecond delay
time.sleep(mSec) #give time for inputs to settle
GPIO.output(LCD_E, GPIO.HIGH) #pulse E high