neovanmatix
Posts: 3
Joined: Wed Jan 08, 2014 8:28 am

Adafruit PiTFT - How to access these tactile Buttons?

Wed Jan 08, 2014 8:39 am

Hi there,
i've bought an PiTFT from Adafruit.
The TFT has 4 tactile buttons, that are connected to the GPIOs 23, 22, 21 and 18.
In the circuit diagram i've found a section that shows, how the tactile buttons are connected:
GPIO 23,22,21,18 (RPI-side) -> BUTTON1-4 -> GROUND.
http://learn.adafruit.com/system/assets ... 1387563188

My problem is, that i can't recognize a button press.
I've wrote a little python3 script, that should read out the status of a button and print a message.

Code: Select all

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

GPIO.setup(22,GPIO.IN)
GPIO.setup(21,GPIO.IN)
GPIO.setup(18,GPIO.IN)

while True:
    if GPIO.input(22):
        print("22 gedrückt!")
    if GPIO.input(21):
        print("21 gedrückt!")
    if GPIO.input(18):
        print("18 gedrückt!")

GPIO.cleanup()
I've played arround with the param pull_up_down and with the PIN-Settings and BCM/BOARD, but the only thing that seems to work is GPIO 18 and the buttonpress:
I start the above script as root and touch (dont press!) the most-right button; now i'm getting spammed with "18 gedrückt!". The other buttons dont give a print-Feedback.

Has someone a idea, how i could solve this?

User avatar
paddyg
Posts: 2541
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Adafruit PiTFT - How to access these tactile Buttons?

Fri Jan 10, 2014 5:33 pm

Suspicious that you don't have to press the button, that sounds like electrical field on a pin that's not tied up or down properly. Can't really tell how it's wired but it sounds like the buttons join the pins to 0V when pressed so I would set a pull up and test for when they read 0 volts i.e

Code: Select all

GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
    if not(GPIO.input(22)):
        print("22 gedrückt!")
    if not(GPIO.input(21)):
        print("21 gedrückt!")
    if not(GPIO.input(18)):
        print("18 gedrückt!")
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

neovanmatix
Posts: 3
Joined: Wed Jan 08, 2014 8:28 am

Re: Adafruit PiTFT - How to access these tactile Buttons?

Sat Jan 11, 2014 3:27 pm

Hi,
thanks for your answer.
I've only one problem: I can't recognize GPIO 21.
I've searched a lil bit, and it seems that RPI rev 2.0 has no internal pullup/down-resistor for GPIO 21.
Any idea, how i could get around?

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Adafruit PiTFT - How to access these tactile Buttons?

Sat Jan 11, 2014 3:48 pm

neovanmatix wrote:Hi,
thanks for your answer.
I've only one problem: I can't recognize GPIO 21.
I've searched a lil bit, and it seems that RPI rev 2.0 has no internal pullup/down-resistor for GPIO 21.
Any idea, how i could get around?
Try using 27

Rev 2 Pi has GPIO 27 on the pin where Rev 1 has 21
Alex Eames RasPi.TV, RasP.iO

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Adafruit PiTFT - How to access these tactile Buttons?

Sat Feb 15, 2014 12:18 pm

I've made a short python class for the buttons on the PiTFT screen: https://github.com/elParaguayo/RPI-Info ... tftgpio.py

It should work with rev1 and rev2 boards (default is rev2) by setting the appropriate flag when initialising the class. It should also allow you to control the backlight on the screen.

You could then do something like this:

Code: Select all

from pitftgpio import PiTFT_GPIO

pitft = PiTFT_GPIO()

while True:
    if pitft.Button1:
        print "Button 1 pressed - screen off"
        pitftf.Backlight(False)
    if pitft.Button2:
        print "Button 2 pressed - screen on"
        pitft.Backlight(True) 
    if pitft.Button3:
        print "Button 3 pressed"
    if pitft.Button4:
        print "Button 4 pressed"
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

fhlipZero
Posts: 35
Joined: Tue May 29, 2012 11:50 pm

Re: Adafruit PiTFT - How to access these tactile Buttons?

Tue May 27, 2014 8:43 pm

Code: Select all

from pitftgpio import PiTFT_GPIO

pitft = PiTFT_GPIO()

while True:
    if pitft.Button1:
        print "Button 1 pressed - screen off"
        pitft.Backlight(False)
    if pitft.Button2:
        print "Button 2 pressed - screen on"
        pitft.Backlight(True) 
    if pitft.Button3:
        print "Button 3 pressed"
    if pitft.Button4:
        print "Button 4 pressed"
correction in your code, under button 1 pressed there is an extra f after tft, thought id make sure no one else had this issue :)

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Adafruit PiTFT - How to access these tactile Buttons?

Tue May 27, 2014 9:22 pm

Indeed. Well spotted!

You can tell I didn't run that code myself!
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

e024576
Posts: 9
Joined: Sun Feb 10, 2013 6:05 pm

Re: Adafruit PiTFT - How to access these tactile Buttons?

Sat Oct 15, 2016 12:29 am

Pitftgpio module looks like what I did. I tried to install with PIP, but that didn't work. Any suggestions?

Paul

DirkS
Posts: 10363
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Adafruit PiTFT - How to access these tactile Buttons?

Sat Oct 15, 2016 4:53 am

e024576 wrote:Pitftgpio module looks like what I did. I tried to install with PIP, but that didn't work. Any suggestions?
elP's module is not available through pip.
The simplest way of making it available is to download the py file directly from github and then put it in the same directory as your python code.
You can then use the same import statement as in the code above

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Adafruit PiTFT - How to access these tactile Buttons?

Sat Oct 15, 2016 8:51 am

Wow. I'd forgotten I wrote that code!

Hope it still works ok.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

jokre
Posts: 1
Joined: Mon Jan 21, 2019 8:18 pm

Re: Adafruit PiTFT - How to access these tactile Buttons?

Mon Jan 21, 2019 8:46 pm

I'm in need for some input on how to get the backlight on/off to work.

I have implemented the python code as suggested in this thread and it works, but just partly.

I start the Python script and it then sits waiting for input. When I press the tactile buttons on the PiTFT screen shield I get response which tells me that it can't be anything wrong with getting the Python code to interact with the hardware. But... the backlight can't for some reason be controlled. Nothing happens when pressing button 1 and 2 (other than the sample text that is echoed to the screen). I've tried running Python with and without sudo.

Return to “Python”