Skip to content

Commit f3c1c6b

Browse files
committed
Add TouchSensor class for capacitive touch sensors
1 parent 6ea3150 commit f3c1c6b

File tree

5 files changed

+67
-9
lines changed

5 files changed

+67
-9
lines changed

docs/api.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ MotionSensor
109109
:inherited-members:
110110
:members:
111111

112+
TouchSensor
113+
-----------
114+
115+
.. autoclass:: TouchSensor
116+
:show-inheritance:
117+
:inherited-members:
118+
:members:
119+
112120
Switch
113121
------
114122

docs/examples/touch_sensor.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from picozero import TouchSensor
2+
from time import sleep
3+
4+
touch = TouchSensor(2)
5+
6+
print("Touch the sensor...")
7+
8+
while True:
9+
if touch.is_touched:
10+
print("Touch detected!")
11+
else:
12+
print("No touch")
13+
sleep(0.1)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from picozero import TouchSensor, pico_led
2+
from time import sleep
3+
4+
touch = TouchSensor(2)
5+
6+
# Set up event callbacks
7+
touch.when_touched = pico_led.on
8+
touch.when_touch_ends = pico_led.off
9+
10+
# Keep the program running
11+
try:
12+
while True:
13+
sleep(1)
14+
except KeyboardInterrupt:
15+
pico_led.off() # Make sure LED is off when exiting

picozero/__init__.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,36 @@
11
__name__ = "picozero"
22
__package__ = "picozero"
3-
__version__ = '0.4.2'
3+
__version__ = "0.4.2"
44
__author__ = "Raspberry Pi Foundation"
55

66
from .picozero import (
77
PWMChannelAlreadyInUse,
88
EventFailedScheduleQueueFull,
9-
109
pinout,
11-
1210
DigitalOutputDevice,
1311
DigitalLED,
1412
Buzzer,
1513
PWMOutputDevice,
1614
PWMLED,
1715
LED,
1816
pico_led,
19-
2017
PWMBuzzer,
2118
Speaker,
22-
2319
RGBLED,
2420
Motor,
2521
Robot,
2622
Servo,
27-
2823
DigitalInputDevice,
2924
Switch,
3025
Button,
3126
MotionSensor,
32-
27+
TouchSensor,
3328
AnalogInputDevice,
3429
Potentiometer,
3530
Pot,
36-
3731
TemperatureSensor,
3832
pico_temp_sensor,
3933
TempSensor,
4034
Thermistor,
41-
4235
DistanceSensor,
4336
)

picozero/picozero.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2027,6 +2027,35 @@ def __init__(self, pin, pull_up=False, bounce_time=1.00):
20272027
MotionSensor.when_motion = MotionSensor.when_activated
20282028
MotionSensor.when_no_motion = MotionSensor.when_deactivated
20292029

2030+
class TouchSensor(Button):
2031+
"""
2032+
Represents a capacitive touch sensor (e.g. TTP223)
2033+
2034+
:param int pin:
2035+
The pin that the capacitive touch sensor is connected to.
2036+
2037+
:param bool pull_up:
2038+
If :data:`True`, the device will be pulled up to
2039+
HIGH. If :data:`False` (the default), the device will be pulled down to LOW.
2040+
Most capacitive touch sensors work with pull_up=False.
2041+
2042+
:param float bounce_time:
2043+
The bounce time for the device. If set, the device will ignore
2044+
any touch events that happen within the bounce time after a
2045+
touch event. This is useful to prevent false triggers from
2046+
electrical noise or multiple rapid touches.
2047+
Defaults to 0.02 seconds.
2048+
"""
2049+
2050+
def __init__(self, pin, pull_up=False, bounce_time=0.02):
2051+
super().__init__(pin=pin, pull_up=pull_up, bounce_time=bounce_time)
2052+
2053+
2054+
TouchSensor.is_touched = TouchSensor.is_active
2055+
# Note: No alias for is_inactive - use 'not touch.is_touched' for clarity
2056+
TouchSensor.when_touched = TouchSensor.when_activated
2057+
TouchSensor.when_touch_ends = TouchSensor.when_deactivated
2058+
20302059

20312060
class AnalogInputDevice(InputDevice, PinMixin):
20322061
"""

0 commit comments

Comments
 (0)