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.
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:
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:
||Raspberry Pi Pin
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.
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
Reboot your Pi
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python-dev python3-dev
git clone https://github.com/doceme/py-spidev.git
sudo make install
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.
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 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:
||Raspberry Pi Pin
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
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:
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.
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.
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:
for a pen trail demo of the TouchScreen.
For any comments or questions, post down here or contact us at firstname.lastname@example.org