Raspberry Pi 3 interfacing with 1602 LCD Display
hi guys, welcome to this tutorial. It’s safe to say 80% of the projects you will do with the raspberry pi will be without a screen due to its effectiveness as a server, or at most, the projects will be with a touch screen or a monitor, so this tutorial could be said to exist for those 20% projects for which we may need the 1602 LCD Display.
In this tutorial, I will show you how to print data to an LCD display via a python script.
The following components will be needed for this project;
1. Raspberry pi 3
2. Micro SD Card (8 or 16GB) running Raspbian jessie
3. 1602 LCD display
4. Jumper wires
6. Ethernet cable
7. USB power supply for the Pi
connect the components listed above as shown in the schematics below.
with the connections all done, we can then move to writing the python script.
Preparing the Raspberry pi
with our connections all done, we need to prepare the raspberry pi before writing the code. While I will assume we are all familiar with things like installing an OS on the raspberry pi, and communicate with the pi via a terminal software like putty, You can also find the information on how to do those things with the Pi on several other tutorials on this website like this.
While we could write our own LCD code from scratch and handle all the heavy lifting involved ourselves, it makes no sense since there is already a library that we can use to handle things. We will be using the display library from adafruit which can be downloaded from here. It’s designed for Adafruit LCD boards but will also work with other brands as well. If your display board uses a HD44780 controller then it should work with no issues at all.
follow the steps below to install the library.
1. Clone the adafruit git directory;
git clone https://github.com/adafruit/Adafruit_Python_CharLCD.git2. change into the directory we cloned and install the setup file.
sudo python setup.py
As soon as the installation is done, we are then ready to write the code and call the library from inside our python script.
With the code below we will print hello world to our LCD screen.
Lets drop the whole code first before going through it for explanation.
To explain the code one after the other.
The first two lines of the code includes the needed libraries, which are the time library(used because of the sleep function) and the Adafruit char LCD library.
next thing is to declare the pins on the raspberry pi to which the LCD is connected.
Then we declare the LCD rows and Column size.
Then we call the LCD.adafruit_charlcd functiona and pass in all the screen parameters.
Next we call the lcd.message function. The parameters supplied to this function is displayed on the screen, for this case, hello world was supplied with the \n in the middle serving as an escape character to print the world part on the line below(2nd row).
The time.sleep function is then called. This pauses the system execution to allow the displayed message spend sometime on the screen. after the pause with the sleep function, the LCD.clear function is then called to clear the data being displayed. This makes the screen go blank.
Every other part of the code is pretty much a repetition of the commands explained above.
To get the code working on your raspberry pi, Create a new python file named lcdtest.py by running the code below from the terminal;
sudo nano lcdtest.py
This will open the nano editor. Copy the full code or type it out into the terminal. When done, press CTRL+X followed by y to save and exit the editor.
With this done, run the command;
sudo python lcdtest.py
to run the script. It should work like a charm.
The 1602 LCD is very useful for project where you need to display minimal information to the user, information like, “enter your password”, “login accepted” etc.
You can go through several examples attached to the library to get tons of use cases that could help you fully maximize the capabilities of the LCD module.
That’s it for this tutorial guys. Don’t forget to tune in for sensors Tuesday!
Let me know if you have any questions using the comment section. I always be at hand to reply.
Have a great week.