Using a Small TFT with a Raspberry Pi

Introduction

As I have said before part of the point of this blog is so I have a record of how I did things and this definitely fits into that category.  If others find it useful, that is a bonus.

I have had a small 2.4″ TFT with touch sitting around for ages.  I looked at using it when it arrived, but compling a kernel put me right off.  So this week the notion took me to try again and I discovered it was actually very easy with newer versions of Raspbian.

[Edit 16 Dec 17] I had made an error in the table below putting both clocks on GPIO08. I have had some PCBs made up to simplify the connection – the error blew the TFT I was using sadly.

Sources

There are a couple of key sources I used and in places I have blatantly plagiarized them (they are that good) so as to bring it all together, and in case the source disappears (it happens).

  1. Wiring and OS tasks
  2. tslib info
  3. fbTFT info
  4. getting started with pygame
  5. More on pygame
  6. Fixing Coordinates on Jessie

Background

The background detail, found in the first reference, is interesting. It seems the old notro type method is being phased out in preference for overlays.  Worth reading.

Wiring

I was fortunate in that my board was very similar to the board in reference 1 so I could use those mappings for my wiring.  If you have a different board then head over there and it is explained how to work out the wiring for your specific overlay.

Panel Pin Function name GPIO Pin No.
T_IRQ Panel Touch Interrupt GPIO25 22
T_DO Touch Data Out GPIO09 21
T_DIN Touch Data In GPIO10 19
T_CS Touch Chip Select SPI_CE1_N GPIO07 26
T_CLK Touch Clock GPIO11 23
SDO (MISO) TFT Data Out SPI_MISO GPIO09 21
LED Backlight GPIO18– see note 12
SCK TFT Clock SPI_CLK GPIO11 23
SDI (MOSI) TFT Data In SPI_MOSI GPIO10 19
D/C TFT Data / Command GPIO24 18
RESET Reset GPIO23 16
CS TFT Chip Select SPI_CE0_N GPIO08 24
GND Ground GND
VCC 3V3 supply
+3V3

I did the wiring using a breadboard as a couple of the pins need the same GPIO pin.  Not pretty but functional!

Note the original sources comment on wiring up the LED Backlight.  For now I have just connected to the 3.3V.

Setting up the OS

As an aside, although I did this initially on a clean install of Raspbian Jessie, I then moved to DietPi. The main reason for this is that you can create a configuration file for such things as WiFi SSID & Password and a fixed IP address.  I could then do everything headless from the go.

The OS changes themselves are simplicity itself.

Edit the file /boot/config.txt (on DietPi you need to edit /DietPi/config.txt) and include the following lines;

And reboot the Pi. That’s it.  If it is OK, the screen will light up during boot, then go blank.

Next step is to check and calibrate the touchscreen.  I deviate a bit from my main source as there were 2 extra packages to install (references 2 & 3).

I found I needed to do the calibration before I did the test so (remembering to change the event number and fb if necessary);

and then

If all is well, you can drag the cross hairs or draw in the screen.

You now need to check the touch input. to do this;

and then run evtest  If you are running headless, the touch input is likely to be event0, if you are not, again head over to the first reference and see how to interpret the contents of  /dev/events/

The AdaFruit tutorial on this has some instructions for creating a udev rule to map the touch screen events to a fixed point.  I have not managed to get this to work and I suspect it is because I am using overlays.

pygame and TSLIB problems with Jessie

I won’t bore you with how I got here, suffice to say I realised that the coordinates of the touch input were not correctly reported by the OS to pygame. After a bit of digging I found reference 6.  In summary, to fix the issue create and run the following script;

This fixed the coordinates for me.

Python and TFT

The aim of all this is to have a small touchscreen that I can use to provide simple control of my heating and hot water – basically a boost function.  I’ll blog about that when I get a bit further.  In the meantime, the following code will test out the touch responses.

 

Edit 5 June 2017

I have now sorted out some more useful code to display buttons which you will find here.

HTH

 

 

Facebooktwitterby feather
This entry was posted in Hardware, Linux, RaspberryPi and tagged , , . Bookmark the permalink.

Leave a Reply

Your e-mail address will not be published.

*