What's Your IT Question?

Want to know more about this topic or about us? Contact us!

Adafruit PiTFT 3.5″ Touch Screen for Raspberry Pi

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 tutorials 

This image is customized for the RESISTIVE touch 3.5″ TFT, also known as PID #2097! Not for PID #1601 or #1983

DIY Installer script

If you don’t want to download an image, you can install our custom kernel and run our installation package helper from inside your existing Raspbian install. It will  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:

  1. curl SLs https://apt.adafruit.com/add | sudo bash
  2. sudo aptget install raspberrypibootloader
  3. sudo aptget install adafruitpitfthelper

The first command adds apt.adafruit.com to your repository list, so you can grab code directly from adafruit’s servers

adafruit_products_aptadd.png

The second two do the actual download and installation, it’ll take a while because there’s a lot of software to replace for PiTFT support

adafruit_products_1.png
adafruit_products_apt-get-install-raspberrypi-bootloader-2.png
adafruit_products_3.png
It’s normal for the Pi to pause at this step for up to 20 minutes, theres a lot of kernel software to replace
adafruit_products_apt-get-install-adafruit-pitft-helper.png

OK now the kernel and helper are 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.

  1. sudo adafruitpitfthelper t 35r

This will install the “3.5 inch Resistive” type of PiTFT into the current install.

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!

adafruit_products_console.png

That’s it!

Run sudo reboot to try out your fancy new PiTFT :)

Detailed Install

If you’ve grabbed our Easy Install image, or use the script, this step is not required, it’s already done! This is just for advanced users who are curious on how to configure and customize the kernel install

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!

adafruit_products_2097_top_ORIG.jpg

In order to add support for the 3.5″ 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 github commits 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 !

To run these all the setup and config commands you’ll need to be logged into a proper Terminal – use ssh, a console cable, or the main text console (on a TV). The WebIDE console may not work.

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:

  1. curl SLs https://apt.adafruit.com/add | sudo bash

To add apt.adafruit.com to your list of software sources

adafruit_products_aptadd.png

Then install the kernel with

  1. sudo aptget install raspberrypibootloader
adafruit_products_1.png
adafruit_products_2.png
adafruit_products_3.png

OK since you’re not going to run the helper, lets add the device tree overlay manually. Edit /boot/config.txt with

sudo nano /boot/config.txt

and add the following lines at the end:

  1. [pi1]
  2. device_tree=bcm2708rpibplus.dtb
  3. [pi2]
  4. device_tree=bcm2709rpi2b.dtb
  5. [all]
  6. dtparam=spi=on
  7. dtparam=i2c1=on
  8. dtparam=i2c_arm=on
  9. dtoverlay=pitft35r,rotate=90,speed=42000000,fps=20
