adafruit pitft 3.5 touch screen for raspberry...

57
Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi Created by lady ada Last updated on 2016-09-30 01:57:06 PM UTC

Upload: vuonglien

Post on 30-Mar-2018

245 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

Adafruit PiTFT 3.5" Touch Screen for Raspberry PiCreated by lady ada

Last updated on 2016-09-30 01:57:06 PM UTC

Page 2: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

24888

13141422222527293235363941414244444548484950515454

Guide Contents

Guide ContentsOverviewEasy InstallReady to go imageDIY Installer scriptDetailed InstallBefore you startDownload & Install KernelDetailed CalibrationSetting up the TouchscreenAutoMagic Calibration ScriptManual CalibrationX CalibrationDetailed Console UseTurn off Console BlankingDisplaying ImagesUsing FBCPBacklightOn / Off Using STMPE GPIOPWM Backlight Control with GPIO 18PiTFT Pygame TipsInstall pip & pygameEnsure you are running SDL 1.2More TipsMaking it easier to click icons in XRight-click on a touchscreenGesture InputFAQDownloadsLayout and Schematic for PiTFT Plus 3.5"

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 2 of 57

Page 3: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

55Layout and Schematic for original PiTFT 3.5"

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 3 of 57

Page 4: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

Overview

Is this not the cutest, little display for the Raspberry Pi? It features a 3.5" display with480x320 16-bit color pixels and a resistive touch overlay, but is only slightly larger than ourpopular original (http://adafru.it/dDE). The plate uses the high speed SPI interface on the Piand 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!

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 4 of 57

Page 5: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

It's designed to fit nicely onto the Pi Model A or B but also works perfectly fine with theModel B+ as long as you don't mind the PCB overhangs the USB ports by 5mm.

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 5 of 57

Page 6: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

Uses the hardware SPI pins (SCK, MOSI, MISO, CE0, CE1) as well as GPIO #25 and #24.GPIO #18 can be used to PWM dim the backlight if you like. All other GPIO are unused.There's a 2x13 header on the bottom, you can connect a standard Pi GPIO cable to it touse any of the other pins ask you like

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 6 of 57

Page 7: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

Best of all, it comes fully assembled and ready to plug into your Pi! You can use this as adisplay for running the X interface, or pygame. You can also have an HDMI displayseperately connected.

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 7 of 57

Page 8: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

Easy InstallThe PiTFT requires kernel support and a couple other things to make it a nice stand-alonedisplay. We have a detailed step-by-step setup for hackers who want to tweak, customizeor understand the PiTFT setup. If you just want to get going, check out the following foreasy-install instructions!

Ready to go imagef you want to start with a fresh image, we have two for Raspbian. There's the larger 'classicJessie' image that will boot into X by default, and requires a 8G image, it has a lot moresoftware 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 (http://adafru.it/aMW)

This image is customized for the Resistive Touch 3.5" TFT, also known as PID #2097! Notfor the 2.8" or 2.4" PiTFTs, PID #1601 or #1983Download Jessie-based PiTFT 3.5" Resistive Image for Pi 1, Pi 2 and Pi 3 (March 25,2016)http://adafru.it/mAbDownload Jessie Lite-based PiTFT 3.5" Resistive Image for Pi 1, Pi 2 and Pi 3 (March 25,2016)http://adafru.it/mAG

Older Images:

