Getting 1.8 Inch LCD Display ST7735 to Work With Raspberry Pi Zero W

by stratosb in Circuits > Raspberry Pi

219 Views, 0 Favorites, 0 Comments

Getting 1.8 Inch LCD Display ST7735 to Work With Raspberry Pi Zero W

P6070028.JPG

I always wanted to interface a lcd display with raspberry pi and see its possibilites.

I had a spare raspberry pi zero and was looking for an affordable lcd display. Fortunately I found what I was looking for at Aliexpress.

I bought an 1.8 inch LCD display 128x160 and managed to display images on it with the use of my raspberry pi zero w, running Raspbian OS bookworm version.

This is a guide to save you time and effort if you want to do the same or similar project.

Supplies

P6070030.JPG
P6070029.JPG
P6070028.JPG

Find Correct Wiring of the LCD Display to the Pi Zero W

P6070028.JPG
P6070032.JPG
P6070033.JPG

The first step was to find the correct wiring of the display to the raspberry pi zero.

Although there are several websites online for getting this display to work on the raspberry pi zero, I was not able to find the exact match. So after a lot of experimentation I was able to figure out the correct wiring:


Display PIN Pi zero PIN
LED GPIO 22 (pin 15)
SCK GPIO 11 SCLK (pin 23)
SDA GPIO 10 MOSI (pin 19)
A0 (DC) GPIO 24 (pin 18)
RESET GPIO 25 (pin 22)
CS GPIO 8 CE0 (pin 24)
GND Ground (pin 9)
VCC 3.3V (pin 1)

Setup Raspberry Pi Zero W

raspbian.jpg

First you need to install Raspbian OS on SD Card and update to the latest version. Luckily there is this guide provided by the Raspberry Pi Foundation which details all the required steps.

You will want to enable SSH so you can send commands to the pi zero from console without using a monitor (headless mode). Once again there is a helpful guide available provided by the Raspberry Pi Foundation.

You also need to enable SPI on your pi zero. The Serial Peripheral Interface (SPI) is a communication protocol used to transfer data between micro-computers like the raspberry pi and peripheral devices, like sensors and displays. You can do this by using the Raspberry Pi configuration tool:

sudo raspi-config

This will launch the raspi-config utility. Select “Interfacing Options”.

Highlight the “SPI” option and activate “<Select>”.

Select and activate “<Yes>”.

Install Necessary Software

Untitled.png

Now, you'll need to install the required dependencies by using the following commands:

sudo apt install python3-pip
sudo apt install python3-pil
sudo apt install python3-dev
sudo apt install python3-numpy
sudo apt istall python3-libgpiod

Next you need to install the Python library to control the ST7735 LCD display:

sudo pip3 install st7735

This command throws the following error:

error: externally-managed-environment
× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
   python3-xyz, where xyz is the package you are trying to
   install.
   If you wish to install a non-Debian-packaged Python package,
   create a virtual environment using python3 -m venv path/to/venv.
   Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
   sure you have python3-full installed.
   For more information visit http://rptl.io/venv

In order to overcome this error, use the following command:

sudo pip install st7735 --break-system-packages

In addition, you need to install the following libraries:

sudo pip install gpiod --break-system-packages
sudo pip install gpiodevice --break-system-packages

Modifications to the Python Library ST7735

The code of the python library ST7735 that has been installed in your raspberry pi zero exists at https://github.com/pimoroni/st7735-python

In the main file of the library, some values need to change because the resolution of the LCD display that is used is different to the resolution of our display 128x160.

Especially the values at line 40, line 41, line 43 and line 44 need to be adjusted.

So you need to edit the main file of the installed ST7735 library with the command:

sudo nano /usr/local/lib/python3.11/dist-packages/st7735/__init__.py

Inside this file, replace the above values with these ones:

ST7735_TFTWIDTH = 124
ST7735_TFTHEIGHT = 160

ST7735_COLS = 124
ST7735_ROWS = 160

Note: For some reason the value of 124, instead of 128, is needed for the best result.

Save the changes and you are ready for the next step.

Try the Example to Display an Image

Getting 1.8 Inch LCD Display st7735s to Work With Raspberry Pi Zero W

In order to test if the display works as expected, you need to download some example files in your raspberry pi zero:

git clone https://github.com/pimoroni/st7735-python.git
cd st7735-python/examples

You will try the file image.py, which displays an image to the LCD display.

However one change is needed before it works.

The code between the line 41 and line 48 need to be adjusted to your LCD display.

Edit the file in the raspberry pi zero:

sudo nano image.py

Replace the above lines with the following:

disp = st7735.ST7735(
  port=0,
  cs=st7735.BG_SPI_CS_BACK,
  dc="GPIO24",
  backlight="GPIO22",
  rst="GPIO25",
  rotation=90,
  invert=False,
  spi_speed_hz=4000000
)

Save changes and run the file:

python image.py cat.jpg

If all is setup correctly, you will see an image of a cat on your LCD display.

Try the Example to Display Scrolling Text

Displaying scrolling text on 1.8 inch LCD display st7735s with raspberry pi zero w

Now you can try the file scrolling-text.py, which displays a scrolling text on the display.

In the same way as before, you need to replace the code between the line 10 and line 17 in order to be adjusted to your LCD display.

Edit the file in the raspberry pi zero:

sudo nano scrolling-text.py.py

Replace the above lines with the following:

disp = st7735.ST7735(
  port=0,
  cs=st7735.BG_SPI_CS_BACK,
  dc="GPIO24",
  backlight="GPIO22",
  rst="GPIO25",
  rotation=90,
  invert=False,
  spi_speed_hz=4000000
)

Save changes and run the file:

python scrolling-text.py

If all is setup correctly, you will see a scrolling text on your LCD display.

Enjoy!

If you enjoyed this guide and helped you to your project, please favourite it and subscribe to my youtube channel for more projects like this. Thank you.