Hello friends, I hope you all are doing great. Today, I am going to share the 6th lecture in the Raspberry Pi 4 Programming series. We're glad you could join us for another lesson in our comprehensive Raspberry Pi programming guide. In today's guide, I'll show you how to interface a 16x2 LCD screen with Raspberry Pi 4.
So, let's get started:
Today, we are going to interface a 16x2 LCD screen with Raspberry Pi 4. At first, we will print the "Hello World" text on the LCD, and in the last section, we will implement the scrolling and blinking of text on the LCD.
We will need the following components for today's project:
The header pins of 16x2 monitors are not pre-soldered. Normally a male header pin is soldered to the LCD pin holes.
We can perform 2 types of communication modes in LCDs, named:
In 8-Bit mode, all 8 data pins are used to send data, while in 4-Bit mode, the last 4 pins(D4-D7) are used for data transmission.
LCDs employ two distinct registers:
You can use the RS pin on LCD to alter the register. If RS is High, we are accessing the data register and if it's Low, we are accessing the command register.
The LCD's command register keeps track of the user's command. Pre-display data is saved in the data register. In order to manipulate the display, one must first load the instruction register with commands and then load the data registers to display the data. If you're working on a Raspberry Pi project and want to avoid learning low-level commands, you can use the Liquid Crystal Library instead.
The screen's brightness can be adjusted from Pin 3, normally a potentiometer is placed at Pin 3 to adjust the brightness. You can also use a resistor if you don't have a potentiometer. If a resistor is used, try one between 5k-10k ohms. You should experiment with a few different values to get the optimal resistance.
LCD works on the principle of light transmission from one layer to the next via molecules. These units vibrate and align themselves in a way that the polarized sheet gets at an angle of 90 degrees, allowing light to pass through. In other words, these molecules inspect the information on every pixel. Every single pixel uses the light absorption technique to display the digits. It is necessary to adjust the molecular orientation to the incident light angle in order to show the value.
The 16x2 LCD screen can easily be connected to the Raspberry Pi 4. There will be a lot of cables to connect because LCD has 16 pins, but nothing too complicated. Here's the schematic of the pin connections between RPi4 and LCD:
Having done so, the screen should power up and establish a connection with the RPi.
The newest Raspbian release has all the necessary packages loaded out of the box to allow for GPIO communication. But we need to install the Liquid Crystal Library to work on the LCD. Let's do that:
git clone https://github.com/pimylifeup/Adafruit_Python_CharLCD.git
cd ./Adafruit_Python_CharLCD
sudo python setup.py install
After the installation, you can use the Adafruit library from any Python program on the Pi. Just paste this line into the beginning of your Python file to make use of the library.
import Adafruit_CharLCD as LCD
The Adafruit LCD library makes it simple to display data from Raspberry Pi to LCD screen. The library package also has several working examples of utilizing the LCD. Before running any of these examples, make sure the pin parameters at the top of the program reflect your setup. My Circuit should yield the following results.
lcd_rs = 25
lcd_en = 24
lcd_d4 = 23
lcd_d5 = 17
lcd_d6 = 18
lcd_d7 = 22
lcd_backlight = 4
lcd_columns = 16
lcd_rows = 2
cd ~/Adafruit_Python_CharLCD/examples/
sudo nano char_lcd.py
Change the values in this section to match the ones described above for the pin configuration. To save the code, hit CTRL+X+Y on your keyboard. To execute this code, open a terminal and type Python followed by the name of the file (including the extension).
python char_lcd.py
In this session, I'll go over the fundamental Python methods for interacting with the screen. To initialize the pins, it is necessary to invoke the following class. Before calling the class, make sure all the parameters have been defined.
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight)
After that, you can adjust the screen to your liking. In this short guide, I'll give you a taste of what you can do with the Adafruit library.
The Ardafruit CharLCD.py file in the Adafruit CharLCD folder of the Adafruit Python CharLCD folder will list all the accessible methods.
sudo nano ~/Adafruit_Python_CharLCD/Adafruit_CharLCD/Ardafruit_CharLCD.py
My simple script for displaying user-entered text is included below.
#!/usr/bin/python
# Example using a character LCD connected to a Raspberry Pi
import time
import Adafruit_CharLCD as LCD
# Raspberry Pi pin setup
lcd_rs = 25
lcd_en = 24
lcd_d4 = 23
lcd_d5 = 17
lcd_d6 = 18
lcd_d7 = 22
lcd_backlight = 2
# Define LCD column and row size for 16x2 LCD.
lcd_columns = 16
lcd_rows = 2
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight)
lcd.message('Hello\nworld!')
# Wait 5 seconds
time.sleep(5.0)
LCD.clear()
text = raw_input("Type your name in the terminal ")
LCD.message(text)
# Wait 5 seconds
time.sleep(5.0)
LCD.clear()
lcd.message('Goodbye\nWorld!')
time.sleep(5.0)
LCD.clear()
If everything's fine, you will get something printed on your screen, as shown in the below figure:
If your Python script isn't producing any output on the screen, it's probably due to incorrectly configured pins.
This guide walked you through connecting the Pi 4 to a 16x2 LCD. You can accomplish so much more with this sleek screen. You may set up a script to run at boot time and show useful information like the IP address, time, temperature, and more.
You can also incorporate a wide variety of interesting sensors with this screen. A temperature sensor like the DS18B20 would be ideally suited for use with the screen. Refresh the screen every few seconds to reflect the current temperature.
Please let me know how successful you were in putting up a Pi 4 with LCD 16x2 display with the help of this tutorial. In the next tutorial, we will interface Keypad 4x4 with Raspberry Pi 4. Till then, take care. Have fun !!!
JLCPCB – Prototype 10 PCBs for $2 (For Any Color)
China’s Largest PCB Prototype Enterprise, 600,000+ Customers & 10,000+ Online Orders Daily
How to Get PCB Cash Coupon from JLCPCB: https://bit.ly/2GMCH9w