Today we are going to see how to interface Nokia 5110 LCD and Raspberry Pi using Python. It is controlled using SPI protocol and the display uses PCD8544 as its controller.
Nobody can forget the most sturdiest robust and reliable phone of the 90s, Yes I am talking about Nokia Phones :). Somehow it has got its place in the history and heart of people. I too have one as a vintage collection. I recently got an idea which I wanted to make and for that I wanted an LCD display. After searching around I got a LCD display what was used in Nokia phones. LCD is called Liquid crystal display and the difference between LCD and OLED has been explained a bit in my previous article.
The display is not actually from Nokia and it uses internally PCF8544 controller which uses SPI interface for operating it.
Pin Connections
1. RST ——— reset.
2. CE ———— chip selection.
3. DC ———– data/commands choice.
4. DIN ———– serial data line.
5. CLK ———— serial Clock Speed.
6. VCC ———- Power, 3.3V (Positive).
7. BL ——— backlight control terminal.
8. GND ———– power negatively.
Hardware required:
Banggood: https://goo.gl/wrcUS3
Amazon US: http://amzn.to/2Dw3oKT
Amazon Germany : http://amzn.to/2FUW5Sj
Banggood: Nokia LCD
Amazon US: https://amzn.to/2I7KS2q
Amazon Germany: https://amzn.to/2rynakS
Banggood: https://goo.gl/E93MCh
Amazon US: http://amzn.to/2FISFmg
Amazon Germany: http://amzn.to/2pkj27N
The LCD should be connected as shown the in the below picture.
The display is connected to the SPI pins of the Raspberry Pi . If you want to know more about the Raspberry Pi pinout refer to http://pinout.xyz/ . The pin placement order is shown the same way the board is placed in the below picture. The VCC is connected to 3.3V supply since the LCD and driver require a 3.3V power supply and 3.3V logic levels for communication.
1. RST——— reset. https://pinout.xyz/pinout/pin16_gpio23
2. CE————chip selection. https://pinout.xyz/pinout/pin24_gpio8
3. DC———–data/commands choice. https://pinout.xyz/pinout/pin21_gpio9
4. DIN———–serial data line. https://pinout.xyz/pinout/pin19_gpio10
5. CLK————serial Clock Speed. https://pinout.xyz/pinout/pin23_gpio11
6. VCC———-Power, 3.3V (Positive). https://pinout.xyz/pinout/pin1_3v3_power
7. LIGHT——— backlight control terminal. https://pinout.xyz/pinout/pin15_gpio22
8. GND———–power negatively. https://pinout.xyz/pinout/ground
After setting up the connection the circuit looks like shown below.
SPI Driver:
Before you start with the programming part, you need to enable the spi driver in your raspbian distribution. It can be done using raspi-config. You can do it as follows.
sudo raspi-config
Ensure that you see the SPI device nodes as shown below.
$ ls -l /dev/spi* crw-rw---- 1 root spi 153, 0 Nov 25 08:32 /dev/spidev0.0 crw-rw---- 1 root spi 153, 1 Nov 25 08:32 /dev/spidev0.1
With the emergence of Pi board and the Linux distros along with it there are always more than one library available for each interface. Likewise for LCD display there are many libraries and many variants of libraries . For the LCD with PCD8544 I choose this library https://github.com/rm-hull/luma.lcd .
This is library is already available as part of python repository or as they called cheese cake factory ;). Type the below command to install all the required dependencies for using the LCD display.
$sudo apt-get install python-dev python-pip libfreetype6-dev libjpeg-dev build-essential $sudo -H pip install --upgrade luma.lcd
Now during the installation if you get any error like shown below
NameError: name 'platform_system' is not defined
Then type the below command and then try again to install the above packages.
sudo pip install -U pip
For the latest information about the installation always refer to https://luma-lcd.readthedocs.io/en/latest/install.html
Example Code
Turning backlight on
Output:
Explanation:
from luma.core.interface.serial import spi
Since the hardware uses spi I am importing the spi handler related module from luma.core.interface.serial
from luma.core.render import canvas
Assume the canvas like a buffer which will be used to transfer the content to display controller.
from luma.lcd.device import pcd8544, st7735, uc1701x
Here I import the display hardware related module which can speak with pcd8544 controller.
from luma.lcd.aux import backlight
Importing backlight functionality from LCD auxilary library.
serial = spi(port=0, device=0, gpio_DC=23, gpio_RST=24)
We specifiy which SPI bus and the Device number to the display controller object and get a device handler as return. port represents the /dev/spidev0.0, in case if the device is /dev/spidev2.0 then port will be 2. device represents which device for example spidev0.0 0 is the device number and spidev0.1 has 1 as device number. Additionally the Data command pin and reset pins needs to be configured seperately using the variables gpio_DC and gpio_RST.
channel=18
light = backlight(gpio_LIGHT=channel, active_low=False)
light.enable(True)
Here the GPIO pin that is connected to the back light is defined and it is passed to the backlight auxilary function along with the logic level of the pin as active high.
Output:
Explanation:
device = pcd8544(serial,rotate=2)
This creates the device object of pcd8544 controller, the option rotate is to turn the orientation and it has 4 numbers 0,1,2 and 3.
with canvas(device) as draw:
Open a canvas for this device with the name draw.
draw.rectangle(device.bounding_box, outline=”white”, fill=”black”)
Draw a rectangle with device.bounding_box which outlines the border.
draw.text((10, 10), “Hello World”, fill=”red”)
Print the text Hello World and the fill has no meaning here except in generate it can be either 0 or 255.
raw_input(“Here”)
Output:
Explanation:
draw.point((5,9),fill=255)
If you want to plot a pixel then use the point method with X and Y co ordinate to plot.
Output:
Explanation:
draw.line((0,0 , 20 , 20 ), fill=“white“)
Here I use the method line with start co ordinates (X1,Y1) and end co ordinates (X2, Y2)
Output:
Explanation:
font = ImageFont.truetype(‘./ssd1306/fonts/Volter__28Goldfish_29.ttf‘,20)
Here I loading the font to the variable font using the ImageFont.truetype method. Here I am using the font Volter__28Goldfish_29.ttf but you can use any ttf font with the complete path. The number 20 is the font size you can play around reducing and increasing to see its effect on the screen,
draw.text((0, 0), “Hello World“, font=font, fill=255)
The font object is used as the font to display the text “Hello World”.
$git clone https://github.com/rm-hull/luma.examples $cd luma.examples/examples
You can run most of the example using the following option.
python examples/pi_logo.py -f conf/pcd8544.conf python examples//animated_gif.py -f conf/pcd8544.conf python examples/clock.py -f conf/pcd8544.conf
The post Interface Nokia 5110 LCD and Raspberry Pi – Python appeared first on Codelectron.