Raspberry Pi

How to scroll the text on the 16×2 LCD using Raspberry Pi 4

The 16×2 LCD is used with the Raspberry Pi 4 to display different strings and characters on the display screen of the LCD. The text displayed on the screen of the LCD can be scrolled right or left of the screen for advertisement purposes or to gain the attention of the audience. The Raspberry Pi is a small size computer board that can be used to configure different electronic projects.

In this write-up, we will learn the scrolling of text on the 16×2 LCD screen which is interfaced with the Raspberry Pi 4.

How to interface the LCD with Raspberry Pi 4

To connect the LCD with the Raspberry Pi 4, we need the following components

  • 16×2 LCD
  • Jumper wires
  • Breadboard
  • 1 potentiometer

To scroll the text on the 16×2 LCD, the circuit diagram will be:

In the first step, we will place the LCD, a potentiometer, and the Raspberry Pi 4 on the breadboard to configure the circuit:

Connect the pins of the 16×2 LCD with the GPIO pins of Raspberry Pi according to the table:

GPIO pins of Raspberry Pi BCM pins of Raspberry Pi LCD pins
GPIO 22 15 RS
GPIO 24 18 RW
GPIO 23 16 E
GPIO 9 21 D4
GPIO 25 22 D5
GPIO 11 23 D6
GPIO 8 24 D7
Ground 6 K, VSS
5V 2 VDD, A

Other than these connections, the Vo pin of the LCD is connected to the output pin of a potentiometer and the remaining two pins of potentiometer are connected to the 5V and the ground of the Raspberry Pi.

How to download the RPLCD library in the Raspberry Pi 4

We have to download the RPLCD library to interface the LCD with the Raspberry Pi. The RPLCD library contains the functions which are used to control the display of the LCD. To download the RPLCD library, we will run the command:

$ wget -c https://github.com/dbrgn/RPLCD/archive/refs/heads/master.zip

After the master.zip has been downloaded, we will unzip the “master.zip” to use its contents:

$ unzip master.zip

Now, using the below mentioned command, we will navigate to the unzipped folder, RPLCD-master:

$ cd RPLCD-master

What is the Python code for scrolling text on 16×2 LCD using the Raspberry Pi 4

Now, we will create a Python file using the nano editor (you can use any other Python IDE as well) to type the Python script for scrolling the text on LCD:

$ nano myfile.py

When the file is open, type the following statements of the Python code to scroll the text on 16×2 LCD using the Raspberry Pi:

import RPi.GPIO as GPIO
#importingRPi.GPIO library

from RPLCD.gpio import CharLCD
#importing CharLCDfrom  RPLCD.gpio

import time
#importing the time library

GPIO.setwarnings(False)
#setting warnings to false

framebuffer = ['Hello!','',]
#a data structure "frame buffer" has defined with two elements

def write_to_lcd(lcd, framebuffer, num_cols):
#defined a function of "write_to_lcd" with three parameters
lcd.home()
    #used to place the cursor of lcd at (0,0) position of LCD
    for row in framebuffer:
    #initiated a for loop
lcd.write_string(row.ljust(num_cols)[:num_cols])
        #displayed the values of "frame buffer"
lcd.write_string('\r\n')
        #placed the pointer in new line and new row

lcd = CharLCD(pin_rs=15,pin_rw=18, pin_e=16, pins_data=[21, 22, 23, 24],
numbering_mode=GPIO.BOARD,
              cols=16, rows=2, dotsize=8,
auto_linebreaks=True, compat_mode=True)
#defined the lcd pins with GPIO pins of Raspberry Pi

write_to_lcd(lcd, framebuffer, 16)
#calling the function and passed the parameters especially num_cols

long_string = 'Welcome to the LinuxHint'
#store a string in variable "long_string"

def loop_string(string, lcd, framebuffer, row, num_cols, delay=0.5):
#defined another function loop_string

    padding = ' ' * num_cols
    #spaces with num_cols and storing in "padding"
    s = padding + string + padding
    #declaring a new variable and store values in it

    for i in range(len(s) - num_cols + 1):
    #declared a new infinite for loop

        framebuffer[row] = s[i:i+num_cols]
        #store values in framebuffer array data structure
write_to_lcd(lcd, framebuffer, num_cols)
        #displayed the output on LCD
time.sleep(0.5)
        #generated a delay of 0.5 seconds

while True:
#initialize the infinite while loop

loop_string(long_string, lcd, framebuffer, 1, 16)
    #call the function loop_string

Explanation of code: The above script of the Python to scroll the text on the 16×2 LCD looks complex but it’s very easy, so we will explain it in different parts.

Import of libraries: First, we have imported three libraries, RPi.GPIO to use the GPIO pins of the Raspberry Pi, CharLCD from RPLCD.gpio to use the LCD, and sleep from time library to generate the delays.

Warnings: We set the setwarnings to False, so it will not generate any warnings while using the LCD with the Raspberry Pi.

Data structure: We have defined a data structure with the name of “frame_buffer” which contains two elements, one is a string of “Hello” and the other is an empty string.

write_to_lcd: We have defined a function in which we just set the cursor at (0,0) position of LCD, prints the first element on the left side of the LCD and put the empty spaces on its other side, then set the cursor at the right position of the LCD on a new line.

Initialize the LCD: In this part, we simply assign the Raspberry Pi 4 pins to the LCD pins and here we are using the board pin numbers of the Raspberry pi instead of the GPIO pin numbers and it is mentioned in the command by setting the numbering_mode= GPIO.BOARD.

Call the write_to_lcd: We have called the function by passing it the number of cols which is 16 so that the elements of the frame_buffer have been set on the LCD display.

long_string: Declare a variable and store a string “Welcome to the LinuxHint” which we want to scroll on the LCD.

loop_string: In this function, we padded some spaces before and after the scrolling text and with the help of a for loop, we moved the text to the right position of the display.

while: Finally, in the infinite while loop, we call the function of loop_string and display the text on the LCD with the scrolling text.

After this, we will run the command to compile and run the above Python script:

$ python myfile.py

The hardware working of the above script on the LCD is:

Conclusion

The LCD can be interfaced with the Raspberry Pi by using the library of the RPLCD which can be downloaded by using the command discussed in the article. To scroll the text we have to use a Python script with some for loops as there is no specific command to scroll the text on LCD in the RPLCD library. In this write-up, we have discussed the Python code to scroll the text on the LCD by interfacing it with the Raspberry Pi 4 in detail.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.