Posted on Leave a comment

How to connect an LCD display to a Raspberry Pi

Raspberry Pi LCD display

If your display is equipped with an IC2 module, it’s not that difficult to connect an LCD display to a Raspberry Pi. Learn with this tutorial how to connect and to program an 1602 LCD with a Raspberry Pi.

There are many types of LCD displays. In this tutorial we are using the popular and affordable 1602 LCD. The LCD has an IC2 module soldered on it (see the pictures below). If your LCD is of the same type, but has a different size, it won’t be a problem to continue with this tutorial. You’ll just have to correct some parameters in the Python script. But if it is from a different type or it has no I2C module, you better look for another tutorial.

  1. Prepare the hardware

    – First, you need to have a Raspberry Pi running on the latest version of Raspberry Pi OS. This version includes “Thonny”. We’ll use this user-friendly IDE to write our Python code. If you’re not familiar with Python or with Thonny or GPIO-pins, I suggest to have a look at our tutorials “How to write your first Python program on the Raspberry Pi” and/or “How to use the Raspberry Pi GPIO pins” to have a quick introduction.

    Next you’ll need some extra components :
    – a breadboard (we are using a 400 points breadboard)
    – a 16×2 or 1602 LCD with I2C bus module
    – Dupont jumper wires
    –  a T-cobbler (optional)
    –  a 40 pin GPIO cable (optional)

    Raspberry Pi LCD display breadboard

    Visit our shop if you miss any components.


  2. Enable I2C on the Raspberry Pi

    Besides preparing the physical stuff, we need to configure our Raspberry Pi to be able to communicate with I2C peripherals.

    For this we open: applications menu > Preferences > Raspberry Pi Configuration as shown in the picture below.

    Raspberry Pi raspi-config
    Once the Raspberry Pi Configuration window opened, click on the tab “Interfaces”. Select here “Enabled” in front of I2C.

    Raspberry Pi enable I2C
    To test if I2C is enabled, you can open a terminal window and enter following command :
    ls /dev/*i2c*
    Your Pi should respond with :
    /dev/i2c-1

  3. Get to know the 1602 LCD

    In this tutorial we are using the popular and quite basic 16×2 or 1602 LCD. It can display 16 characters per line on 2 lines. Each character is made from a matrix with 5×7 dots. It is equipped with a backlight for easy reading. Besides sending text, thanks to specific commands, we can give instructions to the display, as to switch on/off the backlight for example.

    The display we use in this tutorial is equipped with a I2C-module (black part on the picture below). I2C is a communication protocol which allows an easier connection between the display and the Raspberry Pi. Indeed, instead of having to wire all the pins on the top of the screen, we only have to connect the display with 4 wires to our Raspberry Pi.

    Each I2C device has its own I2C address. Often this address is hard-wired in the device and will vary from manufacturer to manufacturer. If you don’t know the address of your I2C device, connect the device to your Raspberry Pi (see the next step). Then, open a terminal window and enter following command :
    i2cdetect -y 1
    The output should be a map with the addresses of all connected devices. If for example there is a ’27’ that appears, it means that the hexadecimal address of your device is ‘0x27’.

    If you bought one of our kits, the hexadecimal address of the LCD is ‘0x27’. We will need the I2C address from the display to insert it in our Python code.


    LCD1602 display with I2C moduleIf you want more information regarding LCD, have a look at the ‘Liquid-crystal display’ webpage of Wikipedia.

    Be careful ! Before starting to connect wires on the GPIO pins of your Raspberry Pi, make sure you properly shut down the Pi and removed the power cable from the board!

  4. Setup the hardware part

    Raspberry Pi LCD display pinout– connect the GND of the display to a GND (ground) pin (black cable)
    – connect the VCC pin of the display with a 5V pin (red wire)
    – connect the SDA pin of the display to GPIO2 (grey wire)
    – connect the SDC pin of the display to GPIO3 (yellow wire)
    Raspberry Pi LCD display breadboard

  5. Write the code

    The aim here is to get a basic message displayed on our LCD.

    Install the Library first
    To avoid extensive and complicated code writing, libraries are often used. For our LCD, we will also be using a library. We found the most appropriate library at GitHub from Dave Hylands . As these files from this quite specific library don’t come automatically with Python, we have to install them ourselves.

    So, before writing the code, we’ll have to upload the files to our Raspberry Pi. You can download a ZIP-folder containing the 2 files to be installed here.

    Download and unzip the files. If you did this operation on your computer, upload the files to your Raspberry Pi. And if you don’t know how to do that, have a look at our tutorial ‘How to transfer files between Raspberry Pi and PC‘. Make sure you upload them in the same folder as the new file we will create for our main code. And don’t change the filenames of the library of course.

    OK, now the library is uploaded to our Raspberry Pi, write or paste following code in the IDE:
    from lcd_api import LcdApi
    from i2c_lcd import I2cLcd
     
    I2C_ADDR = 0x27
    I2C_NUM_ROWS = 2
    I2C_NUM_COLS = 16
     
    lcd = I2cLcd(1, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
     
    lcd.putstr("Great! It Works!")
    lcd.move_to(3,1)
    lcd.putstr("freva.com")


    Raspberry Pi Python 1602 LCD
    Some explanations about the code :

    from lcd_api import LcdApi : to import the first file from the LCD library.
    from i2c_lcd import I2cLcd : to import the second file from the LCD library.
    I2C_ADDR = 0x27 : set the I2C address of the display. Change the I2C address if necessary!
    I2C_NUM_ROWS = 2 : Our display has 2 rows. Change if your display has another size.
    2C_NUM_COLS = 16 : Our display has 16 colums. Change if your display has another size.
    lcd = I2cLcd(1, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS) : to configure the lcd object.

    In the next lines we send characters and give instructions to the display. Feel free to change and to experiment yourself.
    lcd.putstr("Great! It Works!") : to send text to the display.
    lcd.move_to(3,1) : to change the position of the cursor (x,y). x :shift horizontally and y : shift vertically.
    lcd.putstr("freva.com") : sending new text, starting from the new cursor position.

  6. Run your script

    Now, it’s time to save your script. Just make sure you save this Python file in the same folder as the 2 files from the library you uploaded earlier.

    And before running the script, it’s important to adjust the contrast of your LCD. If the contrast isn’t adjusted well, it’s possible you don’t see appearing anything. You can adjust it by turning with a small screwdriver at the blue potentiometer at the back of your LCD (see the pictures here above). Make sure the backlight of the display is on to see the result. If the LCD’s contrast is adjusted right, you can just see the darker rectangles for the characters appear.

    Then, click on the Run button of the Thonny IDE. If you did everything well, the text as in the picture below should appear.

    Raspberry Pi LCD display breadboard

  7. Experiment with additional instructions and characters

    Besides the commands we used in the last lines of our script, there are more possibilities to communicate with the LCD. If you want to learn more about, have a look at this Github webpage.

Congratulations! With this setup you can integrate a display in your Raspberry Pi projects now. Have fun with it!

How useful was this post?

Click on a star to rate it!

Average rating 4.2 / 5. Vote count: 13

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *

How useful was this post?

Click on a star to rate it!

Average rating 4.2 / 5. Vote count: 13

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

find out more products

Be the first to be informed about our latest tutorials and products by subscribing to our Newsletter

freva.com respects your privacy. Read our privacy policy on how we handle your personal information.