Skip to content

Commit 545a8cf

Browse files
authored
Merge pull request #128 from RaspberryPiFoundation/feature/TouchSensor
Feature/TouchSensor
2 parents 1261466 + d9a4c5b commit 545a8cf

File tree

7 files changed

+73
-0
lines changed

7 files changed

+73
-0
lines changed

docs/api.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ MotionSensor
117117
:inherited-members:
118118
:members:
119119

120+
TouchSensor
121+
-----------
122+
123+
.. autoclass:: TouchSensor
124+
:show-inheritance:
125+
:inherited-members:
126+
:members:
127+
120128
Switch
121129
------
122130

docs/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ Change log
77
-----------
88

99
+ Introduced ``Stepper`` class for stepper motors
10+
+ Introduced ``TouchSensor`` class for capacitive touch sensors
11+
+ Updated tests and documentation
12+
+ Other minor bug fixes
13+
1014

1115
0.5.2 - 2025-11-26
1216
-----------

docs/examples/touch_sensor.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from picozero import TouchSensor, pico_led
2+
from time import sleep
3+
4+
# Capacitive touch sensor output connected to pin 2
5+
touch = TouchSensor(2)
6+
7+
while True:
8+
if touch.is_touched:
9+
pico_led.on()
10+
else:
11+
pico_led.off()
12+
sleep(0.1)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from picozero import TouchSensor, pico_led
2+
from time import sleep
3+
4+
touch = TouchSensor(2)
5+
6+
touch.when_touch_starts = pico_led.on
7+
touch.when_touch_ends = pico_led.off

docs/recipes.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,17 @@ Turn the :obj:`pico_led` on when a :class:`Button` is pressed and off when it is
145145

146146
.. literalinclude:: examples/button_led.py
147147

148+
Touch sensor
149+
------------
150+
151+
Detect touch using a capacitive touch sensor:
152+
153+
.. literalinclude:: examples/touch_sensor.py
154+
155+
Use callbacks to respond to touch events:
156+
157+
.. literalinclude:: examples/touch_sensor_callbacks.py
158+
148159
Motion sensor
149160
-------------
150161

picozero/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
Switch,
2626
Button,
2727
MotionSensor,
28+
TouchSensor,
2829
AnalogInputDevice,
2930
Potentiometer,
3031
Pot,

picozero/picozero.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2359,6 +2359,36 @@ def __init__(self, pin, pull_up=False, bounce_time=1.00):
23592359
MotionSensor.when_no_motion = MotionSensor.when_deactivated
23602360

23612361

2362+
class TouchSensor(Button):
2363+
"""
2364+
Represents a capacitive touch sensor (e.g. TTP223)
2365+
2366+
:param int pin:
2367+
The pin that the capacitive touch sensor is connected to.
2368+
2369+
:param bool pull_up:
2370+
If :data:`True`, the device will be pulled up to
2371+
HIGH. If :data:`False` (the default), the device will be pulled down to LOW.
2372+
Most capacitive touch sensors work with pull_up=False.
2373+
2374+
:param float bounce_time:
2375+
The bounce time for the device. If set, the device will ignore
2376+
any touch events that happen within the bounce time after a
2377+
touch event. This is useful to prevent false triggers from
2378+
electrical noise or multiple rapid touches.
2379+
Defaults to 0.02 seconds.
2380+
"""
2381+
2382+
def __init__(self, pin, pull_up=False, bounce_time=0.02):
2383+
super().__init__(pin=pin, pull_up=pull_up, bounce_time=bounce_time)
2384+
2385+
2386+
TouchSensor.is_touched = TouchSensor.is_active
2387+
TouchSensor.is_not_touched = TouchSensor.is_inactive
2388+
TouchSensor.when_touch_starts = TouchSensor.when_activated
2389+
TouchSensor.when_touch_ends = TouchSensor.when_deactivated
2390+
2391+
23622392
class AnalogInputDevice(InputDevice, PinMixin):
23632393
"""
23642394
Represents a generic input device with analogue functionality, e.g.

0 commit comments

Comments
 (0)