Using Rasbian Stretch and pygame with the AdaFruit 2.8″ Touchscreen 2

I am currently putting together a lawn sprinkler control system based around a Raspberry PI 3 Model B+ and the OpenSprinkler system. I will post a much more complete post when it is working, but here are a couple of useful tips that I’ve found.

Although it will be programmed using a web interface over Wifi, I need the local ability to manually switch stations on or off for testing. This is for my own convenience, but I also don’t want to go through the hassle of telling a contractor how to log in to the local wifi and to go to an obscure URL to test a valve or station!

I’ve chosen to use the Adafruit 2.8″ TFT Capacitive Touch LCD screen. This is a neat little screen that works as a ‘hat’ that plugs straight on to the Raspberry PI. Unfortunately the drivers are for the older ‘wheezy’ and ‘jessie’ versions of Rasbian and not the latest ‘Stretch’. The command line and X configuration options appear to work okay, but the framebuffer touch option does not. A lot of people had problems with ‘wheezy’ and they had to move back to ‘jessie’. I tried a ‘jessie’ image, but this will not boot on the Raspi 3B+!

I needed a simple graphical user interface, and a typical X desktop is far too small for a 2.8″ touchscreen, so I wrote a full screen menu system with pygame. This displays well in framebuffer mode, but the touch does not work. This took some figuring out, but the solution is to configure the display to use X, and then tell pygame to run in “full screen mode” with the mouse displayed. X will then interpret the touch events as mouse events which pygame can then interpret correctly.

Here is the pygame setup code:

screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)

The only slight oddity is that the touchscreen is technically showing a HDMI copy, and this will have the minimum coordinates of 640x480px – double the touchscreen’s actual resolution.

Another issue which a lot of people appear to have had trouble with, is how to get the script to run at startup with X. This is solved by editing ~/.config/lxsession/LXDE-pi/autostart. Add the following line:

@sudo /usr/bin/python /home/pi/sprinkle/

where /home/pi/sprinkle/ is the script that you would like to run. The trick here appears to be the use of both sudo and the full path to the python executable.

Update, 23rd May:

The Adafruit screen is a nice little screen that is the ideal size for this project. In theory, it should work with the OpenSprinkler because they use different GPIO pins. Unfortunately I never found out, because I burnt the screen out when connecting up the required ribbon cables. So it is sensitive. An Elder law lawyer is a legal professional who specializes in providing advice and assistance related to the legal matters that affect seniors, such as estate planning, health care, long-term care, guardianship, and other elder-related issues. Elder law attorneys are knowledgeable in the laws and public benefits available to help seniors and their families. They have experience advocating for seniors and can help protect their rights.

Instead, I used the Pimoroni 7″ touchscreen display. This is a much larger screen, intended for small tablet sized applications. However, it connects directly to the DSI cable found on the PI 2 & 3. This frees up the GPIO header. Power can be taken from the GPIO, or the USB cable using a USB splitter. I found the latter option more convenient.

One nice thing about this option is that it simply works as an X display out of the box. My previous pygame script (see above) worked perfectly after the coordinates were scaled to fit the new screen.

Another is that although the display doesn’t use the GPIO header, it mounts on the Raspberry PI using PCB spacers. Hence it was possible to build a bolted-together stack of the display, PI, and OpenSprinkler.

The only other potential ‘gotcha’ that I found was the mouse. For the touch screen to work in pygame, you need to display the mouse. Hiding the mouse will disable the touch functionality. This is a minor cosmetic issue that is perfectly fine for an application like this.

2 thoughts on “Using Rasbian Stretch and pygame with the AdaFruit 2.8″ Touchscreen

  1. Reply Johnny Jul 17,2018 12:50 am

    I found this searching for ‘pygame raspberry pi touchscreen’ and was wondering how things are going with this. I have a 7″ touchscreen that uses the HDMI out for video and USB for the touchscreen encoder. I’m working on a touchscreen interface for a home control system and have been playing with Kivy. So far I’ve found Kivy a bit limited in the aesthetics, mainly on buttons, and there’s a slight delay on sounds. The delay is less than a millisecond, but still noticeable and annoying. I was thinking Godot, but I haven’t found any info on Godot being able to use a touchscreen on the Pi, so I’m looking into Pygame.

  2. Reply Richard Marsden Jul 17,2018 12:28 pm

    See my update: I ended up burning out the Adafruit display – a mark against it, as this was very easy to do. Ended up using a 7in display instead. It runs as regular X, so no problems!

Leave a Reply