Nokia5110 Display Interfacing with Raspberry Pi

Nokia5110 Display Interfacing with Raspberry Pi

Introduction

Nokia5110 Display
Nokia5110 Display Module
Nokia5110 is a graphical display that can display text, images and various patterns.
It has a resolution of 48x84 and comes with a backlight.
It uses SPI communication to communicate with a microcontroller/microprocessor.
Data and commands can be sent through processor to the display to control the display output.
It has 8 pins.
For more information about Nokia5110 display and how to use it, refer the topic Nokia5110 Graphical Display in the sensors and modules section.
As Nokia5110 uses SPI communication, we need to make sure that SPI interface of Raspberry Pi is enabled. If it is not enabled then using we have to make it enable.
How to enable SPI?
To enable SPI on Raspberry Pi, enter following command
sudo raspi-config
then, a window will pop-up in which select  Interface Options
After selecting Interface option, enable SPI interface. And then reboot Raspberry Pi.
Now, we can establish SPI communication.

Interfacing Diagram

Nokia display Interface with Raspberry Pi
Nokia LCD Display Interface with Raspberry Pi 3

Example

Here, we are going to interface Nokia5110 display with Raspberry Pi 3
We will be using theNokia5110 Python library by Adafruit from GitHub.
Extract the above library and install it in the directory of above library by executing following command,
sudo python setup.py install
Also, we need to install python imaging library as given below,
sudo apt-get install python-imaging
Once the library has been added, we can test the Nokia LCD interfacing with Raspberry Pi by executing existing examples kept in examples folder of library.
Here, we are going to create a small python program in which we will display text, image and flying bird animation. This python program file along with sample images is kept in “example” folder of library provided in attachment.
Note: While displaying image on Nokia5110 display, we have to provide image to the program. This image should be of display size (84x48 here). If we provide image with other resolution it will give error.

Python Program

'''
 Nokia5110 LCD Display Interface with Raspberry Pi
 http://www.electronicwings.com
'''
import time
import Adafruit_Nokia_LCD as LCD
import Adafruit_GPIO.SPI as SPI

from PIL import ImageDraw
from PIL import Image
from PIL import ImageFont


# Raspberry Pi hardware SPI config:
DC = 23
RST = 24
SPI_PORT = 0
SPI_DEVICE = 0


# Hardware SPI usage:
disp = LCD.PCD8544(DC, RST, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=4000000))

# Initialize library.
disp.begin(contrast=40)

# Clear display.
disp.clear()
disp.display()

font = ImageFont.load_default()

# Load image and convert to 1 bit color.
image = Image.new('1', (LCD.LCDWIDTH, LCD.LCDHEIGHT))
draw = ImageDraw.Draw(image)
draw.rectangle((0,0,LCD.LCDWIDTH,LCD.LCDHEIGHT), outline=255, fill=255)
draw.text((3,24), 'Welcome to EW', font=font)

#Display Image
disp.image(image)
disp.display()
time.sleep(1.0)

image = Image.open('pi_logo.png').convert('1')

# Display image.
disp.image(image)
disp.display()
time.sleep(0.5)

print('Press Ctrl-C to quit.')
while True:
    
#Create animation of Flying Bird
    
    image2 = Image.open('bird_1.png').convert('1')
    # Display image.
    disp.image(image2)
    disp.display()
    time.sleep(0.1)
    
    image2 = Image.open('bird_2.png').convert('1')
    # Display image.
    disp.image(image2)
    disp.display()
    time.sleep(0.1)

    image2 = Image.open('bird_3.png').convert('1')
    # Display image.
    disp.image(image2)
    disp.display()
    time.sleep(0.1)

    image2 = Image.open('bird_4.png').convert('1')
    # Display image.
    disp.image(image2)
    disp.display()
    time.sleep(0.1)

    image2 = Image.open('bird_5.png').convert('1')
    # Display image.
    disp.image(image2)
    disp.display()

    image2 = Image.open('bird_4.png').convert('1')
    # Display image.
    disp.image(image2)
    disp.display()
    time.sleep(0.1)

    image2 = Image.open('bird_3.png').convert('1')
    # Display image.
    disp.image(image2)
    disp.display()
    time.sleep(0.1)

    image2 = Image.open('bird_2.png').convert('1')
    # Display image.
    disp.image(image2)
    disp.display()    
    time.sleep(0.1)




No comments:

Post a Comment