Raspberry Pi - LCD
This tutorial instructs you how to use Raspberry Pi with LCD display, and print text, special characters on it.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
Overview of LCD I2C 16x2
The LCD I2C module is a alternative to the standard LCD. It simplifies the wiring between Raspberry Pi and the LCD. The LCD I2C module also has a built-in potentiometer, which can be used to adjust the contrast of LCD.
The LCD I2C Pinout
The LCD I2C needs to be connected to the I2C interface of Raspberry Pi. It has 4 pins:
- GND pin: This needs to be connected to GND (0V).
- VCC pin: This is the power supply for the LCD, and it needs to be connected to VCC (5V).
- SDA pin: This is the I2C data signal.
- SCL pin: This is the I2C clock signal.
LCD Coordinate
The LCD I2C 16x2 has 16 columns and 2 rows, with the columns and rows being numbered starting from 0.
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
To simplify and organize your wiring setup, we recommend using a Screw Terminal Block Shield for Raspberry Pi. This shield ensures more secure and manageable connections, as shown below:
LCD I2C | Raspberry Pi |
---|---|
Vin | 5V |
GND | GND |
SDA | GPIO2 (pin 3) |
SCL | GPIO3 (pin 5) |
Raspberry Pi Code
Detailed Instructions
- Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
- Make sure your Raspberry Pi is connected to the same local network as your PC.
- Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
- If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
- Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
- Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
- Prior to utilizing the LCD I2C with a Raspberry Pi, we need to enable I2C interface on Raspberry Pi. See How to enable I2C interface on Raspberry Pi
- Install the LCD I2C library by running the following command:
- Create a Python script file lcd.py and add the following code:
- Save the file and run the Python script by executing the following command in the terminal:
- Check the LCD for the outcome.
The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.