Using Python to Interact with LCD Screen on RaspberryPi
In this tutorial, we will show you how to connect a 1602 LCD screen to your RaspberryPi and display a message. The 1602 LCD screen is a popular display module that can be used to show text and numbers. It is widely used in different projects, such as temperature monitoring, clock display, and more. This tutorial assumes that you have basic knowledge of Raspberry Pi and Python programming.
- Raspberry Pi
- 1602 LCD screen
- Jumper wires
- 1k and 10 Ohm resistors (for the LCD brightness and contrast)
Note: We could have used two potentiometers instead of resistors to adjust the contrast and brightness. However, we decided to keep the design simple in this work.
CONNECTING THE 1602 LCD SCREEN TO THE RASPBERRY PI
The 1602 LCD screen has two rows of 16 characters each, and it requires 16 pins to operate. However, It can be implemented in three different ways:
- 8-bit mode
- 4-bit mode
In this post the 4-bit mode is used, which requires only 6 pins. The 6 pins are divided into three groups:
- power pins
- control pins
- data pins
The power pins provide the necessary voltage to the LCD screen. The 1602 LCD screen requires a 5V power supply, which can be obtained from the RaspberryPi’s 5V pin.
The power pins include:
- VSS (Ground): Connect this pin to GND on the Raspberry Pi.
- VDD (Power): Connect this pin to the 5V pin on the Raspberry Pi.
- V0 (Contrast): Connect this pin to a 1k Ohm resistor and then to the ground. In case of using a potentiometer to adjust the contrast: A potentiometer has 3 pins, connect the centre pin of the potentiometer to V0 of the LCD, one pin to the ground, the last one is not used.
- A (Brightness): Connect this pin to a 10 Ohm resistor and then to the ground. In case of using a potentiometer to adjust the brightness: A potentiometer has 3 pins, connect the centre pin of the potentiometer to VCC (power), one pin to A, the last one is not used.
The control pins are used to control the operation of the LCD screen. They include:
- RS (Register Select): This pin determines whether the data sent to the LCD screen is a command or a character. Connect this pin to GPIO 26 on the RaspberryPi.
- RW (Read/Write): This pin determines whether the data sent to the LCD screen is a read or a write operation. Connect this pin to GND on the Raspberry Pi.
- EN (Enable): This pin enables the LCD screen to read the data on its data pins. Connect this pin to SP11 MISO on the Raspberry Pi.
The data pins are used to send data to the LCD screen. We will be using the 4-bit mode, which requires only 4 data pins. Connect the data pins to the following GPIO pins on the Raspberry Pi:
- D4: GPIO 13
- D5: GPIO 6
- D6: GPIO 5
- D7: SP10 SCLK
This diagram explains how to connect everything together:
DISPLAY A MESSAGE ON THE 1602 LCD SCREEN
After connecting the LCD screen to the Raspberry Pi, we can now display a message on it. We will be using the Python programming language to interact with the LCD screen.
First, you will need to import the necessary libraries. Open a new Python file and import the RPi.GPIO and time libraries.
import RPi.GPIO as GPIO import lcdmodule as lcd
Note: The lcdmodule in the above snippet is a customized Python file that can be downloaded from the GitHub repository at the end of this post.
Then you can used the following code to initialize the Raspberry Pi pins:
Here are a few LCD modules functions you can use to print a message on the LCD screen:
# to initialize the lcd (erase in case it is displaying) lcd.lcd_init() # print command, 0 means from which character on the lcd it should start # you can change 0 and 1 and see the difference on your lcd lcd.lcd_string(text,0,1) # to print in line 1 lcd.lcd_byte(lcd.LCD_LINE_1, lcd.LCD_CMD) # to print in line 2 lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
Finally, you can run the following Python script example. The code below will display a 32-character text on the screen. You can download the code and its dependent library from this github repository: https://github.com/semfionetworks/RaspberryPi-Plus-LCD
import lcdmodule as lcd import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) text = "* SemFio Sample * Python Script" lcd.lcd_init() if len(text) == 15: lcd.lcd_string(text,0,1) else: lcd.lcd_byte(lcd.LCD_LINE_1, lcd.LCD_CMD) lcd.lcd_string(text,0,1) lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD) lcd.lcd_string(text[16:],0,1)
In this tutorial, we showed you how to connect a 1602 LCD screen to your Raspberry Pi and display a message. The 1602 LCD screen is a versatile display module that can be used in many projects. By using the Python programming language, you can interact with the LCD screen and display different messages or add more functionalities. Now that you know how to use the 1602 LCD screen with your Raspberry Pi, you can start building your own projects!