• Poornima Mani

Interfacing 16*2 LCD with Raspberry Pi using an I2C backpack.

Updated: Aug 17, 2020

This article will teach you How to interface a 16*2 LCD with #RaspberryPi using an #I2C module. The process is very simple and can be used for various projects on which you will be working in the future. The article provides you with a step by step guide on "What all components will be required?", "how to connect the LCD with #RaspberryPi?" and working explanation.


List:

The list provides all the hardware and software components that will be required for the interfacing.


Hardware -


1. #RaspberryPi: #RaspberryPi is a single-board microcomputer which utilizes the SoC, DDR RAM memory, ethernet port, USB host, and micro HDMI on it. It provides the opportunity to interact with the outside world and using its processing power in a compact board at a low cost. In this application, we have used the Python IDE to simplify it.


Raspberry Pi

2. LCD Display- 16*2: This is a versatile electronic module that can display 16 characters per line (this module has 2 such lines). It is cheap and easy to use. Keep reading to know about the implementation.


3. #I2CBackpack/Module: This is a general-purpose bi-directional I/O port expander which is used to convert 8-bit data from I2C serial to 8-bit parallel. A major advantage of this module is that it requires only 2 GPIO lines and hence saves a lot of hardware resources.


4. USB Cable for power supply: This cord is used to temporarily connect the #RaspberryPi to the power supply.


5. Male - Female Jumper wires: As simple as it looks, these are used to connect ends without soldering. While male wired have protruding pins to plug into things, female jumpers are the ends that are used to plug into.


Software-


1. SSH Terminal: SH Terminal is used in this project to connect the #RaspberryPi to the terminal of another computer.


2. Python IDE: How does this #RaspberryPi actually work? Programming is done in an IDE (Integrated Development Environment) is a platform where we can write code, run and test the working.


Circuit:


The current address of the I2C module is 0x27. The black wire shown in the circuit is the connection of the ground terminal and the red being Vcc (5V). Though there are multiple pins for ground, the idea is to make the circuit tidy and use the closest supply pin. The SDA and SCL at pin3 and pin5 respectively are responsible for data communication.

What goes where?

  1. RPi Pin 2 - 5V Power

  2. RPi Pin 3 - SDA

  3. RPi Pin 5 - SCL

  4. RPi Pin 14 - Ground


Code:


Initially, all the necessary time modules are imported and 'mylcd' is used as a reference.

Inside the while loop, enter the text messages to be displayed. The function used is lcd_display_string where the parameters are the string to be displayed, line number, and row number respectively.

The sleep function is used to provide a delay in the display and the clear function is used to empty the LCD screen.


import time
import I2C_LCD_driver
mylcd = I2C_LCD_driver.lcd()

while True:
    try:
       mylcd.lcd_display_string("Welcome to",1,3)
       mylcd.lcd_display_string("LearnElectronics",2)
       time.sleep(3)
       mylcd.lcd_clear()
       mylcd.lcd_display_string("How are you",1,3)
       mylcd.lcd_display_string("doin'?",2,5)
       time.sleep(3)
       mylcd.lcd_clear()
       mylcd.lcd_display_string("Stay Home",1,4)
       mylcd.lcd_display_string("Stay Safe",2,4)
       time.sleep(3)
       mylcd.lcd_clear()
       mylcd.lcd_display_string("And never",1,4)
       mylcd.lcd_display_string("stop learning",2,2)
       time.sleep(3)


    except KeyboardInterrupt:
       mylcd.lcd_clear()
       mylcd.lcd_display_string("See you",1,5)
       mylcd.lcd_display_string("Until next time",2,1)
       break

Download the code -

I2C_LCD Main code
.txt
TXT • 820B


I2C_LCD_driver_code
.txt
TXT • 5KB




Note: Convert the file in python-format(.py) to run the above Main code successfully.


Working:


Ensure that the wiring is proper and adjust the lighting using the trimmer on the I2C module. Once the circuit is implemented, enable the I2C interface so that the code can be run. As we power on, the LCD starts flashing with the message that has been programmed into the #RaspberryPi.


Check the output from the images displayed below.

The #I2Cmodule used with the 16*2 LCD saves a lot of wires and makes the circuit less complex. Also, you get to learn the process of #I2C communication while interfacing LCD with #RaspberryPi.


Watch the practical implementation of the above-explained project from the video below.


Video By - Sai Parimal

Learn Electronics. (June 17, 2020). Interfacing LCD with Raspberry Pi using I2C Backpack.

https://www.youtube.com/watch?v=aoMvF5L5Kl4.

See Also:


This circuit can be utilized in many bigger projects such as control panels, smart radios, hospital monitoring systems, etc. In fact, you could come up with an even better idea and make use of this circuit in your hobby projects as well.


Check these out:

  1. Smart Lighting System

  2. Fingerprint Sensor-based attendance system using Raspberry Pi

Keep learning, keep growing!

#Interfacing

#I2CBackpack

#RaspberryPi

#Hardware

127 views