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.
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:
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 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.
For any comments or questions, post down here or contact us at contact@18.169.179.26
Hello,
thanks for the description.
How can i use the Display at CE1 ? ( CE0 is already blocked by a can module)
Thanks
Tim
If you look in the lib_tft24T.py, there is a def initLCD where ce=0. To use CE1, change that to ce=1.
Hope this answers your question.
Hi!
Good job, but landscape version does not work.
It looks like it counts wrongly pixels in lines. even clear() method do not fill screen properly. You get black pixel lines amid background color.
Thank you very much for this example!! Worked great for me!
– the wiring drawing has an error, connecting to GPIO 18 instead of GPIO 15 as given in the table
– py-spidev is available from the raspbian repository as python-spidev and python3-spidev
– same is true for Pillow – just apt-get install python-pil python3-pil
– Your example code works great using python3 when fixing some parentheses errors, tab/space confusion etc. and changing the types.methodtype call by removing the third argument.
Thanks a lot for this head start on using the spi display!
Thanks for your feedback!
Glad you found the post useful 🙂
I see the dpi pins but not the other screen which I have . can I have a diagram of this please of the 8but paralel
This post is for SPI TFT screen and not the the parallel one..
Check out http://18.169.179.26/electronics/arduino-tft-lcd-touch-screen-tutorial/
This is for an Arduino but similar principle and code can be written for the Pi.
Thanks a stack
Worked for me too
luckly I read the comment provided by BEN
and moved wiring from 18 to 15 as mentioned then every thing worked for me
Hurrah !
Perfect
It works.
Do you know if some library can manage to play a video , or gif file ?
Thanks A lot