Dino Fizzotti

Engineer, maker, hacker, thinker, funner.

Jun 10, 2017 - 4 minute read - Comments - Electronics Software Python Raspberry Pi

A Python wrapper for the Adafruit USB/Serial LCD Backpack

Hello World

Overview

I'm working on a project which requires a character LCD to work with a Raspberry Pi. A character LCD is thing displaying the text in the image above. The one I am currently using is a “green backlit 16x2” character LCD. This means that the display is capable of displaying 2 rows of 16 characters, and it features black characters on a green background. There are various sizes and colour combinations (text and backlight) available. Different character LCDs have different ways of connecting to computers and microcontrollers. I won't go into any more technical detail on character LCDs, if you need more information check out this Adafruit tutorial or this great 2-part video series on explaining character LCDs by The 8-bit Guy (part I, part II).

The model I am using exposes 16 pins to use to connect to a host device and conforms to the HD44780 controller protocol

Pins on the character LCD module

In the past when I have needed to interface this type of character LCD with a microcontroller or Arduino I have just connected the pins directly to the pins of the microcontroller/Arduino board. This is also possible with the Raspberry Pi and the Pi's general purpose IO pins, but for my current project I need quite a few IO pins and so was looking for a different solution. The other obvious option for connecting peripheral devices to the Pi is to use one of the four USB ports available (of which I am only using 1 currently). A short search later and I found the Adafruit USB + Serial LCD Backpack. Perfect!

Adafruit USB/Serial LCD Backpack

Adafruit USB/Serial LCD Backpack

The backpack works by acting as a “middle-man” between the Pi and the character LCD. You are able to connect your Pi (or other computer/microcontroller) to the backpack either by USB, or using a serial UART interface if your device supports it. The LCD connects to the backpack directly via a row of connectors, either directly against the LCD PCB (thus forming a real “backpack”), or via jumper wires (as seen in the top image).

Adafruit has a great getting started guide for this product and is what I initially used to test and prototype. Without going into too much detail (it is covered in the guide), the way the backpack works is that you need to open a serial connection to the backpack using your USB port, and then send text over this serial connection, which is then displayed on the LCD. In addition to just sending text one can also use special commands to do things like increase/decrease the backlight brightness or set a specific “cursor” position, in addition to many more.

Python

The software component of my project on the Pi is written in Python. To connect to serial devices (USB/UART) in Python one can use the pySerial Python package. Using pySerial you can write Python scripts which send text or one of the special commands to the LCD. Writing the special commands involve sending specific sequences of bytes to the backpack. There is a list of these byte sequences in the Adafruit tutorial.

I initially hard-coded all the special commands I needed to use directly in my Python script. It wasn't that easy to read, and so I then moved the byte sequences up to the top of the file and saved them in variables which read as “constants” (there is no real constant type in Python):

COMMAND_START = 0xFE
CLEAR_DISPLAY = 0x58
AUTOSCROLL_ON = 0x51
AUTOSCROLL_OFF = 0x52
...

Once I was done listing all the commands and writing helper methods to implement some of them for my specific uses, I realised that it might be useful to encapsulate these commands and helper methods in a Python package which I could use in other projects requiring Python control of a character LCD.

Wrapping it up

And that is what I did! (disclaimer: I did reference the existing Adafruit test script for inspiration)

The package is very simple, and really just provides convenience methods for implementing some of the special commands. You can take a look at the code to see what is exposed.

I also used this excercise as an excuse to figure out how to push a package to the Python Packaging Authority, so that it could be easily installed by other developers/makers using pip install lcdbackpack. PyPI link here.

For more information, code, and usage instructions click here to check out my helper library for the Adafruit USB/Serial LCD Backpack on GitHub.