Complete Guide Raspberry Pi LCD Display

Your circuit displaying a static message

In this project, you will learn how to connect LCD with Raspberry Pi and how to display a message and scrolling the message.

Required Equipment 

You have to need flowing equipment Raspberry Pi LCD project

  1. Raspberry Pi
  2. Breadboard
  3. 16×2 Hitachi HD44780-compatible LCD
  4. 10 kΩ potentiometer
  5. Jumper wires

software

Adafruit_CharLCD library

liquid crystal display (LIQUID CRYSTAL DISPLAY) Description:

One of the most simple and inexpensive display screen, liquid crystal display (LCD). LCDs are found on everyday electronics devices such as vending machines, calculators (see Figure 1), parking meters, and printers, and are an ideal display for displaying text or small icons.

display
Figure 1 display

The screens of LCDs are measured according to the number of rows of letters and columns. A 16 × 2 LCD can display 2 rows of 16 characters each. LCDs range from 8 × 1 to 40 × 4.

LCDs vary according to the background color. You can get different types of background colors with RGB background lights. Which will help you create any color.

The most common LCD module is the Hitachi HD44780 chip. Where you can use custom characters.

Figure 2 shows a standard 16 × 2 Hitachi HD 44780 (16 × 2 Hitachi HD44780) display. In this project we will use this type of LCD.

A standard 16 × 2 Hitachi HD44780 – compatible LCD
Figure 2: A standard 16 × 2 Hitachi HD44780 – compatible LCD

If you look closely at Figure 2, you can see 32 rectangles made of 5 × 8 pixels. The combination of on and off pixels creates the character size.

When you buy from an LCD market, you will see that in most cases the header pins of the LCD are different with the LCD module. Which is shown in Figure 3. These pins should be soldered to the LCD module to make it suitable for breadboard use.

LCD with separate header pinspins of the
Figure 3 LCD with separate header pin

Pinout of the LCD module: 

are counted from 1 to 16 from left to right while the pins are at the top of the screen.

PINSYMBOLDESCRIPTION
1VSSGround
2VDDPower supply
3V0Contrast adjustment
4RSRegister selection
5R / WRead / Write selection
6EEnable
7–14DB0 – DB7Data pins
15LEDABacklight anode (5V)
16LEDKBacklight cathode (- )

WARNING

The Raspberry Pi GPIOs are designed for 3.3 V, but most LCDs are 5 V. This isn’t a problem as long as you’re only sending data out of the Pi and not reading data from the screen to the Pi. With a 5 V screen, do not connect the R / W pin of the display to the Pi. This pin sends data at 5 V, and it will very likely fry your Pi!

VSS is a ground pin and it needs to be connected to GND. VDD will give 5 V or 3.3 v depending on a power pin and the type of LCD you are working with. However, a maximum of 5 V is required. It is connected to a potentiometer to control V0 characters and backlight brightness.

RS, R / W, E are the control pins. When you only display text like this project, connect the R / W pin directly to the ground. As a result, the LCD will not only read the data.

Pins 7 to 14 are data pins, which are used to transmit data. Anode and cathode for pins 15 and 16 backlight. Although the LCD module has 16 pins. However, Raspberry Pi’s pins 4, 6, 11, 12, 13, and 14 need only 6.

Raspberry Pi LCD Circuit Making

Now your LCD is ready for use. You can start making circuits in this project. Follow the steps below for LCD and Raspberry Pi Wiring using the circuit diagram in Figure 4 for reference.

  1. Connect the breadboard power rails to 5 V and GND.
  2. To power the LCD, connect 5V to LCD pin 2 (VDD) and LCD pin 15 (LEDA) from the power rails. Connect LCD pin 1 (VSS), pin 5 (R / W) and pin 16 (LEDK) to the GND rail of the breadboard.
  3. A potentiometer should be used for contrast control. Connect a pin of the potentiometer to the ground and a pin 5v to the middle pin LCD 3 pin (V0).
  4. Connect the GPIO pins of the raspberry pie according to the following table.
LCDRASPBERRY PI
4 (RS)GPIO 27
6 (E)GPIO 22
11 (DB4)GPIO 25
12 (DB5)GPIO 24
13 (DB6)GPIO 23
14 (DB7)GPIO 18
LCD wired to the Raspberry Pi
Figure 4 LCD wired to the Raspberry Pi

