Skip to lesson content

Hardware & Sensors · Lesson 11 of 17

Displaying data on Automation Hat Mini LCD

In this example, we will cover how to write something on an LCD screen using Python. We're using something called an ST7735 display, and we are going to write 'Hello World' on it.

Install Pillow

To display data on our Automation Hat Mini, we will also need the Pillow library.

We can install it via

pip install Pillow

Set up the screen

So, the first thing we do is set up the ST7735 screen.

We do this by giving it various settings, like which port to use, how to control the backlight, and how fast it should talk to the Raspberry Pi. Once we've done that, we tell the screen to turn on.

import st7735

# Create ST7735 LCD display class.
disp = st7735.ST7735(
    port=0,
    cs=st7735.BG_SPI_CS_FRONT,
    dc=9,
    backlight=25,
    rotation=270,
    spi_speed_hz=4000000
)

# Initialize display.
disp.begin()

View source on GitHub

Create an image

After the screen's on, we need something to display. We're going to use a handy Python tool called Pillow. With Pillow, we make a pretend 'screen' in the program that we can draw on. This pretend screen is called an `Image`. So, we create this Image and make it all black - think of it as like the blank screen on an old television.

from PIL import Image, ImageDraw

# Initialize display.
disp.begin()
 
WIDTH = disp.width
HEIGHT = disp.height
img = Image.new('RGB', (WIDTH, HEIGHT), color=(0, 0, 0))
draw = ImageDraw.Draw(img)
 
disp.display(img)

View source on GitHub

Draw and display the message

Next, we need to prepare the image for our content. We do this using a utility from the Pillow library named `draw`. With this, we set the initial state of our image to be entirely black, effectively ensuring that the screen is clear. After clearing the screen, we use `draw.text()` to write 'Hello World' on our prepped image in white color.

At this point, we have a black Image with 'Hello World' written in white, but it's still just in our program. The final step is to show this Image on the actual ST7735 screen. We do this with `disp.display(img)` which takes the Image from our program and puts it on the ST7735 display for everyone to see.

# Now to display some text on the screen we can use the code below
draw.rectangle((0, 0, WIDTH, HEIGHT), (0, 0, 0))  # Used to clear the screen
draw.text((10, 10), 'Hello World', fill=(255, 255, 255))  # Draw "Hello World" 
disp.display(img)  # Display the new image with the text on the screen.

View source on GitHub

Full Code Example

from PIL import Image, ImageDraw
import st7735

# Create ST7735 LCD display class.
disp = st7735.ST7735(
    port=0,
    cs=st7735.BG_SPI_CS_FRONT,
    dc=9,
    backlight=25,
    rotation=270,
    spi_speed_hz=4000000
)

# Initialize display.
disp.begin()

# Initialise display Width and Height defaults
WIDTH = disp.width 
HEIGHT = disp.height

# We create an Image using the Pillow library
# https://www.rapidtables.com/web/color/RGB_Color.html
img = Image.new('RGB', (WIDTH, HEIGHT), color=(0, 0, 0))
draw = ImageDraw.Draw(img)

disp.display(img)

# Now to display some text on the screen we can use the code below
draw.rectangle((0, 0, WIDTH, HEIGHT), (0, 0, 0))  # Used to clear the screen
draw.text((10, 10), 'Hello World', fill=(255, 255, 255))  # Draw "Hello World" 
disp.display(img)  # Display the new image with the text on the screen.

View source on GitHub