Raspberry Pi Library for Sharp Memory LCDs

I have now ported my Arduino library for controlling Sharp Memory LCDs to the Raspberry Pi. The extra memory and processing power available on the Pi certainly expand what you can do with this great little display. The lowly Arduino and even the 80 MHz ChipKIT did struggle to complicated process frames of data for the display, but the Pi breezes through them all without any trouble.

I have written my Memory LCD library for Raspberry Pi in C++ (sorry to all you Pi Python fans, I have no plans to port it to Python) and it uses the Broadcom bcm 2835 C library to control the Pi’s GPIO pins. The library is available on my Github page and this article discusses how to get started with Memory LCDs and the Raspberry Pi.

An Important Note Concerning EXTCOMIN

Before I start to discuss installing the software and connecting the breakout board to the Pi, you need to understand about the need to constantly refresh the Memory LCD’s screen while it is turned on.

The only way to avoid constantly refreshing the LCD is to turn it off in one of two ways. The first way is the obvious hard turn off by disconnecting Vin. The second way is a soft turn off by pulling the DISP pin LOW.

The Memory LCD datasheets advise that a periodic VCOM inversion is required to regularly impose a polarity inversion across the LCD panel in order to stop a latent charge from building up within the liquid crystal cells. This inversion should happen at a rate between 2 Hz and 60 Hz and can be triggered in one of two ways:

Software EXTCOMIN Inversion

The first way is via software commands. For this, the jumper JP1 on the rear of the breakout board should be set to LOW, and the user needs to flip a particular bit within the command ID byte when instructions are sent to the pi over SPI at the rate given above.

So even if the image on the LCD is supposed to stay static for a second, a minute, an hour or even longer, commands must still be sent to the LCD over SPI telling it to invert the display’s polarity. This software method is NOT implemented in my library (although a basic function showing how to do it is in the library, it is not automatically invoked).

You can also use the DISP pin to turn the Memory LCD off while the Raspberry Pi remains powered (see below).

Hardware EXTCOMIN Inversion

The second way to ensure polarity inversion is via an eternal square wave supplied to the EXCTOMIN pin on the breakout board. For this to work, JP1 on the rear of the breakout board should be set to HIGH and a square wave of 50% duty cycle and frequency between 2 Hz and 60 Hz should be supplied to the EXTCOMIN pin.

This second, hardware based method of polarity inversion is the one that I recommend. However, using the Memory LCD on the Pi does complicate matters.

When using the Memory LCD breakout board with an Arduino, it’s nice and easy – you can set one of the dedicated hardware PWM pins on the Arduino to generate your square wave and forget about it. That square wave will be output from the moment the Arduino powers on, to the moment it powers off. There’s no danger of the LCD ending up with an image displayed but without receiving a polarity inversion signal, unless you really, really screw up your code and somehow freeze the Arduino, disabling the PWM pin in the process.

On the Raspberry Pi, however, the Memory LCD turns on at the same time as the Pi starts to boot. When the Memory LCD turns on, it also turns a random selection of pixels on – pixels which will need a polarity inversion signal to be supplied. Now, if you’re not running your own Memory LCD program on boot, those pixels are going to sit there with the latent charge building up on them until you choose to start your program that supplies a proper signal to EXTCOMIN.

The same thing happens when you issue the shutdown command to the Raspberry Pi, the Memory LCD continues to draw power from the GPIO header and display whatever image was on it when your program stopped. It only powers off when the USB connection is removed from the Pi.

There are a number of solutions to this – the first is to wire up a 555 timer on the same breadboard as the Memory LCD breakout board and use the 555 in astable mode to supply a squarewave to the EXTCOMIN pin. This is probably the most foolproof method and can be seen in my videos of the Memory LCD and the Pi. If you want to calculate resistor and capacitor values for the 555, use this 555 calculator. To generate an approx 3 Hz squarewave, I am using:

  • C = 20 nF
  • R1 = 2.7 MOhm
  • R2 = 10 MOhm

Although foolproof and therefore ideal for use during development and debugging, using the 555 timer introduces additional external components you can do without if a little more work is done in software.

The second method is to make use of the DISP pin on the Memory LCD breakout board. Whenever this is set LOW, the Memory LCD is turned OFF and the squarewave on EXTCOMIN is not needed. Unfortunately, the Raspberry Pi’s GPIO pins that I have connected DISP to so far, all seem to default to HIGH when the Pi is powered on. For this you will need to write a small program that will run at both boot and shutdown to ensure the DISP pin to LOW, thus deactivating the MemoryLCD until you decide to run whatever program that uses it.

