Installing The Touchscreen

I'm using an Adafruit 2.8" Touchscreen LCD. It comes connected with header pins that make it easy to fit right on top of the Raspberry Pi GPIO Pins.

Adafruit has some great documentation about setting up the Raspberry Pi, and they even have their own Raspbian Image that you can download and install with everything already on it. Sadly, the pre-made image didn't work for me, which is why I installed a fresh version of Raspbian.

The LCD requires a Kernel in order for it to work, so I'll need to download it manually from the Adafruit website (these steps are also on their instruction page).

Download: file
sudo apt-get update
curl -SLs https://apt.adafruit.com/add-pin | sudo bash
sudo apt-get install raspberrypi-bootloader 

Installing the bootloader will take a few minutes, but when it's done, you can shutdown your Pi, and unplug any other external monitors before turning it back on.

Once the Pi boots back up, we'll need to modify the Pi's boot config file and adjust it so that it displays to the LCD properly.

Download: file
sudo nano /boot/config.txt

and then add this text to the bottom of it:

Download: file
dtparam=i2c_arm=on 
dtparam=i2s=on 
dtparam=spi=on 
dtoverlay=pitft28-resistive,rotate=90,speed=32000000,fps=20 

In order to get anything to display on the LCD, we need to tell the operating system to use it as it's primary display. To do this, you can use these commands:

Download: file
export FRAMEBUFFER=/dev/fb1 
startx 

Now we can test it by displaying an image on it using the fbi (not that F.B.I.) image viewer.

Download: file
sudo apt-get install fbi
wget http://www.tinkernut.com/wp-content/uploads/2015/01/logo_gear_sm3.png -O image.png
sudo fbi -T 2 -d /dev/fb1 -noverbose -a image.png

The LCD part works! Now we need to test the touch screen. By default, the touchscreen should work with the Adafruit kernel. But there are a few commands you can run to help calibrate it.

Download: file
sudo TSLIB_FBDEVICE=/dev/fb1 TSLIB_TSDEVICE=/dev/input/touchscreen ts_calibrate
sudo TSLIB_FBDEVICE=/dev/fb1 TSLIB_TSDEVICE=/dev/input/touchscreen ts_test

These commands will give you visual feedback on your screen that you can touch to calibrate your screen.

Fixing Issues With The Adafruit Touchscreen

If you are using the Adafruit PiTFT for this project, you may find that no matter how much you calibrate the LCD, it still won't work with your Pygame code. This is because there is an issue with the Adafruit touchscreens and the current version of Raspbbian.

The touchscreens work well with the Raspbian "Wheezy" distro, but they don't play well with Raspbian "Jessie". There is a workaround, however, to get it functional again. It involves downloading and installing the "Wheezy" driver.

Download: file
#enable wheezy package sources
echo "deb http://archive.raspbian.org/raspbian wheezy main
" >> /etc/apt/sources.list.d/wheezy.list
#set stable as default package source (currently jessie)
echo "APT::Default-release \"stable\";
" >> /etc/apt/apt.conf.d/10defaultRelease
#set the priority for libsdl from wheezy higher then the jessie package
echo "Package: libsdl1.2debian
Pin: release n=jessie
Pin-Priority: -10
Package: libsdl1.2debian
Pin: release n=wheezy
Pin-Priority: 900
" >> /etc/apt/preferences.d/libsdl
#install
apt-get update
apt-get -y --force-yes install libsdl1.2debian/wheezy

Running the above code should fix the issue and the touchscreen will now work with Pygame. So let's go ahead and create our own test script that sets a background, creates a button, and checks for touch input to perform an action.

Below is some test code called pygame_test.py

Download: file
import pygame 
import os 
from time import sleep 
import random 
os.environ['SDL_FBDEV']= '/dev/fb1' 
os.environ["SDL_MOUSEDEV"] = '/dev/input/touchscreen' 
os.environ['SDL_MOUSEDRV'] = 'TSLIB' 
pygame.init() 
lcd = pygame.display.set_mode((320,240)) 
lcd.fill((255,0,0)) 
def make_button(text, xpo, ypo, color): 
       font=pygame.font.Font(None,24) 
       label=font.render(str(text),1,(color)) 
       lcd.blit(label,(xpo,ypo)) 
       pygame.draw.rect(lcd, cream, (xpo-5,ypo-5,110,35),1) 
def random_color(): 
   rgbl=[255,0,0] 
   random.shuffle(rgbl) 
   return tuple(rgbl) 
blue = 26, 0, 255 
white = 255, 255, 255 
cream = 254, 255, 250 
lcd.fill(blue) 
pygame.mouse.set_visible(False) 
while 1: 
       make_button("Menu item 1", 20, 20, white) 
       for event in pygame.event.get(): 
               if (event.type == pygame.MOUSEBUTTONDOWN): 
                       print "screen pressed" 
                       lcd.fill(random_color()) 
                       pos = (pygame.mouse.get_pos()[0],pygame.mouse.get_pos()[1]) 
                       print pos 
       pygame.display.update() 
This guide was first published on Sep 15, 2017. It was last updated on Sep 15, 2017. This page (Installing The Touchscreen) was last updated on Jul 15, 2019.