This blog has permanently moved to

Contact Form | Email Address

© 2018 All rights reserved by Boseji

Thank you for all your support.

Saturday, April 4, 2015

Raspberry Pi GPIO+SPI to use ST7565 LCD as an Additional Display

We all want to have add-on displays for status outputs on PC. Yes that's the small display on your PC tower displaying how Awesome is the hardware inside is doing. Well we need one for our python scripts running in background on a Raspberry Pi. We had some spare ST7565 driver based LCDs but could not find any generic utility to connect these. So we wrote one ourselves.
These LCDs use SPI communication to configure the driver. Both data and commands are multiplexed using the A0 pin of the LCD.

We found that there are two variants of the LCD:
1. Adafruit Graphics LCD based on ST7565:
 This one had the issue of stange frame locations. These needed some mangling to get it right. Also there was additional column than the mentioned 128x64 resolution of the display. We managed to get this working and added a specific compatibility to our driver. You can buy one of these from Adafruit at http://www.adafruit.com/products/250 and this page contains all the datasheet. However we took the liberty of adding the datasheet to our github repository.

2. Ebay clones Graphics LCDs with ST7565: http://goo.gl/wvmmsl These are the clone of the original Adafruit display but do not have mangling in terms of the pages or the columns. However they have an additional problem, they do not have a roll over function so that when you keep writing data to the LCD row it rolls over to the next. They did not have this function available, so we did an adaptation for that too to help us get the driver running. These are slightly cheaper than the Adafruit ones.

Now for the Raspberry Pi interfacing part we had two choices for the driver:
1. Wiring Pi: This was good but a bit slow for SPI.
2. Pigpio: This was similar but fared better having 20Mhz SPI speed.

We tried out multiple ways of having a Python module do the interfacing work but it was terribly slow in communicating with the LCD. Finally we though of going the C way with Pigpio. And this turned out to be the best choice.

We wanted to have a simple command line interface to work with the LCD. As simple as the 'echo' command in Linux. We needed capability to be able to write, position the cursor and clear the display to begin with.

We created the LCD program a command line tool to communicate with the LCD.
The python scripts can now direct their output to this LCD via commands even though they run in background.

First step was to connect the LCD to the Raspberry Pi, we used a RPi-B+ model as shown in the picture. Here is the pin configuration of J1:

Rpi Connector      ST7565 LCD
3.3v (Pin01)     - LCD Back A
GND  (Pin09)     - LCD Back K
GND  (Pin06)     - GND
3.3V (Pin17)     - VCC

GPIO24           - A0
GPIO25           - nRST

We use the SPI0 bus of the Raspberry Pi to connect the LCD, of course there is MISO as the LCD is only one way traffic. Also we need 2 GPIOs to control the A0 and nRST pins. The nCS chipselect pin used one of the SPI chip select available on the Raspberry Pi (CS0N pin).

Next, since we use the Pigpio program we had to download and install this program.
Here are the instructions:
wget abyz.co.uk/rpi/pigpio/pigpio.zip
unzip pigpio.zip
make install

For the benefit of the users we have created a scrip called 'pigpio.sh' in the github repository to help install this driver.

Finally one needs to download the Repository from Github to build this program. Here are the commands:
git clone https://github.com/boseji/Rpi-ST7565-SPI-LCD.git
This would download and build the code into an executable called 'lcd'

Now for the commands to access the LCD. All commands need root user access we are accessing the hardware. Hence 'sudo' is added to most of the commands. Listed below are the important commands:
  • sudo ./lcd init - Used to initialize the LCD
  • sudo ./lcd c - Used to clear the LCD screen
  • sudo ./lcd w "String" - Used to write the string to the LCD at the current location
  • sudo ./lcd g {COLUMN} {ROW} - This is used to position the current cursor at a give location of the LCD screen
  • sudo ./lcd sleep - Used to put the LCD into Low power mode
  • sudo ./lcd wakeup - Used to restore the LCD from low power mode

The source code of this driver is available on Github: https://github.com/boseji/Rpi-ST7565-SPI-LCD

Our next goal is to build this into a frame buffer driver then the display can be used as an output display for the Raspberry Pi. Let us know your feedback and comments.