How to Set Up an Ada Fruit PiTFT for the Raspberry Pi
About 2 months ago I received my Adafruit PiTFT through the post, possibly the most excited I've ever been to see my postman. I also ordered the 4 tactile buttons to go with it, adding to my geeky buzz.
Foolishly I thought it would be a quick "Plug n' Play" unit, expecting to get my console showing on the little 2.8" screen in seconds. Not quite. I should have read the install guide before ordering!
You see, the PiTFT isn't the easiest thing to get running. In fact, have a quick search on-line and you'll see a mass of forum messages going back and forth trying to get certain parts working. Some can't get their desktop to show up, some can't get the buttons working, others struggle with the backlight control - and so on and so on. It's a bit of a fiddly one to say the least!
After a few attempts and a lot of Googling, I've got mine working perfectly (update - "almost perfectly") - so I'm here to show you exactly what I did, step by step, so that you don't have to go through the same headache as me.
My PiTFT...finally working!
Start with a Fresh Raspbian Image
If you've been having trouble getting things working perfectly on your PiTFT, this is probably the most important bit. I'm convinced that it's best to start with a fresh image, but your experiences may differ.
One of my first attempts to get the PiTFT working, was on an existing Raspbian install - one I had run many projects on. I'm not sure why, but I couldn't get the screen working on this - maybe I had done something to the settings somewhere. I had tried updating it first, but it still didn't work.
Next, I tried the custom PiTFT image on the AdaFruit site. This would surely work, I told myself. Whilst it did get the screen running, I had problems with the backlight GPIO #252 being recognised. I couldn't seem to fix this.
Eventually, I started with a clean image of Raspbian from the Raspberry Pi site. This, plus my other steps, worked for me - so if you can, do this first.
I'm not sure if it makes any difference, but with my fresh Raspbian image I took the following actions in the raspi-config screen:
- Expanded filesystem
- Enabled Camera
- Enabled SSH
- Enabled SPI
Despite it always being recommended, I didn't update the raspi-config tool or the Pi itself.
Set Up the Pi-TFT
Here's what I did to set the PiTFT up on my fresh Raspbian image. What you're doing here is installing a new 'kernel' (nothing to do with grain). You need internet access to download the files you need, so sort out an Ethernet or WiFi connection first.
Download Kernel Files
Head to a Terminal session either on screen or using something like SSH, and run the following command to make sure you're in the main directory:
cd
Then run these one by one, to download the Kernel files:
wget http://adafruit-download.s3.amazonaws.com/libraspberrypi-bin-adafruit.deb
wget http://adafruit-download.s3.amazonaws.com/libraspberrypi-dev-adafruit.deb
wget http://adafruit-download.s3.amazonaws.com/libraspberrypi-doc-adafruit.deb
wget http://adafruit-download.s3.amazonaws.com/libraspberrypi0-adafruit.deb
wget http://adafruit-download.s3.amazonaws.com/raspberrypi-bootloader-adafruit-112613.deb
Install Kernel Files
Now that you've downloaded the files you need, run this command to install the kernel:
sudo dpkg -i -B *.deb
This might take a minute or two, be patient with it.
Turn Off Accelerated X Framebuffer
You're following this guide, which means you should be using the latest image of Raspbian. With recent versions of raspbian you need to turn off the accelerated X framebuffer when using the PiTFT. To do this we move that file to your home directory, which means it has been removed but can still be put back later is needed. We use 'mv' to move files here - use the command below:
sudo mv /usr/share/X11/xorg.conf.d/99-fbturbo.conf ~
Now we need to reboot, so run the usual reboot command:
sudo reboot
You won't see anything on screen yet, don't panic.
Install the Screen Driver
Run the following commands to install the screen driver:
sudo modprobe spi-bcm2708
sudo modprobe fbtft_device name=adafruitts rotate=90
export FRAMEBUFFER=/dev/fb1
startx
That last command will start 'X' on your PiTFT - now I didn't know this before, but 'X' means the GUI...the desktop view for Raspbian. You learn something every day huh?
We're not finished yet, so calm down, and press 'Ctrl - C' on your keyboard to go back to the Terminal view. If you've SSH'd in, you don't have to do this as you will still have terminal view.
Setting the Modules to Auto-load
Open up the modules list with the following command:
sudo nano /etc/modules
Add these lines underneath the line already in there:
spi-bcm2708
fbtft_device
Once you've added them, hit 'Ctrl + x' to save (press 'Y' at the prompt to save the file with the same name).
Add Configuration Info to Modprobe
Open up the configuration file with this command:
sudo nano /etc/modprobe.d/adafruit.conf
Now just add this line:
options fbtft_device name=adafruitts rotate=90 frequency=32000000
Again, hit 'Ctrl + x' to save (press 'Y' at the prompt to save the file with the same name).
Starting the GUI ('X')
Most people will want to start the Raspberry Pi Desktop view regularly, but unfortunately the PiTFT requires a separate command before 'startx' ('FRAMEBUFFER=/dev/fb1 startx')
Luckily there's a way around this. Open up your profile file:
sudo nano ~/.profile
...and add this line above the other non-# lines:
export FRAMEBUFFER=/dev/fb1
You can now just use 'startx' to start the desktop.
Attention to Detail
Don't be fooled - you may think you're done, but there's a few things to tweak yet. For example, if you leave that PiTFT on for 30 minutes, the screen will go blank and you'll be wondering what went wrong. Also, you don't know which GPIO to use for those switches - and did I mention font size on the screen?
Let's sort these things out now.
Screen Time-out
As I mentioned above, the default setting is for the screen to turn off after 30 minutes, and I couldn't find a way to bring it back over SSH - very annoying. Use the following command to open the settings file:
sudo nano /etc/kbd/config
Then set 'BLANK_TIME' to '0' to stop the screen from timing out.
I also set POWERDOWN_TIME to '0' as it seemed like a good idea as the description made it seem like it shut down 'something' after 60 minutes (30+30). You may not need to do this, but I did. (UPDATE - try #'ing that POWERDOWN line out if you still experience time-outs)
Starting Terminal on Boot
If you want to go straight to the terminal (my preference), follow these steps...
First, tell the Pi to use the TFT Framebuffer instead of the HDMI one. Open up the cmdline file:
sudo nano /boot/cmdline.txt
There will already be a long line of text in this file - we're going to change it. Near the end of the text you'll find 'rootwait' - directly after this enter the following text:
fbcon=map:10 fbcon=font:VGA8x8
Exit and save the usual way, and you're done.
Font Size
The last step will get Terminal running on boot, but one of the many guides I used suggested changing the text size by doing the following...
Run the following command to get into console setup - it's a pretty blue screen:
sudo dpkg-reconfigure console-setup
- On the first screen select the appropriate encoding (UK should be UTF-8)
- At the next screen select 'choose optimal character set'
- Next select 'Terminus' as the font
- Then select '6x12 framebuffer only'
Have a play if you don't like that font, there are a load to choose from.
GPIO Buttons
The PiTFT comes with some built in options for button control (backlight and shut down), but I never really understood why as it's easy to start your own Python script on boot to control whatever you want - much more fun!
From left to right (buttons on the PiTFT), the GPIO numbering (GPIO.BCM) is as follows:
23
22
27
18
The GPIO pins feeding these buttons are set high by default, so to trigger them you have to tell your script to look for 'FALSE'...let me show you my script - copy it, tweak it, freak it - go for it! I'd love to hear what you've done with your PiTFT buttons.
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import os
#Set GPIO mode
GPIO.setmode(GPIO.BCM)
#Setup GPIO
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#Set up backlight GPIO
os.system("sudo sh -c 'echo 252 > /sys/class/gpio/export'")
#Give the system a quick break
time.sleep(0.5)
#Set the intitial counter value to zero
counter = 0
#var for the 'while' statement to keep it running
var = 1
#Main program
while var == 1:
if (GPIO.input(23) == False): #Backlight control
if (counter == 0):
os.system("sudo sh -c 'echo 'out' > /sys/class/gpio/gpio252/direction'")
counter = 1
print("counter now 1")
time.sleep(0.5)
elif (counter == 1) or (counter == 3):
os.system("sudo sh -c 'echo '1' > /sys/class/gpio/gpio252/value'")
counter = 2
print("counter now 2")
time.sleep(0.5)
elif (counter == 2):
os.system("sudo sh -c 'echo '0' > /sys/class/gpio/gpio252/value'")
counter = 3
print("counter now 3")
time.sleep(0.5)
if (GPIO.input(22) == False):
print("22 Working")
time.sleep(0.5)
if (GPIO.input(27) == False):
print("27 working")
time.sleep(0.5)
if (GPIO.input(18) == False): #Shutdown button
print("SHUTDOWN")
os.system("sudo halt")
GPIO.cleanup()
Resources
I used a few different guides to finally get my PiTFT working. I'm not confident that one single guide has the correct way of doing things, but here they are anyway - they contain more info such as calibrating your touchscreen and more:
Adafruit Guide - https://learn.adafruit.com/adafruit-pitft-28-inch-resistive-touchscreen-display-raspberry-pi/overview
Sandal.tw Guide - http://www.sandal.tw/upload/adafruit-pitft-28-inch-resistive-touchscreen-display-raspberry-pi.pdf
Continue reading here: 3.5 Touchscreen TFT Display for the Raspberry Pi
Was this article helpful?
Readers' Questions
-
Simon8 months ago
- Reply