Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8013

Python • DIY touchpad with RPi.GPIO and gpiozero

$
0
0
I am having problems using a DIY touch sensor with both RPi.GPIO and gpiozero on a Raspberry Pi 4B running Trixie and completely up-to-date.

Code:

pi@zenobia:~ $ uname -aLinux zenobia 6.12.62+rpt-rpi-v8 #1 SMP PREEMPT Debian 1:6.12.62-1+rpt1 (2025-12-18) aarch64 GNU/Linuxpi@zenobia:~ $ sudo apt updateHit:1 http://deb.debian.org/debian trixie InReleaseHit:2 http://deb.debian.org/debian trixie-updates InReleaseHit:3 http://deb.debian.org/debian-security trixie-security InReleaseHit:4 http://archive.raspberrypi.com/debian trixie InRelease     All packages are up to date. 
Both a physical button and a DIYables touch sensor (which I believe is a TTP223) behave as expected in the following code; but a coin, held by a jumper with an alligator clip and connected to GPIO25, with another jumper connecting GPIO25 to ground, doesn't behave the same way as the touch sensor but instead behaves differently when used with RPi.GPIO or gpiozero.

First, here's the RPi.GPIO example.

Code:

import RPi.GPIO as GPIOfrom signal import pauseGPIO.setmode(GPIO.BCM)BUTTON = 12TOUCH_PAD = 18COIN = 25GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)GPIO.setup(TOUCH_PAD, GPIO.IN)GPIO.setup(COIN, GPIO.IN)def BUTTON_PRESSED(channel):if not GPIO.input(channel):print(f"Encoder button on GPIO{BUTTON} pressed")else:print(f"Encoder button on GPIO{BUTTON} released")    def TOUCH_PAD_TOUCHED(channel):if GPIO.input(channel):print(f"DIYables sensor on GPIO{TOUCH_PAD} touched")else:print(f"DIYables sensor on GPIO{TOUCH_PAD} released")    def COIN_TOUCHED(channel):if GPIO.input(channel):print(f"€0.10 on GPIO{COIN} touched")else:print(f"€0.10 on GPIO{COIN} released")    GPIO.add_event_detect(BUTTON, GPIO.BOTH, callback=BUTTON_PRESSED, bouncetime=200)GPIO.add_event_detect(TOUCH_PAD, GPIO.BOTH, callback=TOUCH_PAD_TOUCHED, bouncetime=200)GPIO.add_event_detect(COIN, GPIO.BOTH, callback=COIN_TOUCHED, bouncetime=200)input("Test and then\nHit RETURN to close\n\n")GPIO.cleanup()
When I touch the coin there is no output, but when I then release (lift my finger) the

Code:

print(f"€0.10 on GPIO{COIN} released")
is executed and the script continues to run and from then on the code for the button and the sensor continue to work but nothing happens when I touch and release the coin. If I change

Code:

COIN, GPIO.BOTH
to

Code:

COIN, GPIO.RISING
or

Code:

COIN, GPIO.FALLING
nothing happens on a finger press, but on release the

Code:

print(f"€0.10 on GPIO{COIN} released")
is executed and the script continues to run with the button and sensor working and the coin only reporting the release. Increasing the bouncetime only delays the execution of the print line.

Trying to get the same effects with gpiozero I tried

Code:

from gpiozero import Buttonfrom signal import pauseimport timeBUTTON_PIN = 12TOUCH_PIN = 18 # DIYables sensor TTP223COIN_PIN = 25 # 50 ctvs or €0.10 coinbutton = Button(BUTTON_PIN)touch_sensor = Button(TOUCH_PIN, pull_up=None, active_state=True) coin_sensor = Button(COIN_PIN, pull_up=None, active_state=True)def sensor_touched():    print(f"Touch on sensor detected!")    def sensor_not_touched():    print(f"Release on sensor detected.")    def coin_touched():print(f"Touch on coin detected!")def coin_not_touched():print(f"Release on coin detected.")    def button_pressed():    print(f"Button press detected!")def button_not_pressed():    print(f"Button release detected.")touch_sensor.when_pressed = sensor_touchedtouch_sensor.when_released = sensor_not_touchedcoin_sensor.when_pressed = coin_touchedcoin_sensor.when_released = coin_not_touchedbutton.when_pressed = button_pressedbutton.when_released = button_not_pressedpause()
The button and touch sensor work as expected, but now a touch on the coin causes a continous stream of
Touch on coin detected!
Release on coin detected.
Touch on coin detected!
Release on coin detected.
until release. The number of repeated doublets is random, sometimes it continues for a bit after release, sometimes not. Adding a bounce_time=0.1 to the coin_sensor instantation causes the script to do nothing on a coin press and including time.sleep(n) statements just increases the time between prints.

Statistics: Posted by rquint — Tue Feb 24, 2026 1:23 am



Viewing all articles
Browse latest Browse all 8013

Trending Articles