Raspberry Pie LCD program:

To display the message on the LCD, you need to install the Python library called Adafruit_CharLCD before writing the program. This library has many features that will make it easy to control the LCD.

To install the Python library named Adafruit_CharLCD, you need to open the terminal from the Raspberry Pi desktop taskbar. Before installing the Adafruit_CharLCD library you need to install the following dependencies.

pi @ raspberrypi: $ sudo apt update

pi @ raspberrypi: ~ ud sudo apt install build-essential python3 python-dev python-smbus python3-pip git-core

 

When prompted, type Y and press ENTER.

Create a folder called Libraries and change the directory to the newly created folder with the following commands.

pi @ raspberrypi: ~ $ cd ~

the following command to enter the installation files download:

pi spraspberrypi: ~ / Desktop / Libraries $ git clone https://github.com/adafruit/Adafruit_Python_CharLCD.git

pi @ raspberrypi: ~ / Desktop / Libraries $ cd Adafruit_Python_CharLCD

pi / har / desktop / Libraries: setup.py install

Congratulations! You have successfully installed Python Library for LCD characters.

Displaying a Character Message

Go to your project folder and create a new folder called display.

Open Python 3 (IDLE) and click on File ▸ New File to create new code.

Then write the following code in Python Editor and save the file as character_lcd.py.

➊ import Adafruit_CharLCD as LCD

#Raspberry Pi pin configuration
➋ lcd_rs = 27
lcd_en = 22
lcd_d4 = 25
lcd_d5 = 24
lcd_d6 = 23
lcd_d7 = 18
lcd_backlight = 4

#define the LCD size
➌ lcd_columns = 16
lcd_rows = 2

#initialize the LCD
➍ lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6,
lcd_d7, lcd_columns, lcd_rows, lcd_backlight)

#write your message
➎ lcd.message('It works\nYou rock!')

Script Analysis:

➊ import Adafruit_CharLCD as LCD

The Adafruit_CharLCD library has been imported to control LCD.

Lcd_rs = 27

lcd_en = 22

lcd_d4 = 25

lcd_d5 = 24

lcd_d6 = 23

lcd_d7 = 18

lcd_dlight = 4raspberry

Here thepie pins are configured for LCD.

➌ lcd_columns = 16

lcd_rows = 2

Here your LCD size is set. If you use a 20 × 4 LCD, you need to change the two line code accordingly. We have used 16 × 2 LCD in this project, according to what is written here.

C lcd = LCD.Adafruit_CharLCD (lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6,

lcd_d7, lcd_columns, lcd_rows, lcd_backlight)

LCD has been initialized.

➎ lcd.message (‘It works \ nYou rock!’) Is

now useful for displaying your LCD message. Now you called. message (‘string’)

write My message in the string. The escape character refers to the LCD to display the message on the next line. You can change the message as you wish.

Raspberry Pi LCD Program run 

Run Run ▸ Run Module program. Running the program will display the text on the LCD according to the image.

Your circuit displaying a static message
Figure 5: Your circuit displaying a static message

 

LCD Some additional functions Not

only text display but you need to know some more useful functions. You need to clear the message first to set the cursor to a specific position or to prepare new messages. The library you installed provides you with the following functions.

  • lcd.message (string) displays the message written between brackets.
  • lcd.clear () clears the display.
  • lcd.show_cursor (boolean) shows the cursor after the message.
  • lcd.blink (boolean) shows a blinking cursor.
  • lcd.move_right () moves the displayed message one character to the right.
  • lcd.move_left () moves the displayed message one character to the left.
  • lcd.home () sets the cursor to the first column and first row (0,0).
  • lcd.set_cursor (int, int) sets the cursor to the specified column and row. For example, lcd.set_cursor (2, 1) sets the cursor to the third column and second row.

The information displayed in italics in brackets here tells you what kind of value you need to insert into a bracket as an argument. For example, suppose if the place of boolean is true, then true and if it is false, then False.

Scrolling a Reminder Message

The LCD screen used here is quite small. If you want to display large text of 32 characters, you can’t. To solve this problem, we will write a script, through which we can easily scroll through a large text or message.

Displaying a scrolling message is not as straightforward as displaying a simple message. For this we need to think a little more about how to display a scrolling message when writing a script. Let’s take a little advance idea about our project.

  1. In the first row of the display we will display the static title.
  2. In the second row we will display a scrolling message.
  3. The scrolling message will be moved from right to left.
  4. The characters will appear from the right column.
  5. The letters will disappear in the left column.