adafruit_products_pi35config.png

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. 42MHz (42000000) is a good place to start but if your screen is acting funny, try taking it down to 16MHz (16000000especiallyif 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:

  1. sudo mv /usr/share/X11/xorg.conf.d/99fbturbo.conf ~
  2. export FRAMEBUFFER=/dev/fb1
  3. startx
adafruit_products_startx.png

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

adafruit_products_stmpe.png

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 HX5387D screen frequency as highlighted here

adafruit_products_hxdmesg.png

We can set up the touchscreen for rotate=90 configuration by doing the following (for more delicate calibration or for other rotate=XX values, see the next section)
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.

  1. Section “InputClass”
  2. Identifier “calibration”
  3. MatchProduct “stmpe-ts”
  4. Option “Calibration” “3800 120 200 3900”
  5. Option “SwapAxes” “1”
  6. EndSection
adafruit_products_xconf.png

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

adafruit_products_raspberry_pi_profile.png

Detailed Calibration

If you’ve grabbed our Easy Install image, or use the script, this step is not required, it’s already done! This is just for advanced users who are curious on how to configure and customize the touchscreen
adafruit_products_2097_demo_hand_ORIG.jpg

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”

adafruit_products_raspberry_pi_udev.png

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

adafruit_products_raspberry_pi_udevlink.png

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

adafruit_products_raspberry_pi_aptgetlibts.png

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.

adafruit_products_raspberry_pi_evtest1.png
adafruit_products_raspberry_pi_evtest2.png

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 run a small Python script which will automatically set a default touchscreen calibration based on the screen orientation.

This helper is automatically installed for you but if you’d like you can check it out here on github

Run it at the command line with sudo adafruit-pitft-touch-cal
it will try to figure out what display you have installed and the rotation it’s set up for

adafruit_products_touchcal.png

By default the script will attempt to read the screen orientation by examining the PiTFT module configuration with modprobe. If the script can read the orientation it will print out the current orientation, the current touchscreen calibration values, and the new touchscreen calibration values baesd on the current orientation. Before updating the calibration the script will ask you to confirm that you’d like to make the change. Press y and enter to confirm.

adafruit_products_xinputed.png

Try using this default calibration script to easily calibrate your touchscreen display. Note that the calibration values might not be exactly right for your display, but they should be close enough for most needs. If you need the most accurate touchscreen calibration, follow the steps in the next section to manually calibrate the touchscreen.

Manual Calibration

If you rotate the display 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!

adafruit_products_raspberry_pi_tscalibrate.png
You should see five crosshair targets. If you see less than that, the touchscreen probably generated multiple signals for a single touch, and you should try calibrating again.
adafruit_products_raspberry_pi_tscalib.png

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!

adafruit_products_raspberry_pi_tstest.png

X Calibration

You can also calibrate the X input system but you have to use a different program called xinput_calibrator

You can do this if the calibration on the screen isnt 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

Normally you’d have to compile it but we have a ready to go package for you so run:

  1. wget http://adafruit-download.s3.amazonaws.com/xinput-calibrator_0.7.5-1_armhf.deb
  2. sudo dpkg i B xinputcalibrator_0.7.51_armhf.deb

Before you start the xinput_calibrator you will need to delete the old calibration data so run

sudo rm /etc/X11/xorg.conf.d/99-calibration.conf

Before running startx and the calibrator – otherwise it gets really confused!
Now you’ll have to run the xcalibrator while also running X. You can do this by startx and then opening up the terminal program and running the xinput_calibrator command (which is challenging to do on such a small screen) OR you can do what we do which is run startx in a SSH/Terminal shell and then run the xinput_calibrator from the same shell, which requires the following command order:

 FRAMEBUFFER=/dev/fb1 startx &
DISPLAY=:0.0 xinput_calibrator

Follow the directions on screen

adafruit_products_raspberry_pi_xinput.png

Once complete you’ll get something like:

adafruit_products_raspberry_pi_calibrated.png

Run sudo nano /etc/X11/xorg.conf.d/99-calibration.conf and copy the

  1. Section “InputClass”
  2. Identifier “calibration”
  3. MatchProduct “stmpe-ts”
  4. Option “Calibration” “119 3736 3850 174”
  5. # Option “SwapAxes” “1”
  6. EndSection

or whatever you got, into there. You can quit X if you want by typing fg to bring that command into the foreground, and then Control-C to quit.

Depending on the ‘rotation’ of the screen, when you do this calibration, you may need to comment out the SwapAxes part with a # and/or swap the numbers around so looks like:

  • Option “Calibration” “119 3736 3850 174”

to

  • Option “Calibration” “3736 119 174 3850”

Your touchscreen is now super calibrated, hurrah!

Detailed Console Use

If you’ve grabbed our Easy Install image, or use the script, this step is not required, it’s already done! This is just for advanced users who are curious on how to configure and customize the console
adafruit_products_2097_demo_screen_05_ORIG.jpg

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 40 x 60 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.

adafruit_products_raspberry_pi_fbcon.png

I think the VGA8x8 font is a bit chunky, you probably want 12×6 which is what is shown in the photo above. To change the font, run sudo dpkg-reconfigure console-setup and go thru to select Terminus 6×12

adafruit_products_raspberry_pi_UTF-8.png
adafruit_products_raspberry_pi_guess.png
adafruit_products_raspberry_pi_Terminus.png
adafruit_products_raspberry_pi_Term6x12.png

Turn off Console Blanking

You may notice the console goes black after 30 minutes, this is a sort of ‘power saving’ or ‘screensaver’ feature. You can disable this by editing /etc/kbd/configand setting the blank time to 0 (which turns it off)

BLANK_TIME=30

Displaying Images

adafruit_products_adapiluv480x320.png

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

adafruit_products_raspberry_pi_fbi.png

Grab our lovely wallpaper with

 wget http://adafruit-download.s3.amazonaws.com/adapiluv480x320.png

and view it with

sudo fbi -T 2 -d /dev/fb1 -noverbose -a adapiluv480x320.png

That’s it!

adafruit_products_raspberry_pi_P1000082.jpg

Backlight

The backlight of the 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. By default, the backlight’s on…but you can control it in two ways.

On / Off Using STMPE GPIO

First option is to just turn it on and off using the extra GPIO created by the touchscreen driver

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

adafruit_products_exportgpio.png

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”

adafruit_products_backlit.png

PWM Backlight Control with GPIO 18

If you want more 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.

If you did the above commands, you’ll need to turn off the STMPE GPIO which overrides the PWM output. You only have to run this if you set GPIO508 to an output in the previous option

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)

  1. gpio g mode 18 pwm
  2. gpio pwmc 1000
  3. gpio g pwm 18 100
  4. gpio g pwm 18 1023
  5. gpio g pwm 18 0
adafruit_products_35pwm.png

Try other numbers, from 0 (off) to 1023 (all the way on)!

More Tips

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 and from the 2.8″ TFT, still figuring out how to screenshot the framebuffer!)

adafruit_products_raspberry_pi_filemanager.png

Then under the Edit menu, select Preferences

adafruit_products_raspberry_pi_filemanagerprefs.png

Then select Open files with single click and close the window (you’ll need to drag it over to get to the X button

adafruit_products_raspberry_pi_singleclick.png

Gesture Input

FAQ

How do I automatically boot to X windows on the PiTFT?

Check out the 2.8″ resistive PiTFT FAQ for an answer to this common question.

How can I 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!

If both
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/99fbturbo.conf ~

I’m tring to run startx and I get FATAL: Module g2d_23 not found.

don’t forget you have to remove the turbo file!
sudo mv /usr/share/X11/xorg.conf.d/99-fbturbo.conf ~

I want better performance and faster updates!

You can also change the SPI frequency (overclock the display) by editing /etc/modprobe.d/adafruit.conf and changing the options line to:

options fbtft_device name=adafruitrt35 rotate=90 frequency=64000000 fps=30

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 and it will update a bit faster

You can tweak fps (frames per second) from 20 to 60 and frequency up to 42MHz 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. You can try 62MHz SPI but you may find the image corrupted so, again, tradeoffs! :)

How can I take screenshots of the display?

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

adafruit_products_raspberry_pi_framegrab.png

Downloads

Layout and Schematic for PiTFT Plus 3.5″

This is the newer PID 2441

adafruit_products_35pitftplus.png
adafruit_products_pitftplusch.png

Layout and Schematic for original PiTFT 3.5″

This is the original PID #2097 version

adafruit_products_printinches.png
adafruit_products_schem.png