Page 1 of 1

Pi 3 + PiTFT: Program 4 Buttons

Posted: Mon Apr 11, 2016 12:12 pm
by andrewlbrown
I recently purchased a new Raspberry Pi 3 and the 2.8in Capacitive PiTFT from Adafruit. I also picked up their case that includes the 4 "buttons" to the side of the display.

PiTFT product page: https://www.adafruit.com/products/2423
Case product page: https://www.adafruit.com/products/2807

What I'm trying to accomplish is simple, I've been doing quite a bit of reading online though, and everyone seems to do it differently. I simply want to program the buttons as follows:

See here (https://goo.gl/wfypur) for the GPIO numbers referenced below.

#27 - [call this Button 1] - start X session on PiTFT
#23 - [Button 2] - return to terminal session on PiTFT
#22 - [Button 3] - shutdown
#17 - [Button 4] - reboot

I have been reading that you need to be careful in programming the GPIO ports so as not to damage the Pi itself or corrupt your SD card.

After some messing around, I've ended up with the following, but it doesn't seem to work correctly (and it's only for one button...as far as I've made it). I'm only able to push the button and have it start X if I do so within a few seconds of executing the command. It starts X automatically if I run it at startup.

Code: Select all

import RPi.GPIO as GPIO
import subprocess

GPIO.setmode(GPIO.BCM)
GPIO.setup(27,GPIO.IN)

if GPIO.input(27):
    print('Button not pushed!')
else:
    subprocess.call("FRAMEBUFFER=/dev/fb1 startx", shell=True)

while GPIO.input(27) == GPIO.LOW:
    time.sleep(0.01)  # wait 10 ms to give CPU chance to do other things

GPIO.cleanup()
Any input/suggestions are greatly appreciated it; I'm mainly focused right now on getting Button 1 working correctly and will move on to the others from there.

Re: Pi 3 + PiTFT: Program 4 Buttons

Posted: Mon Apr 11, 2016 3:11 pm
by Davies
without knowing your circuitry it may be that you need to use GPIO.setup(27, GPIO.IN, pull_up_down = GPIO.PUD_UP) if your sending your gpio low or PUD_DOWN if your sending to high