Getting an HD44780 16×2 LCD working on the Raspberry Pi – a tutorial

This post is all about HD44780 LCD screens and how to use them in your projects.

I’ve recently been thinking about what my next project will be. I’ve previously experimented with handheld enclosures such as these ones:

and I wanted to do another handheld device. So, I took a look around and found these ones on the Rapid Electronics website:

I thought to myself: that gap in the sloped part looks about the right size for a 16×2 LCD display. These LCDs are quite inexpensive, so I had a look on eBay and found these ones for about £5 which are HD44780-type. In particular, they offer an option with an I2C backpack driven by a PCF8574 chip. I like I2C – it allows you to use many devices on a single ‘bus’ which, in I2C’s case, amounts to 4 wires – one for power, one for ground and two for SDA and SCL, which of course the Raspberry Pi has as you can see in this diagram on pinout.xyz. Here’s a picture of the reverse of the LCD with the I2C backpack showing:

I attached the LCD to the Pi using jumper cables, making sure to get SDA and SCL connected the correct way round (SDA to SDA, SCL to SCL). To start with, I connected the power line to 3v3 but this had the effect of making text come out in black on a blue background. Fortunately, I realised and swapped it over to the 5V line which resulted in the text displaying nice and bright in white (see the picture at the top of the screen).

I then looked around for the software to drive the LCD. I knew that previously I’d used an Adafruit library (and before that, I used the smbus library, which is even more complicated). Fortunately, I came across a library written by Danilo Bargen called RPLCD.

Using this library is very easy. You can see the library source code on the GitHub repository and read all about how to use it at ReadTheDocs.

To install it, do the following to get RPLCD and also install the I2C base library. You will also need to go into Preferences->Raspberry Pi Configuration and make sure I2C is turned on (and reboot).

sudo pip3 install RPLCD
sudo apt-get install python-smbus

Next, run the following command to find out the I2C address of the LCD (mine was 0x3F, which comes in useful later).

i2cdetect -y 1

For me, the pip install placed a test file in the home folder. However, if it doesn’t do this, run the following command:

wget https://raw.githubusercontent.com/dbrgn/RPLCD/master/lcdtest.py > /home/pi/lcdtest.py

Then run the test script by specifying all the required options. These may be different for you, depending on which I2C or other controller you have working for you.

cd ~
python3 lcdtest.py i2c testsuite expander=PCF8574 cols=16 rows=2 addr=0x3f

For a test script with all the parameters set in the Python file itself, and with the LCD initialised, clone my repository:

git clone https://github.com/recantha/HD44780_with_I2C_RPLCD

and run the test.py script:

python3 test.py

This test script does a few things:

  1. Import the necessary libraries.
  2. Initialise the LCD.
  3. Print Hello World to the screen.
  4. Wait 5 seconds.
  5. Clear the screen and power off the backlight.

These are great, cheap screens if all you want to do is display some data or text to the user. I used it on my Picorder way back when and, in my new project (which, yes, is probably going to be another Picorder, but new-and-improved) it will be displaying data from sensors once again.

1 comment for “Getting an HD44780 16×2 LCD working on the Raspberry Pi – a tutorial

  1. Tony Goodhew
    17 March 2018 at 3:42 pm

    This was really useful. The cursor_pos() has been implemented, missing from other APIs I’ve tried. This allows you to update just the values as necessary in different parts of the screen without formatting a whole line each time. Great!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.