The third method is to run your program as a properly written service that starts at boot and stops at shutdown, managing not just the setting of the DISP pin but also the squarewave generation for EXTCOMIN.

Hardware Setup

First, a warning on connecting the Memory LCD breakout board to the Pi:

I have had a powered-on Pi switch itself off upon inserting a breakout board into a breadboard that already had the necessary connections made with the Pi’s GPIOs.

So, save any open files and power your Pi down gracefully before connecting a Memory LCD to it.

Also, while reading this section you may find it helpful to refer to the Raspberry Pi GPIO diagram here.

You must make the following connections between the Pi’s GPIO pins and the breakout board header to make the demo run. You can alter the GPIO pins used in your own code for SCS, DISP and, EXTCOMIN by changing the pins given when the Memory LCD constructor is called. NB: SI and SCLK cannot be changed.

Connections from Breakout Board –> Raspberry Pi:

  • DISP –> “GPIO 24” [can be changed]
  • EXTCOMIN –> “GPIO 25” [can be changed] (or connect EXCOMIN to the output of a 555 timer)
  • SCS –> “GPIO 23” [can be changed]
  • SI –> “GPIO 10 (MOSI)” [Must be on this pin, is dedicated bcm2835 hardware SPI pin]
  • SCLK –> “GPIO 11 (SCLK)” [Must be on this pin, is dedicated bcm2835 hardware SPI pin]
  • Vin –> 5V or 3V3 depending on minimum Vin of your Memory LCD model [can be changed]
  • GND –> GND [can be changed, multiple GND connections on the GPIO header]

Jumper settings (on rear of breakout board):

  • JP1 = HIGH for hardware control, or LOW for software control (see above)
  • JP2 = Set according to your model of LCD

Software Installation

Download the zip file from GitHub and extract it a directory of your choice within your HOME directory using the following commands:

wget https://github.com/MakerDyne/Memory-LCD-for-Raspberry-Pi/archive/master.zip
unzip master.zip

alternatively, if you are a user of the version control softare, git, you can accomplish the same thing with the following single command

git clone https://github.com/MakerDyne/Memory-LCD-for-Raspberry-Pi.github

and you should end up with the following files on your Pi:


Now, in addition to the Memory LCD library for Raspberry Pi from my GitHub page, it is essential to install the C library for the Pi’s Broadcom BCM 2835 chip. This library allows direct control of the GPIO pins. The library’s home is here and the all important documentation and API can be read here.

Use the following commands to download and install it (NB: version number may change, check website for latest version before entering these commands):

cd ~
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.38.tar.gz
tar zxvf bcm2835-1.38.tar.gz
cd bcm2835-1.38
sudo make check
sudo make install

Now, before you compile and run the Memory LCD demo program, you must first open the MemoryLCD.h file and edit the LCDWIDTH and LCDHEIGHT defines such that they match the pixel dimensions of your particular LCD:

#define LCDWIDTH 96
#define LCDHEIGHT 96

must be changed to match the dimensions of your particular model of Memory LCD in pixels. The boards I am selling are either (WxH) 96×96 pixels, 128×128 pixels, or 400×240 pixels.

If everything has gone to plan, change to the demo directory within the Memory-LCD-for-Raspberry-Pi-master/ directory and compile, link and run the demo program

cd ~/Memory-LCD-for-Raspberry-Pi-master/examples/demo/
g++ -c -g -Wall -funsigned-char demo.cpp ../../MemoryLCD.cpp
g++ -o demo demo.o MemoryLCD.o -l bcm2835 -l pthread
sudo ./demo

If there were no compile or linking errors, and the pi is connected to the Memory LCD breakout board as described above, you should see the demo start with a side scrolling sinewave.

Note the use of sudo to run the demo – any program wanting access to the GPIOs must run as root.

I have made a short video to demonstrate installing the software and using the Memory LCD with a Raspberry Pi

Compatibility with the Raspberry Pi 2 has not been verified as I do not own one yet. However, the BCM2835 library currently has a beta version that promises compatibility.

One thought on “Raspberry Pi Library for Sharp Memory LCDs

  1. Do you know if your breakout board(s) would support a Sharp Lq043t3dx03a LCD display? I’ve got one that I’m trying to find the appropriate breakout board for..


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s