Connecting ili9341 SPI TouchScreen LCD to a Raspberry Pi in Python

For many Raspberry Pi projects, it may be convenient to have a small display to show valuable information. Connecting to a big monitor can be a bit of an overkill specially if you want to keep your project compact. You can either use a character LCD but you are limited to characters. A TFT LCD can be the next best thing. In this post, we are going to show you how to connect the ili9341 SPI TouchScreen LCD to a Raspberry Pi in Python.
ili9341 TouchScreen Raspberry Pi

Please bear in mind that we are not showing how to display the full Raspbian GUI on the LCD but rather running a Python program and display on the LCD while the program is running!

To get started, you need an SPI LCD screen. We got ours from eBay for only £5.69. Make sure that you get the SPI version and not the 8-bit parallel one. A comparison between the SPI and the 8-bit Parallel one is shown below:

ili9341 types comparison

We used a Raspberry Pi Zero W in this tutorial. Install the latest Raspbian and power the Pi with SSH and Wifi enabled.

ili9341 SPI Raspberry Pi Wiring (Display Only)

For a start, connect the ili9341 SPI TouchScreen LCD to a Raspberry Pi as follows just to get the display:

ili9341 SPI Raspberry Pi connections

ili9341 Pin Raspberry Pi Pin
Vcc 3V3 Vcc
GND GND
CS GPIO8 (CE0)(26)
Reset GPIO25
DC GPIO24
SDI(MOSI) MOSI (SPI0)
SCLK SPI0 SCLK
LED GPIO15
SDO(MISO) SPI0 MISO

ili9341 SPI Python Driver

The datasheet for the ili9341 SPI LCD is readily available and you are welcome to use it to create your python driver. In our case, we did not have the time to do, so we cheated a bit. We used the driver written by Brian Lavery which can be downloaded on his github.

Adafruit also has a python driver but we did not try it. If you do try it, please do let us know how you got on.

Prerequisites

To be able to use the python driver, there is a few prerequisites that you need to install on your Pi. Through SSH, install the following:

Enable SPI through the raspi-config

sudo raspi-config

Reboot your Pi

Install SPIDEV

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python-dev python3-dev
cd ~
git clone https://github.com/doceme/py-spidev.git
cd py-spidev
make
sudo make install

Install pip

sudo apt-get install python-pip

Install PIL Pillow

sudo pip install Pillow

ili9341 Python Display Demo

Once you have wired the ili9341 LCD to the Pi and have install all the prerequisites, try to run the following code. Extract the archive in your user space on the Pi and run the following command:
Download Archive Demo from here.

python BTS_SPI_LCD_DEMO.py

If you get an error, check if you have successfully installed the prerequisites. Try searching for the errors you get; Google is your friend 🙂

If it runs successfully, you will see this on your SSH terminal:

and you should see this on the ili9341 SPI TouchScreen LCD:
ili9341 Raspberry Pi Display Demo

ili9341 Python TouchScreen Demo

Now that you have a working display, it is time to get the touchscreen to work. First of all finish wiring the ili9341 SPI TouchScreen LCD to the Raspberry Pi according to the following table:

ili9341 Pin Raspberry Pi Pin
T_clk SPI1 SCLK
T_cs SPI1 CE0(GPIO18)
T_DIN SPI1 MOSI
T_DO SPI1 MISO
T_IRQ GPIO26

The Raspberry Pi has 2 SPI Bus, SPI0 and SPI1. SPI0 can support 2 devices so we can also connect the touch part to SPI0. However, to make this simple, we are using jumper cables, and it is not easy to connect two jumper cables to the same SPI_SCLK pin without getting our soldering iron out! So we decided to connect the Touch part to SPI1.

Enable SPI1 on the Raspberry Pi

SPI1 is disabled by default and to enable it, enter the following in your /boot/config.txt

#enable spi1 with a single CS line
dtoverlay=spi1-1cs

This will only enable one CS line; you can have up to 3 on the SPI1 bus. For options see /boot/overlays/README

Reboot your Pi and check whether you see SPI1 in /dev/

You should see:

/dev/spidev0.0
/dev/spidev0.1
/dev/spidev1.0

Brian Lavery code works with only SPI0 and not SPI1. To use the ili9341 according to how we have shown, download our modified code here.

Brian Lavery has also written some calibration code to help getting the exact postion of where you have your stylus down.

Calibrating your ili9341 TouchScreen

Now that you have wired your ili9341 SPI TouchScreen LCD to a Raspberry Pi, you need to calibrate it. Run the following through SSH.

python BTS-ili9341-touch-calibration.py

Follow the instructions on your SSH terminal and you will need your stylus pen for this 🙂

Once done, you will presented with some calibration figures, make a note of them. You will need to enter them in the lib_tft24T.py file.

Conclusion

Now you have your screen ready to run your python scripts! The projects you can do with this is endless. We will post one or two projects that uses the ili9341 SPI TouchScreen LCD with the Raspberry Pi in the coming weeks, so do come back 🙂

Finally, for just a bit of fun, run:

python BTS-touch-penprint-trail.py

for a pen trail demo of the TouchScreen.

ili9341 TouchScreen

For any comments or questions, post down here or contact us at contact@behindthesciences.com

    • behindthesciences

Add a Comment

Your email address will not be published. Required fields are marked *