Using An I2C Enabled LCD Screen With The Raspberry Pi

6

In previous posts I’ve covered using 16×2 and 20×4 LCD screens with the Raspberry Pi using Python. These are relatively easy to use but require a number of connections to be made to the Pi’s GPIO header. This setup can be simplified with the use of an I2C enabled LCD screen.

These can either be purchased as a complete unit or you can attach an I2C backpack to a standard screen.

Here is an I2C backpack :

I2C LCD Backpack

and here is a backpack soldered to the back of a standard 16×2 LCD screen :I2C LCD Backpack

This is a great way to add an I2C enabled LCD screen to your Pi with only four wires!

Step 1 – Connect Screen to the Pi

i2c Level ShifterThe I2c module can be powered with either 5V or 3.3V but the screen works best if it provided with 5V. However the Pi’s GPIO pins aren’t 5V tolerant so the I2C signals need to be level shifted. To do this I used an I2C level shifter.

This requires a high level voltage (5V) and a low level voltage (3.3V) which the device uses as a reference. The HV pins can be connected to the screen and two of the LV pins to the Pi’s I2C interface.

Level Shifter Pi I2C Backpack
LV 3.3V (pin 1)
LV1 SDA (pin 3)
LV2 SCL (pin 5)
GND GND (pin 6) GND
HV 5V (pin 2) VCC
HV1 SDA
HV2 SCL

While experimenting I found that it worked fine without the level shifting but I couldn’t be certain this wasn’t going to damage the Pi at some point. So it’s probably best to play it safe!

Step 2 – Download the Example Script

The example script will allow you to send text to the screen via I2C. It is very similar to my scripts for the normal 16×2 screen. To download the script directly to your Pi you can use :

wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/lcd_i2c.py

Step 3 – Enable the I2C Interface

In order to use I2C devices you must enable the interface on your Raspberry Pi. This can be done by following my “Enabling The I2C Interface On The Raspberry Pi” tutorial. By default the I2C backpack will show up on address 0x27.

Step 4 – Run Script

Running the script can be done using :

sudo python lcd_i2c.py

Hopefully you will see something like this :

I2C 16x2 LCD Screen

Troubleshooting

If you are having problems then :

  • Double check your wiring
  • Start from a fresh Raspbian image
  • Make sure you set up I2C as mentioned in “Enabling The I2C Interface On The Raspberry Pi” tutorial
  • Check the screen is detected when you follow the “Testing hardware” section in the above tutorial
  • Ensure SDA and SCL are correctly wired to Pins 3 and 5 on the Pi via the level shifter
  • Adjust the contrast ratio using the trimmer on the i2c backpack
Share.

6 Comments

  1. On the backpack shown (and on the one I use) there is an NPN transistor connected to pin 16, which controls the backlight – as you’ve implemented in your previous posts. The backlight can be turned off by setting LCD_BACKLIGHT = 0 in your script.

    Also, some backpacks use i2c address 0x3f, not 0x27.

  2. Hi,

    Your lcd_i2c.py contains somes typos on lines 42-43 (#comments): they say the opposite as lines 74-75.

    Thanks a lot, I’m using it right now.

  3. Isn’t the level shifter a bit unnecessary? The SDA and SCL pins are outputs from the Pi, so they are sending out 3.3v logic levels, not receiving them? As long as the backpack can cope with 3.3v logic, and +5 to the PCF8574 + LCD you should be fine. The code works fine so thanks!

  4. Thanks Matt!
    This seems to be the only script that worked for me so far.
    I still have a “challenge” and hope you can help here.
    The root cause is that I’m a total noob in terms of Python.
    I need your script to read the content of the text file (I did it already and it’s ok) when launched and pass the selected content as a static text (in this case artist and song title) to the screen and that’s it. The script will be automatically launched by the player (MoOde audio) when something changes (it’s covered by the player itself, where I can define the script I want to launch). Once this is working – I’m happy and can start thinking about more advanced level, where this text will be scrolled when can’t fit the screen.
    Thanks in advance!

Leave A Reply