Raspbian Jessie 2015/09/24-based image (http://adafru.it/iDD)Raspbian Wheezy 2015/09/24-based image (http://adafru.it/idy)Raspbian 2014/09/09-based image (http://adafru.it/e10)Raspbian 2015/03/12 image (http://adafru.it/eUE)

The DIY Installer isn't working right now, please try the All-In-One image above - no ETAon why its not working, something to do with the latest Raspbian has changed. Thanks!

DIY Installer scriptIf you don't want to download an image, you can install our custom kernel and run ourinstallation package helper from inside your existing Raspbian install. It will configure your

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 8 of 57

Page 9: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

Pi for PiTFT joy

The helper is available for perusal here (http://adafru.it/eIn) if you are interested in how itworks

To download and run it, simply run the following commands:

curl -SLs https://apt.adafruit.com/add-pin | sudo bashsudo apt-get install raspberrypi-bootloadersudo apt-get install adafruit-pitft-helper

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

The next three do the actual download and installation, it'll take a while because there's alot of software to replace for PiTFT support.

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 9 of 57

Page 10: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

It's normal for the Pi to pause at this step for up to 20 minutes, theres a lot of kernelsoftware to replace

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 10 of 57

Page 11: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

OK now the kernel and helper are installed, all you have to do is run the helper which willconfigure the kernel device tree overlays and add the few configurations to make theconsole show up, etc.

sudo adafruit-pitft-helper -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 thePiTFT. Answer Y or N depending on your personal desires!

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 11 of 57

Page 12: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

That's it!

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

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 12 of 57

Page 13: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

Detailed InstallThe DIY Installer isn't working right now, please try the All-In-One image above - no ETAon why its not working, something to do with the latest Raspbian has changed. Thanks!If you've grabbed our Easy Install image, or use the script, this step is not required, it'salready done! This is just for advanced users who are curious on how to configure andcustomize the kernel install

In the next few steps we'll cover the detailed installation procedure. Chances are, youshould grab the Easy Install image or script. If you have some interest in the details of howwe install the PiTFT setup, read on!

In order to add support for the 3.5" TFT and touchscreen, we'll need to install a new LinuxKernel. Lucky for you, we created a kernel package that you can simply install over yourcurrent Raspbian (or Raspbian-derived) install instead of needing a whole new image. This

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 13 of 57

Page 14: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

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 withArch or other Linux flavors. As Raspbian is the official OS for the Pi, that's the only Linuxwe will support! Others can recompile their own kernel using our github commits (http://adafru.it/aPa)but we have no tutorial or support or plans for such.

Before you startYou'll need a working install of Raspbian with network access. If you need help getting thatfar, check out our collection of Pi tutorials (http://adafru.it/aWq).

We'll be doing this from a console cable connection, but you can just as easily do it from thedirect 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 properTerminal - use ssh, a console cable, or the main text console (on a TV). The WebIDEconsole may not work.

Download & Install KernelThe only way we're distributing the PiTFT kernel packages right now is thruapt.adafruit.com so you'll still need to run:

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

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

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 14 of 57

Page 15: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

Then install the kernel with

sudo apt-get install raspberrypi-bootloader

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 15 of 57

Page 16: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

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:

[pi1]device_tree=bcm2708-rpi-b-plus.dtb[pi2]device_tree=bcm2709-rpi-2-b.dtb[all]dtparam=spi=ondtparam=i2c1=ondtparam=i2c_arm=ondtoverlay=pitft35r,rotate=90,speed=42000000,fps=20

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 16 of 57

Page 17: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

The rotate= variable tells the driver to rotate the screen 0 90 180 or 270 degrees. 0 is portrait, with the bottom near theUSB jacks90 is landscape, with the bottom of the screen near the headphone jack180 is portrait, with the top near the USB jacks270 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 agood 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 thescreen 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, placethe 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 turnblack, that means that likely there's something up with your connection or kernel install.

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 17 of 57

Page 18: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

Solder anything that needs resoldering!

Now that you're rebooted, log back in on the console/TV/SSH. There's nothing displayed onthe 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/fb1startx

You should see the Pi desktop show up on the TFT! Congrats, you've completed the firsttest 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 listwith

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 18 of 57

Page 19: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

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 rundmesg in the console window after logging in) you will see the modules install. Look inparticular for the STMPE610 detection and the HX5387D screen frequency as highlightedhere

We can set up the touchscreen for rotate=90 configuration by doing the following (for more

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 19 of 57

Page 20: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

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.dsudo 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 120 200 3900" 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 canmake it a default state by editing your profile file: sudo nano ~/.profile and adding

export FRAMEBUFFER=/dev/fb1

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 20 of 57

Page 21: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

near the top and saving the file. Then reboot to reload the profile file. It will now alwaysassume you want to use /dev/fb1

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 21 of 57

Page 22: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

Detailed CalibrationIf you've grabbed our Easy Install image, or use the script, this step is not required, it'salready done! This is just for advanced users who are curious on how to configure andcustomize the touchscreen

Setting up the TouchscreenNow that the screen is working nicely, we'll take care of the touchscreen. There's just a bitof 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 eventXname of the device will change a lot and its annoying to figure out what its called dependingon whether you have a keyboard or other mouse installed.

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 22 of 57

Page 23: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

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/touchscreenIt should point to eventX where X is some number, that number will be different on differentsetups 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

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 23 of 57

Page 24: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

There are some tools we can use to calibrate & debug the touchscreen. Install the "eventtest" and "touchscreen library" packages with

sudo apt-get install evtest tslib libts-bin

Now you can use some tools such as sudo evtest /dev/input/touchscreen which will letyou see touchscreen events in real time, press on the touchscreen to see the reports.

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 24 of 57

Page 25: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

AutoMagic Calibration Script

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 25 of 57

Page 26: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

If you rotate the display you need to recalibrate the touchscreen to work with the newscreen orientation. You can manually run the calibration processes in the next section, oryou can run a small Python script which will automatically set a default touchscreencalibration based on the screen orientation.

This helper is automatically installed for you but if you'd like you can check it out here ongithub (http://adafru.it/eIu)

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

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

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 26 of 57

Page 27: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

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

Manual CalibrationIf you rotate the display you have some other setup where you need to carefully calibrateyou can do it 'manually'

You will want to calibrate the screen once but shouldn't have to do it more than that. We'llbegin by calibrating on the command line by running

sudo TSLIB_FBDEVICE=/dev/fb1TSLIB_TSDEVICE=/dev/input/touchscreen ts_calibrate

follow the directions on the screen, touching each point. Using a stylus is suggested so youget a precise touch. Don't use something metal, plastic only!

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 27 of 57

Page 28: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

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

Next you can run sudo TSLIB_FBDEVICE=/dev/fb1TSLIB_TSDEVICE=/dev/input/touchscreen ts_test which will let you draw-test the touchscreen. Go back and re-calibrate if you feel the screen isn't precise enough!

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 28 of 57

Page 29: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

X CalibrationYou can also calibrate the X input system but you have to use a different program calledxinput_calibrator

You can do this if the calibration on the screen isnt to your liking or any time you change therotate=XX module settings for the screen. Since the screen and touch driver arecompletely 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:

wget http://adafruit-download.s3.amazonaws.com/xinput-calibrator_0.7.5-1_armhf.debsudo dpkg -i -B xinput-calibrator_0.7.5-1_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 andthen opening up the terminal program and running the xinput_calibrator command (whichis challenging to do on such a small screen) OR you can do what we do which is run startxin a SSH/Terminal shell and then run the xinput_calibrator from the same shell, whichrequires the following command order:

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

Follow the directions on screen

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 29 of 57

Page 30: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

Once complete you'll get something like:

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

Section "InputClass" Identifier "calibration" MatchProduct "stmpe-ts" Option "Calibration" "119 3736 3850 174"# Option "SwapAxes" "1"EndSection

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

Depending on the 'rotation' of the screen, when you do this calibration, you mayneed to comment out the SwapAxes part with a # and/or swap the numbers around so

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 30 of 57

Page 31: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

looks like:

Option "Calibration" "119 3736 3850 174"

to

Option "Calibration" "3736 119 174 3850"

Your touchscreen is now super calibrated, hurrah!

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 31 of 57

Page 32: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

Detailed Console UseIf you've grabbed our Easy Install image, or use the script, this step is not required, it'salready done! This is just for advanced users who are curious on how to configure andcustomize the console

One fun thing you can do with the display is have it as your main console instead of theHDMI/TV output. Even though it is small, with a good font you can get 40 x 60 of text. Formore details, check out https://github.com/notro/fbtft/wiki/Boot-console (http://adafru.it/cXQ)

First up, we'll update the boot configuration file to use the TFT framebuffer /dev/fb1 insteadof 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.

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 32 of 57

Page 33: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

Note that the kernel has to load up the display driver module before it can displayanything on it so you won't get the rainbow screen, a NooBs prompt, or a big chunkof the kernel details since the module is loaded fairly late in the boot process.

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

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 33 of 57

Page 34: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 34 of 57

Page 35: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

Turn off Console BlankingYou 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 blanktime to 0 (which turns it off)

BLANK_TIME=30

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 35 of 57

Page 36: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

Displaying Images

You can display every day images such as GIFs, JPGs, BMPs, etc on the screen. To dothis we'll install fbi which is the frame buffer image viewer (not to be confused with the FBIagency!)

sudo apt-get install fbi will install it

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 36 of 57

Page 37: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

Grab our lovely wallpaper with

wget http://adafruit-download.s3.amazonaws.com/adapiluv480x320.png (http://adafru.it/cXU)

and view it with

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

That's it!

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 37 of 57

Page 38: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 38 of 57

Page 39: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

Using FBCP

The Ideal: Adafruit’s PiTFT displays are razor sharp. Whereas small composite screens onthe Raspberry Pi usually require some video scaling (resulting in blurriness), PiTFT usesthe GPIO header, digitally controlled pixel-by-pixel for a rock steady image. Though not alot 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 thegraphics accelerator. Many games and emulators depend on the GPU for performancegains. So the PiTFT has traditionally been limited to just a subset of specially-compiledemulators 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 openthe 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!

Click here to go to our FBCP tutorial!http://adafru.it/fbe

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 39 of 57

Page 40: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 40 of 57

Page 41: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

BacklightThe backlight of the 3.5" display has 6 LEDs in a row, and we use a boost converter to getthe 5V from the Pi up to the ~20V needed to light up all the LEDs. By default, thebacklight's on...but you can control it in two ways.

On / Off Using STMPE GPIOFirst option is to just turn it on and off using the extra GPIO created by the touchscreendriver

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

Once you verify that you see GPIO #508, then you can set it to an output, this will turn offthe 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 Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 41 of 57

Page 42: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

PWM Backlight Control with GPIO 18If you want more precise control, you can use the PWM output on GPIO 18. There's pythoncode for controlling the PWM but you can also just use the kernel module and shellcommands.

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

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 1000Hz frequency, set the output to 100 (out of 1023, so dim!), set the output to 1023 (out of1023, nearly all the way on) and 0 (off)

gpio -g mode 18 pwmgpio pwmc 1000gpio -g pwm 18 100gpio -g pwm 18 1023gpio -g pwm 18 0

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

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 42 of 57

Page 43: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 43 of 57

Page 44: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

PiTFT Pygame TipsSince the PiTFT screen is fairly small, you may need to write custom UI programs. Pygameis the easiest way by far to do this.

Jeremy Blythe has an excellent tutorial here on getting started. (http://adafru.it/kD2)

However, before you follow that link you'll want to set up pygame for the best compatibility:

Install pip & pygameInstall Pip: sudo apt-get install python-pip

Install Pygame: sudo apt-get install python-pygame

(this will take a while)

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 44 of 57

Page 45: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

Ensure you are running SDL 1.2SDL 2.x and SDL 1.2.15-10 have some serious incompatibilities with touchscreen. You canforce SDL 1.2 by running a script. (Thanks to heine in the forums! (http://adafru.it/fH3))

Edit a new file with sudo nano installsdl.sh and paste in the following text:

#!/bin/bash #enable wheezy package sourcesecho "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 packageecho "Package: libsdl1.2debianPin: release n=jessiePin-Priority: -10Package: libsdl1.2debianPin: release n=wheezyPin-Priority: 900" > /etc/apt/preferences.d/libsdl

#install

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 45 of 57

Page 46: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

apt-get updateapt-get -y --force-yes install libsdl1.2debian/wheezy

run

sudo chmod +x installsdl.sh

sudo ./installsdl.sh

it will force install SDL 1.2

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 46 of 57

Page 47: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

OK now you can continue with pygame

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 47 of 57

Page 48: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

More Tips

Making it easier to click icons in XIf you want to double-click on icons to launch something in X you may find it annoying toget it to work right. In LXDE you can simply set it up so that you only need to single clickinstead 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!)

Then under the Edit menu, select Preferences

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

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 48 of 57

Page 49: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

Right-click on a touchscreenObviously if you have a touchscreen, it cannot tell what finger you are pressing with. Thismeans 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.

(Thx adamaddin! (http://adafru.it/fH3))

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 49 of 57

Page 50: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

Gesture InputYou can use the same tutorial we have for the 2.8" TFT if you'd like to use gestureinput! (http://adafru.it/dJ1)

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 50 of 57

Page 51: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

FAQI have a question that isn't answered here

Check out the 2.8" resistive PiTFT FAQ (http://adafru.it/dJ2) for an answer to manycommon questions.

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, forexample:FRAMEBUFFER=/dev/fb0 startxwill 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 &andFRAMEBUFFER=/dev/fb1 startx &wind up showing the GUI on your PiTFT, enter the following instruction from the commandline:

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.

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 change the SPI frequency (overclock the display) by editing /boot/config.txt andchanging 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, thePi only supports a fixed number of SPI frequencies. So tweaking the number a little won'tdo anything. The kernel will round the number to the closest value. You will always getfrequencies that are 250MHz divided by an even number. Here's the only SPI frequenciesthis kernel supports

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 51 of 57

Page 52: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

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 onlyget about 42MHz because it gets rounded down. We tested this display nicely with 32MHzand 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 fortradeoffs in performance and speed. Reboot after each edit to make sure the settings areloaded properly. There's a trade off that if you ask for higher FPS you're going to load thekernel more because it's trying to keep the display updated.

How can I take screenshots of the display?

We took the screenshots for this tutorial with (http://adafru.it/diV)fbgra (http://adafru.it/diV)b (http://adafru.it/diV)

wget http://fbgrab.monells.se/fbgrab-1.2.tar.gz (http://adafru.it/diW)tar -zxvf fbgrab*gzcd fbgrab/make./fbgrab screenshot.png

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 52 of 57

Page 53: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 53 of 57

Page 54: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

DownloadsThe latest kernel fork that adds all the TFT, touchscreen, and other addons is here ongithub (http://adafru.it/aPa)Datasheet for the controller chip (http://adafru.it/dQQ)Datasheet for the 'raw' 3.5" TFT display (http://adafru.it/dR4)PCB files on GitHub (http://adafru.it/rEC)

Layout and Schematic for PiTFT Plus 3.5"This is the newer PID 2441

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 54 of 57

Page 55: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

Layout and Schematic for original PiTFT

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 55 of 57

Page 56: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

3.5"This is the original PID #2097 version

© Adafruit Industries https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi

Page 56 of 57

Page 57: Adafruit PiTFT 3.5 Touch Screen for Raspberry Pidocs-europe.electrocomponents.com/webdocs/1534/... · Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi ... This image is customized

© Adafruit Industries Last Updated: 2016-09-30 01:57:04 PM UTC Page 57 of 57