Some documentation and code to plug an LCD screen into a raspberry pi computer.
Here is a program to... program the LCD screen from the raspberry pi.
For plugging the LCD and the raspberry pi, see those pictures (this is a raspberry pi 4): lcd1.jpg and lcd2.jpg. My LCD doesn't provide pins 3, 15 and 16. Note also that we use the 4 bits interface mode, not 8 bits, so not all pins of the LCD are used.
Here are some basic schemamtics of the GPIO pins of the raspberry pi 4, the pins of my LCD module, the functions of the pins in use, and what is plugged into what.
pins of the raspberry pi 4: +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | 2| 4| 6| 8|10|12|14|16|18|20|22|24|26|28|30|32|34|36|38|40| +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | 1| 3| 5| 7| 9|11|13|15|17|19|21|23|25|27|29|31|33|35|37|39| +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ pins of the LCD module (x means on my LCD device, there is no pin here): +--+--+--+--+--+--+--+--+ | 2| 4| 6| 8|10|12|14| x| +--+--+--+--+--+--+--+--+ | 1| x| 5| 7| 9|11|13| x| +--+--+--+--+--+--+--+--+ pins of the raspberry pi 4 in use and the pin of the LCD they connect to: 2 1 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | 2| 4| 6| 8|10|12|14|16|18|20|22|24|26|28|30|32|34|36|38|40| +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | 1| 3| 5| 7| 9|11|13|15|17|19|21|23|25|27|29|31|33|35|37|39| +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 14 13 12 11 6 4 5 functions of the raspberry pi 4 pins used and the corresponding pins and functions of the LCD module: RASPBERRY PI 4 PINS | LCD PINS ---------------------+----------------- pin 2: 5V output | pin 2: 5V input pin 6: ground | pin 1: ground pin 23: GPIO 11 | pin 14: DB7 pin 29: GPIO 5 | pin 13: DB6 pin 31: GPIO 6 | pin 12: DB5 pin 33: GPIO 13 | pin 11: DB4 pin 35: GPIO 19 | pin 6: E pin 37: GPIO 26 | pin 4: R/S pin 39: ground | pin 5: R/W functions of the LCD pins used: pin 1: ground pin 2: 5V input pin 4: R/S (register select) pin 5: R/W (read/write) pin 6: E (enable signal) pin 11: DB4 (data bit 4) pin 12: DB5 (data bit 5) pin 13: DB6 (data bit 6) pin 14: DB7 (data bit 7)
Very important: plug the LCD pin named R/W into a ground pin of your raspberry pi to be sure the LCD is always in write mode and does not send any voltage to the raspberry pi which can only accept 3.3 V. The modules seems to send 5 V (not checked, and I won't ckeck this, this is what I deduce from documentation found here and there) which can damage the raspberry pi (from what I read here and there).
People who make raspberry pi: it would be a good idea to protect your GPIO from over-voltage for your future models so that if someone sends 5 V (or more) by accident, nothing bad happens to the raspberry pi. (I have no idea how hard this protection would be to implement.)
Edit the code, change the GPIO to match your setup. Compile.
To use the program, first run:
sudo ./lcd -clear
Then to print something:
sudo ./lcd -no-init "hello world"
To print on second line:
sudo ./lcd -no-init -l 2 "second line!"
To clear the screen and print something on first line:
sudo ./lcd -no-init -clear -l 1 "alert detected"
This is very basic. You can edit the program to fit your needs
if this simple API is not enough. The use of sudo
is
not very satisfying to my taste. I'll live with it for the time
being. (It's not too hard to make a daemon that would accept commands
from non sudo
programs. Maybe some day... depends on
laziness.) (There is also the library pigpiod_if2
that can
be used together with the existing daemon pigpiod
, but
it requires a rewrite of the program to use this different library.)
The timings as found in the code work with my LCD module. Maybe you would need to change them for yours.
Very important again: be sure to be the exclusive user of
the raspberry pi's GPIO pins you choose. The program uses the library
pigpio
. I have no idea how this and all other components
of a running raspberry pi mix together. I chose GPIO pins as found in
a video from the internet. I hope none of the ports I chose are in use
by some other components of the system. (If I understand correctly,
technically pigpio
uses mmap
to map the IO memory of the raspberry pi and
programs it by writing at this and that address. I am not sure if there
is some kind of "reservation" done to, first, check that no one is using
the GPIO pins and to, second, inform everyone that we now use them.)
I'm a bit of a noob here and I don't understand everything. So be careful
with your hardware so as not to damage it.
My LCD is an SC1602DU. The pins are placed on the left. Refer to your model's documentation to know what function each pin has.
All links were valid 2024-09-23.
Contact: see this
Created: 2024-09-23
Last update: 2024-09-23