Keep in mind that this program will continue until it is turned off. To write the program, first open Python 3 (IDLE) in the Display folder and click on File ▸ New File to create new code.

Then type the following code in Python Editor and save the file as scrolling_text.py.

import Adafruit_CharLCD as LCD
➊ import time

#Raspberry Pi pin configuration
lcd_rs = 27
lcd_en = 22
lcd_d4 = 25
lcd_d5 = 24
lcd_d6 = 23
lcd_d7 = 18
lcd_backlight = 4

#define the LCD size
lcd_columns = 16
lcd_rows = 2

#initialize the LCD
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6,
lcd_d7, lcd_columns, lcd_rows, lcd_backlight)

#write your message
➋ title = "Don't forget!"
➌ reminder = "You have a doctor appointment next Monday"

#set the delay for scroll
➍ delay = 0.3

#write a function to scroll the message
➎ def scroll_message(reminder, delay):
padding = ' ' * lcd_columns
reminder_message = padding + reminder + ' '
➏ for i in range(len(reminder_message)):
lcd.set_cursor(0, 1)
lcd.message(reminder_message[i:(i+lcd_columns)])
time.sleep(delay)

➐ lcd.clear()
lcd.home()
lcd.message(title)

#scroll the message in an infinite loop
➑ while True:
scroll_message(reminder, delay)

➊ import Adafruit_CharLCD as LCD

import time

  lcd_rs = 27

lcd_en = 22

lcd_d4 = 25

lcd_d5 = 24

lcd_d6 = 23

lcd_d7 = 18

lcd_dlight = 4 Adfruit_CharLCD

to control LCD and time library to control time. Here the raspberry pie pins are configured for LCD.

 #define the LCD size

lcd_columns = 16

lcd_rows = 2

Here your LCD size is set. If you use a 20 × 4 LCD, you need to change the two line code accordingly. We have used 16 × 2 LCD in this project, according to what is written here.

 lcd = LCD.Adafruit_CharLCD (lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6,

lcd_d7, lcd_columns, lcd_rows, lcd_backlight)

LCD has been initialized.

#write your message

title = “Don’t forget!”

➌ reminder = “You have a doctor appointment next Monday”

Here is the message written.

➍ delay = 0.3

Here is the time set for scrolling. Time has been taken as 0.3.

➎ def scroll_message (reminder, delay):

padding = ” * lcd_columns

reminder_message = padding + reminder + ”

➏ for i in range (len (reminder_message)):

lcd.set_cursor (0, 1)

lcd.message (reminder_message [i : (i + lcd_columns)])

time.sleep (delay)

Here a function called scroll_message has been created. It takes two parameters (reminder, delay). Your message is set in reminder and time in delay. Inside this function, a variable called padding is declared, multiplied by an LCD pen with a blank space (ie, a padding variable containing blank letters). This will add your character slots to an empty square variable. Then a variable called reminder_message was created which added padding, reminder and blank space.

The length of reminder_message has been determined using the len () function through a For loop in the function. With this we can know how many characters are in reminder_message. We also know how many times this loop has to be run.

NOTE

Python uses zero-based indexing, which means that indexes start counting at zero. For instance, the first character of a string has index 0.

 

Inside the loop is a cursor named lcd.set_cursor, from which our reminder will start displaying the message.

On the next line, reminder_message [i: (i + lcd_columns)] is set to show the message individually each time in lcd.message. Each time the loop runs, you will see a separate display of your message. This actually creates the scrolling effect (see Figure 6). Here time.sleep (delay) is set for the deal. After displaying the message, the code (delay bheriyebalatite as well as the number of seconds will be waiting for a slip.

Truncating a string to make a
Figure 6: Truncating a string to make a

to the screen been. It has been instructed to show the title from the first column of the first row.

Hascleared#Scroll the message in an infinite loop

➑ while True:

scroll_message (reminder, delay)

Finally, now you create a loop, which is always true. This is a trick to run something. Now call the function scroll_message (reminder, delay) with your own logic inside that loop, so that the message continues.

Raspberry Pi LCD Program run

Run ▸ Module to run the program. Display the text on the LCD.

RASPBERRY PI TUTORIALS FOR BEGINNERS

 23 total views,  1 views today