Overview
Is this not the cutest little display for the Raspberry Pi? It features a 2.8β³ display with 320Γ240 16-bit color pixels and a resistive touch overlay. The plate uses the high speed SPI interface on the Pi and can use the mini display as a console, X window port, displaying images or video etc. Best of all it plugs right in on top!
It's designed to fit nicely onto the Pi Model A or B but also works perfectly fine with the Pi 2 or Model B+ as long as you don't mind the PCB overhangs the USB ports by 5mm
This design uses the hardware SPI pins (SCK, MOSI, MISO, CE0, CE1) as well as GPIO #25 and #24. All other GPIO are unused. Since we had a tiny bit of space, there's 4 spots for optional slim tactile switches wired to four GPIOs, that you can use if you want to make a basic user interface. For example, you can use one as a power on/off button.
We bring out GPIO #23, #22, #21, and #18 to the four switch locations!
Assembly
Easy Install
The PiTFT requires kernel support and a couple other things to make it a nice stand-alone display. We have a detailed step-by-step setup for hackers who want to tweak, customize or understand the PiTFT setup. If you just want to get going, check out the following for easy-install instructions!
Ready to go image
If you want to start with a fresh image, we have one for Raspbian β click here to download it and install into a new SD card. Unzip and follow the classic SD card burning tutorial
Previous images:
DIY Installer script
If you don't want to download an image, you can run our installation package helper from inside your existing Raspbian install. It will download the kernel add-ons, and configure your Pi for PiTFT joy
The helper is available for perusal here if you are interested in how it works
To download and run it, simply run the following commands:
- curl βSLs https://apt.adafruit.com/add | sudo bash
- sudo aptβget install βy adafruitβpitftβhelper
The first command adds apt.adafruit.com to your repository list, so you can grab code directly from adafruit's servers
The second does the actual download and installation, it'll take a while because there's a lot of software to replace for PiTFT support
OK now the kernel is installed, all you have to do is run the helper which will configure the kernel device tree overlays and add the few configurations to make the console show up, etc.
- sudo adafruitβpitftβhelper βt 28r
This will install the β2.8 Resistiveβ type of PiTFT into the current install. This is the same as the 2.4β³ Resistive screen too (same resolution, pinout, etc.)
At the end you will be prompted on whether you want the text console to appear on the PiTFT. Answer Y or N depending on your personal desires!
You will also be prompted on whether you want one of the tactile buttons to act as an βon off' switch. Answer Y or N depending on your personal desires!
Thats it!
Run sudo reboot to try out your fancy new PiTFT π
Detailed Installation
In the next few steps we'll cover the detailed installation procedure. Chances are, you should grab the Easy Install image or script. If you have some interest in the details of how we install the PiTFT setup, read on!
In order to add support for the 2.4β³ or 2.8β³ TFT and touchscreen, we'll need to install a new Linux Kernel. Lucky for you, we created a kernel package that you can simply install over your current Raspbian (or Raspbian-derived) install instead of needing a whole new image. This makes it easier to keep your install up-to-date.
To use our kernel .deb files you must be using Raspbian or derivative. This wont work with Arch or other Linux flavors. As Raspbian is the official OS for the Pi, that's the only Linux we will support! Others can recompile their own kernel using our patchfile, but we have no tutorial or support or plans for such.
Before you start
You'll need a working install of Raspbian with network access. If you need help getting that far, check out our collection of Pi tutorials.
We'll be doing this from a console cable connection, but you can just as easily do it from the direct HDMI/TV console or by SSH'ing in. Whatever gets you to a shell will work!
Also, run sudo apt-get update !
Download & Install Kernel
The only way we're distributing the PiTFT kernel packages right now is thru apt.adafruit.com so you'll still need to run:
- curl βSLs https://apt.adafruit.com/add | sudo bash
To add apt.adafruit.com to your list of software sources
hen install the kernel with
- sudo aptβget install βy adafruitβpitftβhelper
This will take a up to 20 minutes so go make a sandwich or coffee. It takes longer than it used to because there's now 2 kernels (v6 and v7 arm) and 2 kernel module directories.
The rotate= variable tells the driver to rotate the screen 0 90 180 or 270 degrees.
0 is portrait, with the bottom near theUSB jacks
90 is landscape, with the bottom of the screen near the headphone jack
180 is portrait, with the top near the USB jacks
270 is landscape, with the top of the screen near the headphone jack.
You can change this file with nano and reboot to make the change stick.
The speed= variable tells the driver how to fast to drive the display. 32MHz (32000000) is a good place to start but if your screen is acting funny, try taking it down to 16MHz (16000000) especially if you're doing something like using a GPIO extender to put the screen away from the Pi.
Save the file. Now we'll just reboot to let it all sink in.
sudo shutdown -h now (if you don't have the TFT installed, shutdown, place the TFT on the Pi and re-power)
or
sudo reboot (if you have the TFT plate installed already)
When the Pi restarts, the attached PiTFT should start out all white and then turn black. That means the kernel found the display and cleared the screen. If the screen did not turn black, that means that likely there's something up with your connection or kernel install. Solder anything that needs resoldering!
Now that you're rebooted, log back in on the console/TV/SSH. There's nothing displayed on the screen yet, we'll do a test to make sure everything is perfect first!
Run the following commands to startx on the /dev/fb1 framebuffer, a.k.a PiTFT screen:
- sudo mv /usr/share/X11/xorg.conf.d/99βfbturbo.conf ~
- export FRAMEBUFFER=/dev/fb1
- startx
You should see the Pi desktop show up on the TFT! Congrats, you've completed the first test perfectly.
Hit Control-C in the console to quit the X server so we can continue configuration
Next up we'll add support for the touch screen automatically on boot. Edit the module list with
sudo nano /etc/modules
and addΒ stmpe-ts on a line at the end
Save the file and reboot the Pi with sudo reboot and look at the console output (or run dmesg in the console window after logging in) you will see the modules install. Look in particular for the STMPE610 detection and the ILI9340 screen frequency as highlighted here
Create the directory and new calibration configuration file:
sudo mkdir /etc/X11/xorg.conf.d
sudo nano /etc/X11/xorg.conf.d/99-calibration.conf
and enter in the following lines, then save.
- Section βInputClassβ
- Identifier βcalibrationβ
- MatchProduct βstmpe-tsβ
- Option βCalibrationβ β3800 200 200 3800β
- Option βSwapAxesβ β1β
- EndSection
You can now try to run X again with
FRAMEBUFFER=/dev/fb1 startx
Type Control-C to quit X
If you don't ever want to have to type FRAMEBUFFER=/dev/fb1 before startx, you can make it a default state by editing your profile file: sudo nano ~/.profile and adding
export FRAMEBUFFER=/dev/fb1
near the top and saving the file. Then reboot to reload the profile file. It will now always assume you want to use /dev/fb1
Setting up the Touchscreen
Now that the screen is working nicely, we'll take care of the touchscreen. There's just a bit of calibration to do, but it isn't hard at all.
Before we start, we'll make a udev rule for the touchscreen. That's because the eventX name of the device will change a lot and its annoying to figure out what its called depending on whether you have a keyboard or other mouse installed.
Run
sudo nano /etc/udev/rules.d/95-stmpe.rules
to create a new udev file and copy & paste the following line in:
emove and re-install the touchscreen with
sudo rmmod stmpe_ts; sudo modprobe stmpe_ts
Then type ls -l /dev/input/touchscreen
It should point to eventX where X is some number, that number will be different on different setups since other keyboards/mice/USB devices will take up an event slot
There are some tools we can use to calibrate & debug the touchscreen. Install the βevent testβ and βtouchscreen libraryβ packages with
Gesture Input
Installation
Unfortunately xstroke hasn't been actively maintained for a few years so there isn't a binary package you can directly install. However compiling the tool is straightforward and easy with the steps below. Credit for these installation steps goes to mwilliams03 at ozzmaker.com.
First install a few dependencies by opening a command window on the Pi and executing:
- sudo aptβget βy install buildβessential libxftβdev libxpmβdev libxtstβdev
- cd ~
- wget http://mirror.egtvedt.no/avr32linux.org/twiki/pub/Main/XStroke/xstroke-0.6.tar.gz
- tar xfv xstrokeβ0.6.tar.gz
- cd xstrokeβ0.6
- ./configure
- sed βi β/^X_LIBS = / s/$/ -lXrender -lX11 -lXext -ldl/' Makefile
- make
- sudo make install
If the commands above execute successfully xstroke should be installed. If you see an error message, carefully check the dependencies above were installed and try again.
Once xstroke is installed you will want to add a couple menu shortcuts to start and stop xstroke. Execute the following commands to install these shortcuts:
- wget https://github.com/adafruit/PiTFT_Extras/raw/master/xstroke.desktop
- wget https://github.com/adafruit/PiTFT_Extras/raw/master/xstrokekill.desktop
- sudo cp xstroke*.desktop /usr/share/applications/
Usage
To use xstroke I highly recommend using a plastic stylus instead of your finger. Also calibrate the touchscreen for X-Windows so you have the best control over the cursor possible.
Start X-Windows on the PiTFT and open the LXDE menu by clicking the icon in the lower left corner. Scroll up to the Accessories menu at the top and notice the new XStroke and XStroke Kill commands.
Click the XStroke menu option to start xstroke. You should see a small pencil icon appear on the bottom right side of the screen. The pencil icon means xstroke is running, however by default it's not yet looking for gesture input.
Open an application that takes text input, such as LXTerminal. To enable gesture input click the xstroke pencil icon. You should see the pencil turn green and the text βabc' written over top of the icon. You might need to click the icon a few times to get the click to register in the right spot.
When xstroke is looking for gesture input you can drag the mouse cursor in a gesture anywhere on the screen to send specific key strokes. Here's a picture of the possible gestures you can send:
(credit to Carl Worth for the image above)
To draw a gesture from the above image, press anywhere on the screen, start from the circle in the gesture, and follow the gesture pattern towards the arrow. As you draw a gesture you should see a blue line displayed that shows what you've drawn. Lift up the stylus when you get to the end of the gesture at the arrow. If xstroke recognizes the gesture it will send the appropriate key press to the active window. Try drawing a few characters from the image above to get the hang of writing gestures.
A few very useful gestures are backspace (which deletes a character), return/enter, and space. To draw a backspace gesture just draw a line going from the right side of the screen to the left side. The gesture for return/enter is a diagonal line from the top right to bottom left. Finally a space is a straight line from the left to the right.
Note that when xstroke is looking for gestures you might not be able to click or control the cursor as you normally would expect. To stop xstroke's gesture recognition carefully press the xstroke pencil icon again until the βabc' text disappears. I've found this process can be a little finicky as the icon is very small and any movement will be interpreted as a gesture. Use a light touch and try a few times to click the icon.
If you get stuck completely and can't disable xstroke by clicking the icon, connect to the Raspberry Pi in a terminal/SSH connection and run βkillall xstroke' (without quotes) to force xstroke to quit. The normal way to stop xstroke is to navigate to the Accessories -> XStroke Kill command, but you might not be able to do that if xstroke is listening for gesture input.
Have fun using xstroke to control your Pi by writing gestures on the PiTFT screen!
FAQ
My PiTFT used to work, now it doesn't!
Did you do an apt-get upgrade
? This command may blow away our PiTFT kernel which means that you will no longer have PiTFT support, you will have to redo the easy-install steps to reinstall the kernel.
You may be able to reinstall the Adafruit kernel like so:
sudo apt-get install raspberrypi-bootloader=1.20150309-1
If it tells you that the latest version is already installed, try this instead:
sudo apt-get install --reinstall raspberrypi-bootloader=1.20150309-1
β¦you can check here and substitute the most recent version you see in the =1.20150309-1
part.
My PiTFT works for a bit and then I get a black screen with a short line of white pixels in one corner
Sounds like you tried to configure your Pi to βboot straight to X', that is, start up the graphics interface on boot. This doesn't work by default because the Pi operating system is not expecting a PiTFT so it boots to the HDMI output. See below for how to set up your Pi to boot to X on the PiTFT
To βfix' this, you can either connect an HDMI monitor, then in a terminal window run sudo raspi-config and configure the Pi to boot to the command line not X! If you do not have an HDMI monitor, you can also try a console cable
How can I force the Pi to bring up X on the HDMI/TV monitor?
Use the fb0 framebuffer when you want to display stuff on the HDMI/TV display, for example:
FRAMEBUFFER=/dev/fb0 startx
will use the HDMI/TV framebuffer for X windows instead of the PiTFT
That doesn't work! I can't get X on HDMI!
FRAMEBUFFER=/dev/fb0 startx &
and
FRAMEBUFFER=/dev/fb1 startx &
wind up showing the GUI on your PiTFT, enter the following instruction from the command line:
- sudo mv /usr/share/X11/xorg.conf.d/99βfbturbo.conf ~
I'm tring to run startx and I get FATAL: Module g2d_23 not found.
sudo mv /usr/share/X11/xorg.conf.d/99-fbturbo.conf ~
All the PiTFT's we ship now have the button labeled #21 and #27
I want better performance and faster updates!
You can change the SPI frequency (overclock the display) by editing /boot/config.txt and changing the dtoverlay options line to:
dtoverlay=pitft28r,rotate=90,speed=62000000,fps=25
Or whatever you like for speed, rotation, and frames-per-second. BUT, here's the thing, the Pi only supports a fixed number of SPI frequencies. So tweaking the number a little won't do anything. The kernel will round the number to the closest value. You will always get frequencies that are 250MHz divided by an even number. Here's the only SPI frequencies this kernel supports
- 15,625,000 (a.k.a 16000000 = 16 MHz)
- 17,857,142 (a.k.a. 18000000 = 18 MHz)
- 20,833,333 (a.k.a 21000000 = 21 MHz)
- 25,000,000 (= 25 MHz)
- 31,250,000 (a.k.a 32000000 = 32MHz)
- 41,666,666 (a.k.a 42000000 = 42MHz)
- 62,500,000 (a.k.a 62000000 = 62MHz)
So if you put in 48000000 for the speed, you won't actually get 48MHz, you'll actually only get about 42MHz because it gets rounded down. We tested this display nicely with 32MHz and we suggest that. But you can put in 42MHz or even try 62MHz and it will update faster
You can tweak fps (frames per second) from 20 to 60 and frequency up to 62MHz for tradeoffs in performance and speed. Reboot after each edit to make sure the settings are loaded properly. There's a trade off that if you ask for higher FPS you're going to load the kernel more because it's trying to keep the display updated.
How can I take screenshots of the little screen?
We took the screenshots for this tutorial with fbgrab
wget http://fbgrab.monells.se/fbgrab-1.2.tar.gz
tar -zxvf fbgrab*gz
cd fbgrab/
make
./fbgrab screenshot.png
Make sure your Pi boots to the graphical PIXEL desktop on the HDMI output monitor, then using the Easy Installer, selectΒ Mirror HDMI
Check to make syre that the flat flex cableΒ is fully seated in the connetor and the βears' are pushed in to secure it. See the picture for what it should look like:
Yes! Β Please see this post:
https://forums.adafruit.com/viewtopic.php?f=47&t=77528&p=393280#p393322
X11 (the graphical system) has changed how it gets touchscreen input, so if you rotate the display and the calibration isn't being picked up:
Check /usr/share/X11/xorg.conf.d for a file called 10-evdev.conf
If you don't see that file
- You need toΒ
sudo apt-get install xserver-xorg-input-evdev
, and then⦠- If you do have a 40-libinput.conf in that same directory, you must remove it even if/once evdev is installed, since it will override the 10-evdev.conf otherwise.
Thanks to cerebrate in the forums for the hint!
Playing Videos
How To Play Videos
You can play many types of videos on the screen, using mplayer you don't even need to run X and you can script the movies to play using Python. We'll show you how to just play one video for now.
To demo, we'll use an mp4 of Big Buck Bunny for 320 pixel wide screens. Below we show you how to create/resize videos, but to make it easy, just download our version with:
wgetΒ http://adafruit-download.s3.amazonaws.com/bigbuckbunny320p.mp4
If you don't haveΒ mplayerΒ yet, run
sudo apt-get update
sudo apt-get install mplayer
to install it. It may take a few minutes to complete
OK now you just have to run:
sudo SDL_VIDEODRIVER=fbcon SDL_FBDEV=/dev/fb1 mplayer -vo sdl -framedrop bigbuckbunny320p.mp4
If your video is not sized for 320 wide, you may need to add aΒ -zoom
Β afterΒ -framedrop
Β so that it will resize β note that this is quite taxing for the Pi, so it may result in a choppy or mis-synced video!
Converting/Resizing Videos
It's possible to play full length videos on the TFT plate, but since the screen is small and the Pi cant use hardware accelleration to play the videos its best to scale them down to 320Γ240 pixels. This will be easier for the Pi to play and also save you tons of storage space. For this demo, we'll be using the famousΒ Big Buck BunnyΒ video, which is creative commons and also very funny!
You can download it from the link above, we'll be using the 720p AVI version.
Overview
Is this not the cutest little display for the Raspberry Pi? It features a 2.8β³ display with 320Γ240 16-bit color pixels and a resistive touch overlay. The plate uses the high speed SPI interface on the Pi and can use the mini display as a console, X window port, displaying images or video etc. Best of all it plugs right in on top!
Original PiTFT
The original version PID 1601 is designed to fit nicely onto the Pi Model A or B but also works perfectly fine with the Pi Zero, Pi 2, Pi 3 or Pi 1 Model A+ or B+ as long as you don't mind the PCB overhangs the USB ports by 5mm
PiTFT Plus
The newer PiTFTs are updated to fit perfectly onto the Pi Zero, Pi 3, Pi 2 or Model A+, B+! (Any Pi with a 2Γ20 connector)Β Not for use with an old Pi 1 with 2Γ13 connector
This design uses the hardware SPI pins (SCK, MOSI, MISO, CE0, CE1) as well as GPIO #25 and #24. All other GPIO are unused. Since we had a tiny bit of space, there's 4 spots for optional slim tactile switches wired to four GPIOs, that you can use if you want to make a basic user interface. For example, you can use one as a power on/off button.
We bring out GPIO #23, #22, #21, and #18 to the four switch locations!
Assembly
If you want to attach an Adafruit Cobbler or similar, you can solder in the optional 2Γ13 IDC on theΒ bottomΒ of the screen as shown here. This will keep the top side clean and flat. Solder in all 26 pins
The picture shows a 2Γ13 male header. We've since updated this product to include an IDC socket so it's easier to add a cobbler. Both will work, though!
Easy Install
The PiTFT requires some device tree support and a couple other things to make it a nice stand-alone display. If you just want to get going, check out the following for easy-install instructions!
Install Raspberry Pi OS on an SD Card
You'll need to start with Raspberry Pi OS or Raspberry Pi OS Lite.
The last known for-sure tested-and-working version is May 28, 2021 (https://downloads.raspberrypi.org/raspios_lite_armhf/images/raspios_lite_armhf-2021-05-28/) fromΒ https://downloads.raspberrypi.org/raspios_lite_armhf/images/
Raspberry Pi OS does often βbreak' stuff when new versions come out so to be safe, if you are having problems try this version!
Installer script
This script will do all the work for you, and install both device tree overlay support as well as configure rotation and any HDMI mirroring. PiTFT no longer needs any custom kernels or modules, so you can continue to update/upgrade your Pi and it will work with the most recent releases.
Here's the commands to run. Make sure your Pi has network access, it needs to download the software!
The latest installer script was rewritten in Python, so it can be installed with just a few commands. First, start by installing a few dependencies and downloading the repo:
cd ~ sudo apt-get update sudo apt-get install -y git python3-pip sudo pip3 install --upgrade adafruit-python-shell click git clone https://github.com/adafruit/Raspberry-Pi-Installer-Scripts.git cd Raspberry-Pi-Installer-Scripts
Easy Single Command Install Options
The latest script allows you to specify all of your options within the command so you can sit back and watch the installation. We'll cover interactive installation below if you want to answer each prompt for a customized installation.
βConsole Modeβ Install Commands
If you just want a Linux text console to appear on the display, use one of the following commands:
For theΒ PiTFTΒ 2.4β³, 2.8β³, or 3.2β³ Resistive touchscreens, use the following command:
sudo python3 adafruit-pitft.py --display=28r --rotation=90 --install-type=console
For theΒ PiTFTΒ 2.8β³ Capacitive touchscreen, use the following command:
sudo python3 adafruit-pitft.py --display=28c --rotation=90 --install-type=console
For theΒ PiTFTΒ 2.2β³ Display, use the following command:
sudo python3 adafruit-pitft.py --display=22 --rotation=90 --install-type=console
For theΒ PiTFTΒ 3.5β³ Resistive touchscreen, use the following command:
sudo python3 adafruit-pitft.py --display=35r --rotation=90 --install-type=console
For theΒ MiniΒ PiTFT 1.3β³, BrainCraft HAT, 1.3β³ Color TFT Bonnet, or 1.5β³ Display, use the following command:
sudo python3 adafruit-pitft.py --display=st7789_240x240 --rotation=0 --install-type=console
For theΒ Mini PiTFT 1.14β³ Display, use the following command:
sudo python3 adafruit-pitft.py --display=st7789_240x135 --rotation=270 --install-type=console
FBCP Install Commands
If you want toΒ mirror the HDMI output to the display, known as βFrameBuffer Copyβ or FBCP for short, use one of the following commands:
For theΒ PiTFTΒ 2.4β³, 2.8β³, or 3.2β³ Resistive touchscreens, use the following command:
sudo python3 adafruit-pitft.py --display=28r --rotation=90 --install-type=fbcp
For theΒ PiTFTΒ 2.8β³ Capacitive touchscreen, use the following command:
sudo python3 adafruit-pitft.py --display=28c --rotation=90 --install-type=fbcp
For theΒ PiTFTΒ 2.2β³ Display, use the following command:
sudo python3 adafruit-pitft.py --display=22 --rotation=90 --install-type=fbcp
For theΒ PiTFTΒ 3.5β³ Resistive touchscreen, use the following command:
sudo python3 adafruit-pitft.py --display=35r --rotation=90 --install-type=fbcp
For theΒ MiniΒ PiTFT 1.3β³, BrainCraft HAT, 1.3β³ Color TFT Bonnet, or 1.5β³ Display, use the following command:
sudo python3 adafruit-pitft.py --display=st7789_240x240 --rotation=0 --install-type=fbcp
For theΒ Mini PiTFT 1.14β³ Display, use the following command:
sudo python3 adafruit-pitft.py --display=st7789_240x135 --rotation=270 --install-type=fbcp
Rebooting
When it asks you toΒ reboot, then choose yes because the setting won't take full effect until you do so.
Interactive Installation
For an interactive install, you can just run the script without any options:
sudo python3 adafruit-pitft.py
Once you run it you will be presented with menus for configuration.
Display Selection
Start by selecting the board that best corresponds with your PiTFT. There's lots of options and we will likely be adding more in the future.
Rotation
Next you will be asked for the rotation you want, don't worry if you're not 100% sure which you want, you can always change this later by re-running the script
It will take a few minutes to install the software and download all the thingsβ¦
Configuring what shows where
You have a few different ways to set up the PiTFT, we askΒ 2Β questions to figure out what you want
PiTFT as Text Console (best for Raspberry Pi OS βLite')
This is the simplest to set-up type of use.Β Its great if you have a simple text based or pygame/SDL based interface. If you want the PiTFT to act as a text console you can expect:
- HDMI will be βdeactivated' β nothing appears on the HDMI output but a black screen
- The login prompt appears on the Pi
- The Pi is all text, not a GUI (no PIXEL desktop)
- Keyboard and mouse are used only by the PiTFT interface
- Framebuffer-capable software (such asΒ fbiΒ for displaying images,Β mplayerΒ for videos, or pygame software, etc) appear on the PiTFT
- OpenGL accelerated softwareΒ will not appear on the PiTFTΒ (it is unaccelerated framebuffer only)
- But, non-OpenGL-accelerated graphics software is a bit faster than using HDMI mirroring (not tons faster but you're not runningΒ fbcpΒ which will always make it faster)
If you want that, sayΒ YesΒ to the questionΒ Would you like the console to appear on the PiTFT display
Then simply reboot. Once rebooted you will not see anything on HDMI, but the console will appear on the PiTFT. That's it!
PiTFT as HDMI Mirror (Best for Raspberry Pi OS with Desktop)
This option is the easiest to understand: whatever appears on the HDMI display will be βmirrored' to the PiTFT. Note that HDMI is much higher resolution so it's not like it turns the PiTFT into a 1080p display. This is great for when you want to run OpenGL-optimized software, PIXEL desktop software, or really anything. The down-side is its a little slower than drawing directly to the framebuffer. You may not notice it but it's worth us mentioning!
- HDMI will be βactivated' but at a lower resolution β you can change this later but it looks best at 320Γ240 (PiTFT 2.2β³, 2.4β³, 2.8β³ and 3.2β³) or 480Γ320 (PiTFT 3.5β³)
- The login prompt or GUI appears on both HDMI and PiTFT at the same time
- Keyboard and mouse are shared, since the display is mirrored
- All graphics appear on both HDMI and PiTFT, thanks toΒ fbcp
If you want that, sayΒ YesΒ to the questionWould you like the HDMI display to mirror to the PiTFT display?
PiTFT as Raw Framebuffer Device
For advanced users who are comfortable using framebuffer devices, it is possible to have the PiTFT and HDMI graphics beΒ bothΒ active and display different data.
- HDMI will be active and act like a normal Pi
- The login prompt or GUI (PIXEL) appears on the HDMI
- PiTFT appears black, nothing appears on it
- Keyboard and mouse are used by the HDMI interface but can, in theory, be captured and used to change graphics on PiTFT through programming
- Framebuffer-capable software (such asΒ fbiΒ for displaying images,Β mplayerΒ for videos, or pygame software, etc)Β canΒ appear on the PiTFT if you set it up to display toΒ /dev/fb1
- OpenGL accelerated softwareΒ will never appear on the PiTFTΒ (it is unaccelerated framebuffer only)
If you want that, sayΒ NoΒ to both of the configuration questions!
Creating Your Own Automated Command
Under theΒ Easy Single Command Install OptionsΒ section, there are a bunch of different installation flags that you can choose between. If you want to create your own command, you can find the latest options by typing:
sudo python3 adafruit-pitft.py --help
The main options to pay attention to are theΒ --display
,Β --rotation
, andΒ --install-type
Β arguments.
For theΒ display, you can either choose the number you type for the options or the display type, which has the options listed under help. For instance, to us the 2.8β³ Resisitive display you could use
--display=1
Β orΒ --display=28r
For theΒ rotation, you can either choose the number you type for the options or the angle, which has the options also listed under help. To use 90 degrees, you could use
--rotation=1
Β orΒ --rotation=90
ForΒ install type, You can either chooseΒ fbcp,Β consoleΒ orΒ uninstall. To use FBCP, you would simply supply
--install-type=fbcp
Automated Reboot Option
To go with a fully automated installation, you can provide aΒ --reboot
Β flag. To always reboot, use:
--reboot=yes
This is useful if you want to use the display right after running the script. To never reboot immediately, use
--reboot=no
This is useful if you want it as part of a larger script that installs many things.
Custom User Directory
By default, it is installed under the current user directory, but you can provide theΒ -u
Β orΒ --user
Β flag along with the folder that you want to install to.
Uninstalling The Driver
To uninstall, you would either run the script interactively and choose Uninstall or you can provide the uninstall flag:
--install-type=uninstall
Unsupported Full Images
Historically, we provided full βimages' of Raspbian, which is now called Raspberry Pi OS. This worked OK until Raspbian started doing releases every few months. These are no longer supported, and won't even boot on Pi 3B+, so we recommend the script above.
There's the larger βclassic Jessie' image that will boot into X by default, and requires a 8G image, it has a lot more software installed. There's also the smaller βJessie Lite' that will boot into the command line, and can be burned onto a 2G card! Click below to download and install into a new SD card.Β Unzip and follow the classic SD card burning tutorials
PiTFT 2.2β³ Images
- Raspbian Jessie 2016/10/23-based image
- Raspbian Jessie Lite 2016/10/23-based image
- Raspbian Jessie 2016/03/25-based image
- Raspbian Jessie Lite 2016/03/25-based image
- Raspbian Jessie 2015/09/24-based image
- Raspbian Wheezy 2015/09/09-based image
PiTFT 2.4β³/2.8β³/3.2β³ Resistive Images
- Raspbian Jessie 2016/9/23-based image
- Raspbian Jessie Lite 2016/9/23-based image
- Raspbian Jessie 2016/03/25-based image
- Raspbian Jessie Lite 2016/03/25-based image
- Raspbian Jessie 2015/09/24-based image
- Raspbian Wheezy 2015/09/09-based image
- Raspbian 2014/06/20-based image
- Raspbian 2014/09/09-based image
PiTFT 2.8β³ Capacitive
- Raspbian Jessie 2016-09-23-based image
- Raspbian Jessie Lite 2016-09-23-based image
- Raspbian Jessie 2016-03-25-based image
- Raspbian Jessie Lite 2016-03-25-based image
- Raspbian Jessie 2015/09/24-based image
- Raspbian Wheezy 2015/09/24-based image
- Raspbian 2014/09/18-based image
- Raspbian 2014/06/20-based image
- Raspbian image from 2015/03/03
PiTFT 3.5β³ Images
Resistive Touchscreen Manual Install & Calibrate
Setting up the Touchscreen
Now that the screen is working nicely, we'll take care of the touchscreen. There's just a bit of calibration to do, but it isn't hard at all.
Before we start, we'll make aΒ udevΒ rule for the touchscreen. That's because theΒ eventXΒ name of the device will change a lot and its annoying to figure out what its called depending on whether you have a keyboard or other mouse installed.
Run
sudo nano /etc/udev/rules.d/95-stmpe.rules
to create a newΒ udevΒ file and copy & paste the following line in:
SUBSYSTEM=="input", ATTRS{name}=="stmpe-ts", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
Remove and re-install the touchscreen with
sudo rmmod stmpe_ts; sudo modprobe stmpe_ts
Then typeΒ ls -l /dev/input/touchscreen
It should point toΒ eventXΒ where X is some number, that number will be different on different setups since other keyboards/mice/USB devices will take up an event slot
There are some tools we can use to calibrate & debug the touchscreen. Install the βevent testβ and βtouchscreen libraryβ packages with
sudo apt-get install evtest tslib libts-bin
Running evtest
Now you can use some tools such as
sudo evtest /dev/input/touchscreen
which will let you see touchscreen events in real time, press on the touchscreen to see the reports.
AutoMagic Calibration Script
If you rotate the display you need to recalibrate the touchscreen to work with the new screen orientation. You can manually run the calibration processes in the next section, or you can re-run the installer script and select a new rotation:
Manual Calibration
If the βautomagicβ calibration technique isn't working for you, or you have some other setup where you need to carefully calibrate you can do it βmanually'
You will want to calibrate the screen once but shouldn't have to do it more than that. We'll begin by calibrating on the command line by running
sudo TSLIB_FBDEVICE=/dev/fb1 TSLIB_TSDEVICE=/dev/input/touchscreen ts_calibrate
follow the directions on the screen, touching each point. Using a stylus is suggested so you get a precise touch. Don't use something metal, plastic only!
Next you can run
sudo TSLIB_FBDEVICE=/dev/fb1 TSLIB_TSDEVICE=/dev/input/touchscreen ts_test
which will let you draw-test the touch screen. Go back and re-calibrate if you feel the screen isn't precise enough!
X Calibration
You can also calibrate the X input system but you have to use a different program calledΒ xtcalΒ (xinput_calibrator no longer works)
You can do this if the calibration on the screen isn't to your liking or any time you change theΒ rotate=XXΒ module settings for the screen. Since the screen and touch driver are completely separated, the touchscreen doesn't auto-rotate
Download and compile it with the following:
sudo apt-get install libxaw7-dev libxxf86vm-dev libxaw7-dev libxft-dev
git clone https://github.com/KurtJacobson/xtcal
cd xtcal
make
You must be running PIXEL (the GUI) while calibrating.
Before you start the calibrator you will need to βreset' the old calibration data so run
DISPLAY=:0.0 xinput set-prop "stmpe-ts" 'Coordinate Transformation Matrix' 1 0 0 0 1 0 0 0 1
Now you'll have to run the calibrator while also running X. You can do this by opening up the terminal program and running the theΒ xtcalΒ command (which is challenging to do on such a small screen) OR you can do what we do which is create an SSH/Terminal shell and then run the calibrator from the same shell, which requires the following command:
DISPLAY=:0.0 xtcal/xtcal -geometry 640x480
Note that theΒ geometry
Β may vary!
If you are using a 2.4β³/2.8β³/3.2β³ 320Γ240 display with landscape orientation, use 640Γ480. If you're in portrait, use 480Γ640.
If you are using a 3.5β³ display with landscape, use 720Γ480, portrait is 480Γ720
Follow the directions on screen
RunΒ sudo nanoΒ /usr/share/X11/xorg.conf.d/20-calibration.confΒ and copy the 9 numbers into the TransformationMatrix option so it looks like:
Section "InputClass"
Identifier "STMPE Touchscreen Calibration"
MatchProduct "stmpe"
MatchDevicePath "/dev/input/event*"
Driver "libinput"
Option "TransformationMatrix" "-0.000087 1.094214 -0.028826 -1.091711 -0.004364 1.057821 0 0 1"
EndSection
or whatever you got, into there.
You will want to reboot your Pi to verify you're done
Your touchscreen is now super calibrated, hurrah!
Console Configuration
One fun thing you can do with the display is have it as your main console instead of the HDMI/TV output. Even though it is small, with a good font you can get 20 x 40 of text. For more details, check outΒ https://github.com/notro/fbtft/wiki/Boot-console
First up, we'll update the boot configuration file to use the TFT framebufferΒ /dev/fb1Β instead of the HDMI/TV framebuffer /dev/fb0
sudo nano /boot/cmdline.txt
you can also edit it by putting the SD card into a computer and opening the same file.
At the end of the line, find the text that saysΒ rootwait
Β and right after that, enter in:
fbcon=map:10 fbcon=font:VGA8x8
Β then save the file.
On the next boot, it will bring up the console.
Note that the kernel has to load up the display driver module before it can display anything on it so you won't get the rainbow screen, a NooBs prompt, or a big chunk of the kernel details since the module is loaded fairly late in the boot process.
Turn off Console Blanking
You may notice the console goes black after 30 minutes, this is a sort of βpower saving' or βscreensaver' feature.
Raspbian Jessie
Add the following line to /etc/rc.local
sudo sh -c "TERM=linux setterm -blank 0 >/dev/tty0"
on the line before the finalΒ exit 0
Raspbian Wheezy
You can disable this by editingΒ /etc/kbd/configΒ and looking for
BLANK_TIME=30
and setting the blank time to 0 (which turns it off)
BLANK_TIME=0
Userspace Tools
Sometimes the PiTFT device tree and related kernel packageΒ donβt work across different OS releases, so weβve experimented with an alternate approach thatΒ doesnβt rely on a custom kernelΒ β it insteadΒ works in βuser space.β So far itβs workedΒ well regardless of the OS version being used!
There areΒ tradeoffs. The code is still in a rough state withΒ many features yet to be implemented, and also the performance is slightly less than the kernel approach. Itβs typicallyΒ adequateΒ though, even for game emulation (RetroPie, etc.), so give it a try if youβve had trouble with the βclassicβ approach.
This currently requires a bit of Linux-y knowledge, editing files and suchβ¦
Download, Test and Install
PiTFT displays useΒ SPIΒ to communicate, so make sure thatβs enabled using theΒ raspi-configΒ utility:
sudo raspi-config
Menu options move around from time to timeβ¦at the time of this writing, SPI is under βInterfacing Options.β
ThenΒ retrieve the softwareΒ usingΒ wgetβ¦
wget https://github.com/adafruit/Adafruit_Userspace_PiTFT/archive/master.zip
unzip master.zip
And then a quick testβ¦
cd Adafruit_Userspace_PiTFT-master
sudo ./tftcp
The PiTFT should mirror the contents of the Raspberry Piβs HDMI output at this point. Text and everything will be microscopic, but weβre just checking that the programΒ runs. If not, confirm that the fileΒ /dev/spidev0.0Β exists β this should happen when SPI is enabled. Double-check raspi-config and it never hurts to reboot.
Does it run? Good. Press control+c to kill the program, and weβll set it up to run automatically on boot.
First, copy theΒ tftcpΒ executable toΒ /usr/local/bin:
sudo cp tftcp /usr/local/bin
Then edit the fileΒ /etc/rc.localΒ asΒ rootΒ (you can substitute your editor of preference for nano):
sudo nano /etc/rc.local
Just aboveΒ the final βexit 0β line, insert the following line:
/usr/local/bin/tftcp &
The screenΒ looks best if the HDMI resolution exactly matches the PiTFT resolution, so the final step is to configure the system for 320Γ240 video:
sudo nano /boot/config.txt
Append the following lines to the bottom of the file:
disable_overscan=1
hdmi_force_hotplug=1
hdmi_group=2
hdmi_mode=87
hdmi_cvt=320 240 60 1 0 0 0
OPTIONAL: you can also use β640 480β in place of β320 240β above. This is exactly twice the PiTFT native resolution, and the tftcp utility will perform a smooth 2:1 filtering of the image. Any larger though and the image isnβt as sharp (and text becomes tiny, like when we first tested it).
NowΒ rebootΒ and the PiTFT should activate toward the end of the boot process.
Resistive Touchscreen Support
This is even more experimental than theΒ tftcpΒ utilityβ¦it only works with the resistive screen, and thereβs no calibration support yet, but if youβd like to try it outβ¦
First thereβs some prerequisite software to install:
sudo apt-get update
sudo apt-get install python-pip python-smbus python-dev
sudo pip install evdev
βcdβ to theΒ same directory where the software was downloaded earlier, and try it outβ¦
cd Adafruit_Userspace_PiTFT-master
sudo python touchmouse.py
Whether youβre in X11 or in text console mode (e.g. Raspbian Lite), the cursor should move in response to touch, which is emulating a mouse.
If that seems OK, press control+c to stop it and weβll use the same steps to make it auto-run on boot:
sudo cp touchmouse.py /usr/local/bin
sudo nano /etc/rc.local
Insert this line just above the βexit 0β at the end of the file:
/usr/bin/python /usr/local/bin/touchmouse.py &
rebootΒ and both PiTFT and touch should be active now.
HELP! (FAQ)
If you messed withΒ /boot/config.txt
Β orΒ /etc/rc.local
Β you may have removed or disabled some of the elements required for the PiTFT to work. Try re-running the Easy Installer script!
The Raspberry Pi 4 and the Raspberry Pi 400 have dual HDMI outputs. However, only one of those is a primary port. The is likely caused by connecting to HDMI1. Please try connecting the display to the other HDMI port and rebooting.
For the Pi 4, HDMI 0 is closer to the USB-C port. For the Pi 400, it's the port closer to the MicroSD Card slot.
It looks like the Pi is βhalting' or βlocking' up during bootΒ but what is really happening is the console is switching from the HDMI output to the PiTFT console output.
Check your PiTFT connections, particularly make sure you seated the PiTFT on the Pi properly, nothing is in the way, and the TFT flex connector is seated properly.
Sounds like you tried to configure your Pi to βboot straight to X', that is, start up the graphics interface on boot. This doesn't work by default because the Pi operating system is not expecting a PiTFT so it boots to the HDMI output. See below for how to set up your Pi to boot to X on the PiTFT
To βfix' this, you can either connect an HDMI monitor, then in a terminal window runΒ sudo raspi-configΒ and configure the Pi to boot to the command line not X! If you do not have an HDMI monitor, you can also try a console cable
sudo mv /usr/share/X11/xorg.conf.d/99-fbturbo.conf ~
Some programs are graphics-optimized, particularly the video playback tools and some other programs like Minecraft. They write βdirectly' to the HDMI output, and cannot write to the PiTFT so there is no way to directly make them work. However, youΒ canΒ have the output go to HDMI and then mirror the HDMI onto the PiTFT withΒ fbcp. Using the Easy Installer, selectΒ Mirror HDMI
All the PiTFT's we ship now have the button labeled #21 and #27
You can change the SPI frequency (overclock the display) by editingΒ /boot/config.txtΒ and changing theΒ dtoverlayΒ options line to:
dtoverlay=pitft28r,rotate=90,speed=62000000,fps=25
Or whatever you like for speed, rotation, and frames-per-second. BUT, here's the thing, the Pi only supports aΒ fixed numberΒ of SPI frequencies. So tweaking the number a little won't do anything. The kernel will round the number to the closest value. You will always get frequencies that are 250MHz divided by an even number. Here's the only SPI frequencies this kernel supports
- 15,625,000 (a.k.a 16000000 = 16 MHz)
- 17,857,142 (a.k.a. 18000000 = 18 MHz)
- 20,833,333 (a.k.a 21000000 = 21 MHz)
- 25,000,000 (= 25 MHz)
- 31,250,000 (a.k.a 32000000 = 32MHz)
- 41,666,666 (a.k.a 42000000 = 42MHz)
- 62,500,000 (a.k.a 62000000 = 62MHz)
So if you put in 48000000 for the speed, you won't actually get 48MHz, you'll actually only get about 42MHz because it gets rounded down. We tested this display nicely with 32MHz and we suggest that. But you can put in 42MHz or even try 62MHz and it will update faster
You can tweak fps (frames per second) from 20 to 60 and frequency up to 62MHz for tradeoffs in performance and speed. Reboot after each edit to make sure the settings are loaded properly. There's a trade off that if you ask for higher FPS you're going to load the kernel more because it's trying to keep the display updated.
We took the screenshots for this tutorial withΒ fbgrab
wgetΒ http://fbgrab.monells.se/fbgrab-1.2.tar.gz
tar -zxvf fbgrab*gz
cd fbgrab/
make
./fbgrab screenshot.png
Make sure your Pi boots to the graphical PIXEL desktop on the HDMI output monitor, then using the Easy Installer, selectΒ Mirror HDMI
Check to make syre that the flat flex cableΒ is fully seated in the connetor and the βears' are pushed in to secure it. See the picture for what it should look like:
This happens on the Raspberry Pi the first time you run startx, no matter what display. You can just re-start X and it wont appear again.
Yes! Β Please see this post:
https://forums.adafruit.com/viewtopic.php?f=47&t=77528&p=393280#p393322
X11 (the graphical system) has changed how it gets touchscreen input, so if you rotate the display and the calibration isn't being picked up:
Check /usr/share/X11/xorg.conf.d for a file called 10-evdev.conf
If you don't see that file
- You need toΒ
sudo apt-get install xserver-xorg-input-evdev
, and then⦠- If you do have a 40-libinput.conf in that same directory, you must remove it even if/once evdev is installed, since it will override the 10-evdev.conf otherwise.
Playing Videos
How To Play Videos
You can play many types of videos on the screen, using mplayer you don't even need to run X and you can script the movies to play using Python. We'll show you how to just play one video for now.
To demo, we'll use an mp4 of Big Buck Bunny for 320 pixel wide screens. Below we show you how to create/resize videos, but to make it easy, just download our version with:
wgetΒ http://adafruit-download.s3.amazonaws.com/bigbuckbunny320p.mp4
If you don't haveΒ mplayerΒ yet, run
sudo apt-get update
sudo apt-get install mplayer
to install it. It may take a few minutes to complete
OK now you just have to run:
sudo SDL_VIDEODRIVER=fbcon SDL_FBDEV=/dev/fb1 mplayer -vo sdl -framedrop bigbuckbunny320p.mp4
If your video is not sized for 320 wide, you may need to add aΒ -zoom
Β afterΒ -framedrop
Β so that it will resize β note that this is quite taxing for the Pi, so it may result in a choppy or mis-synced video!
Converting/Resizing Videos
It's possible to play full length videos on the TFT plate, but since the screen is small and the Pi cant use hardware accelleration to play the videos its best to scale them down to 320Γ240 pixels. This will be easier for the Pi to play and also save you tons of storage space. For this demo, we'll be using the famousΒ Big Buck BunnyΒ video, which is creative commons and also very funny!
You can download it from the link above, we'll be using the 720p AVI version.
Displaying Images
You can display every day images such as GIFs, JPGs, BMPs, etc on the screen. To do this we'll installΒ fbiΒ which is theΒ frame buffer imageΒ viewer (not to be confused with the FBI agency!)
sudo apt-get install fbiΒ will install it
Grab our lovely wallpapers with
wgetΒ http://adafruit-download.s3.amazonaws.com/adapiluv320x240.jpg
wgetΒ http://adafruit-download.s3.amazonaws.com/adapiluv480x320.png
For 320Γ240 PiTFTs (2.2β³, 2.4β³, 2.8β³ or 3.2β³) view it with
sudo fbi -T 2 -d /dev/fb1 -noverbose -a adapiluv320x240.jpg
or for 3.5β³ PiTFTs:
sudo fbi -T 2 -d /dev/fb1 -noverbose -a adapiluv480x320.png
That's it!
The Ideal:Β Adafruitβs PiTFT displays are razor sharp. Whereas small composite screens on the Raspberry Pi usually require some video scaling (resulting in blurriness), PiTFT uses the GPIO header, digitally controlled pixel-by-pixel for a rock steady image. Though not aΒ lotΒ of pixels, it works great for retro gaming (and the display neatly stacks above the board, no side protuberances for video cables).
The Downside:Β this GPIO link entirely bypassesΒ the Piβs video hardware, including the graphics accelerator. Many games and emulatorsΒ dependΒ on the GPU for performance gains. SoΒ the PiTFT has traditionally been limited to just a subset of specially-compiled emulators that can work and run well enough without the GPU.
The Solution:Β our latest PiTFT drivers, along with a toolΒ calledΒ fbcpΒ (framebuffer copy), careful system configuration, and (optionally) the more potent Raspberry Pi 2 board open the doors to many more gaming options. Existing emulator packages (such as RetroPie, withΒ dozensΒ of high-performance emulators and ports) β previously off-limits to the PiTFT β can run quite effectively now!
Backlight Control
The backlight of the 2.8β³ PiTFT has 4 LEDs in series and it draws ~75mA at all times, controlled by a transistor. The PiTFT 3.5β³ display has 6 LEDs in a row, and we use a boost converter to get the 5V from the Pi up to the ~20V needed to light up all the LEDs.
There might be times you'd like to save some power and turn off the backlight. The screen and touchplate will still work, you just can't see anything. We designed the board with the STMPE610 touchscreen controller which has 2 extra GPIO and tied one of them to control the backlight. You can use the command line to control the backlight.
By default, the backlight's onβ¦but you can control it in two ways!
PWM Backlight Control with GPIO 18
If you want precise control, you can use the PWM output on GPIO 18. There's python code for controlling the PWM but you can also just use the kernel module and shell commands.
You'll need to make sure the STMPE control is not βactive' as the STMPE GPIO overrides the PWM output.
sudo sh -c 'echo "0" > /sys/class/backlight/soc\:backlight/brightness'
(Or if you are running an old kernel before the backlight object, tryΒ sudo sh -c "echo 'in' > /sys/class/gpio/gpio508/direction"
)
OK now you can set the GPIO #18 pin to PWM mode using WiringPi'sΒ gpioΒ command
With these basic shell commands, you can set the GPIO #18 pin to PWM mode with 1000 Hz frequency, set the output to 100 (out of 1023, so dim!), set the output to 1023 (out of 1023, nearly all the way on) and 0 (off)
gpio -g mode 18 pwm gpio pwmc 1000 gpio -g pwm 18 100 gpio -g pwm 18 1023 gpio -g pwm 18 0
Disabling Backlight Control
If you'd like to not have #18 control the backlight, simply cut the solder jumper, the tiny trace between the two large gold pads markedΒ Lite #18
On / Off Using STMPE GPIO
Another option is to just turn it on and off using the extra GPIO created by the touchscreen driver
Thanks to the raspberry Pi overlay system, this GPIO is already set up for you in a file calledΒ /sys/class/backlight/soc:backlight/brightness
To turn the backlight off run
sudo sh -c 'echo "0" > /sys/class/backlight/soc\:backlight/brightness'
To turn it back on, run
sudo sh -c 'echo "1" > /sys/class/backlight/soc\:backlight/brightness'
On / Off Using GPIO for the Capacitive Display
If you installed the capacitive display, you may notice thatΒ /sys/class/backlightΒ isn't available. This is because the capacitive display uses a different touchscreen driver than the STMPE. You can however still control it using GPIO 18.Β Start by getting access to the GPIO by making a device link:
echo 18 >/sys/class/gpio/export
Check that theΒ gpio18Β was created:
ls /sys/class/gpio/
Set the direction to out with:
echo out >/sys/class/gpio/gpio18/direction
The backlight should turn off. You can turn it on with:
echo 1 >/sys/class/gpio/gpio18/value
or back off again with
echo 0 >/sys/class/gpio/gpio18/value
For older versions of PiTFT Kernel
On older versions of the PiTFT kernel/overlay, the GPIO was not tied to the backlight device. Start by getting access to the GPIO by making a device link
sudo sh -c "echo 508 > /sys/class/gpio/export"
ls -l /sys/class/gpio
For someΒ reallyΒ old versions, the GPIO pin was #252 not #508 so substitute that if you're running something from 2014 or earlier
Once you verify that you see GPIO #508, then you can set it to an output, this will turn off the display since it will output 0 by default
sudo sh -c "echo 'out' > /sys/class/gpio/gpio508/direction"
Then turn the display back on with
sudo sh -c "echo '1' > /sys/class/gpio/gpio508/value"
or back off
sudo sh -c "echo '0' > /sys/class/gpio/gpio508/value"
Python Backlight Control with Blinka
You can also use python to control the backlight using Python. With Blinka, you can use it to turn the backlight off or on or even use PWM to control the level of the backlight. Since Python is such a flexible language, you can add some effects such as fading the backlight in or fading it out. First make sure you have Blinka installed. For the Raspberry Pi, you can follow ourΒ CircuitPython on Linux and Raspberry PiΒ guide. To turn the backlight on, you can use this simple demo script:
import board
import digitalio
backlight = digitalio.DigitalInOut(board.D18)
backlight.value = True
To turn it off, you can use this script:
import board
import digitalio
backlight = digitalio.DigitalInOut(board.D18)
backlight.value = False
To use PWM and demonstrate the fading in and fading out effect, you can try running this script:
import time
import board
import pwmio
led = pwmio.PWMOut(board.D18, frequency=5000, duty_cycle=0)
while True:
for i in range(101):
led.duty_cycle = int(i * 65535 / 100) # Up
time.sleep(0.01)
time.sleep(1)
for i in range(100, -1, -1):
led.duty_cycle = int(i * 65535 / 100) # Down
time.sleep(0.01)
time.sleep(1)
PiTFT PyGame Tips
Since the PiTFT screen is fairly small, you may need to write custom UI programs. Pygame is the easiest way by far to do this.
Jeremy Blythe has an excellent tutorial here on getting started.
However,Β beforeΒ you follow that link you'll want to set up pygame for the best compatibility:
Install pip & pygame
Install Pip:Β sudo apt-get install python-pip
Ensure you are running SDL 1.2
SDL 2.x and SDL 1.2.15-10 have some serious incompatibilities with touchscreen. You can force SDL 1.2 by running a script. (Thanks to heine in the forums!)
Edit a new file withΒ sudo nanoΒ installsdl.sh
and paste in the following text:
#!/bin/bash
# enable wheezy package sources
echo "deb http://legacy.raspbian.org/raspbian wheezy main
" > /etc/apt/sources.list.d/wheezy.list
# set stable as default package source (currently buster)
echo "APT::Default-release \"stable\";
" > /etc/apt/apt.conf.d/10defaultRelease
# set the priority for libsdl from wheezy higher then the buster package
echo "Package: libsdl1.2debian
Pin: release n=buster
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 --allow-downgrades install libsdl1.2debian/wheezy
run
sudo sh ./installsdl.sh
Using the Capacitive touch screen in PyGame
The 2.8β³ Capacitive touch screen driver may not work by default in pygame, but this handy script shows how you can capture the device messages in python to create a UI
Extras!
Making it easier to click icons in X
If you want to double-click on icons to launch something in X you may find it annoying to get it to work right. In LXDE you can simply set it up so that you only need to single click instead of double.
From LXDE launch the file manager (sorry these pix are grayscale, still figuring out how to screenshot the framebuffer!)
Right-click on a touchscreen
Obviously if you have a touchscreen, it cannot tell what finger you are pressing with. This means that all βclicks' are left clicks. But if you want a right-click, youΒ canΒ do it.
Just add the following lines into your InputClass ofΒ /etc/X11/xorg.conf.d/99-calibration.confΒ after the calibration section
Option "EmulateThirdButton" "1"
Option "EmulateThirdButtonTimeout" "750"
Option "EmulateThirdButtonMoveThreshold" "30"
So for example your file will look like:
Section "InputClass"
IdentifierΒ Β Β "calibration"
MatchProductΒ Β "stmpe-ts"
OptionΒ "Calibration"Β Β "3800 120 200 3900"
OptionΒ "SwapAxes"Β Β Β "1"
Option "EmulateThirdButton" "1"
Option "EmulateThirdButtonTimeout" "750"
Option "EmulateThirdButtonMoveThreshold" "30"
EndSection
This makes a right mouse click emulated when holding down the stylus for 750 ms.
Gesture Input
Installation
Unfortunately xstroke hasn't been actively maintained for a few years so there isn't a binary package you can directly install. However compiling the tool is straightforward and easy with the steps below. Credit for these installation steps goes toΒ mwilliams03 at ozzmaker.com.
First install a few dependencies by opening a command window on the Pi and executing:
sudo apt-get -y install build-essential libxft-dev libxpm-dev libxtst-dev
cd ~
wget http://mirror.egtvedt.no/avr32linux.org/twiki/pub/Main/XStroke/xstroke-0.6.tar.gz
tar xfv xstroke-0.6.tar.gz
cd xstroke-0.6
./configure
sed -i '/^X_LIBS = / s/$/ -lXrender -lX11 -lXext -ldl/' Makefile
make
sudo make install
If the commands above execute successfully xstroke should be installed. If you see an error message, carefully check the dependencies above were installed and try again.
Once xstroke is installed you will want to add a couple menu shortcuts to start and stop xstroke. Execute the following commands to install these shortcuts:
wget https://github.com/adafruit/PiTFT_Extras/raw/master/xstroke.desktop
wget https://github.com/adafruit/PiTFT_Extras/raw/master/xstrokekill.desktop
sudo cp xstroke*.desktop /usr/share/applications/
Usage
To use xstroke I highly recommend using a plastic stylus instead of your finger. AlsoΒ calibrate the touchscreen for X-WindowsΒ so you have the best control over the cursor possible.
Start X-Windows on the PiTFT and open the LXDE menu by clicking the icon in the lower left corner. Scroll up to theΒ AccessoriesΒ menu at the top and notice the newΒ XStrokeΒ andΒ XStroke KillΒ commands.
Click theΒ XStrokeΒ menu option to start xstroke. You should see a small pencil icon appear on the bottom right side of the screen. The pencil icon means xstroke is running, however by default it's not yet looking for gesture input.
Open an application that takes text input, such as LXTerminal. To enable gesture input click the xstroke pencil icon. You should see the pencil turn green and the text βabc' written over top of the icon. You might need to click the icon a few times to get the click to register in the right spot.
When xstroke is looking for gesture input you can drag the mouse cursor in a gesture anywhere on the screen to send specific key strokes. Here's a picture of the possible gestures you can send:
(credit to Carl Worth for the image above)
To draw a gesture from the above image, press anywhere on the screen, start from the circle in the gesture, and follow the gesture pattern towards the arrow. As you draw a gesture you should see a blue line displayed that shows what you've drawn. Lift up the stylus when you get to the end of the gesture at the arrow. If xstroke recognizes the gesture it will send the appropriate key press to the active window. Try drawing a few characters from the image above to get the hang of writing gestures.
A few very useful gestures are backspace (which deletes a character), return/enter, and space. To draw a backspace gesture just draw a line going from the right side of the screen to the left side. The gesture for return/enter is a diagonal line from the top right to bottom left. Finally a space is a straight line from the left to the right.
Note that when xstroke is looking for gestures you might not be able to click or control the cursor as you normally would expect. To stop xstroke's gesture recognition carefully press the xstroke pencil icon again until the βabc' text disappears. I've found this process can be a little finicky as the icon is very small and any movement will be interpreted as a gesture. Use a light touch and try a few times to click the icon.
If you get stuck completely and can't disable xstroke by clicking the icon, connect to the Raspberry Pi in a terminal/SSH connection and run βkillall xstroke' (without quotes) to force xstroke to quit. The normal way to stop xstroke is to navigate to theΒ AccessoriesΒ ->Β XStroke KillΒ command, but you might not be able to do that if xstroke is listening for gesture input.
Have fun using xstroke to control your Pi by writing gestures on the PiTFT screen!
Downloads
- The latest kernel fork that adds all the TFT, touchscreen, and other addons is here on github
- Datasheet for the βraw' 2.8β³ TFT display
- Original 2.8β³ PiTFT EagleCAD PCB Files on GitHub
- PiTFT Plus 2.8β³ EagleCAD PCB Files on GitHub
- PiTFT Plus 3.2β³ EagleCAD PCB Files on GitHub
- Fritzing Files in the Adafruit Fritzing Library
2.8β³ PiTFT Plus Schematic & Layout
For the Pi B+ & Pi 2 version (2Γ20 header)
PiTFT 3.2β³ Plus Schematic
Original PiTFT 2.8β³ Schematic & Layout
For the Original Pi 1 version (2Γ13 header)