Skip to content
8 changes: 8 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ MotionSensor
:inherited-members:
:members:

TouchSensor
-----------

.. autoclass:: TouchSensor
:show-inheritance:
:inherited-members:
:members:

Switch
------

Expand Down
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Change log
-----------

+ Introduced ``MotionSensor`` class for PIR sensors
+ Introduced ``TouchSensor`` class for capacitive touch sensors

0.4.2 - 2023-05-12
------------------
Expand Down
12 changes: 12 additions & 0 deletions docs/examples/touch_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from picozero import TouchSensor, pico_led
from time import sleep

# Capacitive touch sensor output connected to pin 2
touch = TouchSensor(2)

while True:
if touch.is_touched:
pico_led.on()
else:
pico_led.off()
sleep(0.1)
15 changes: 15 additions & 0 deletions docs/examples/touch_sensor_callbacks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from picozero import TouchSensor, pico_led
from time import sleep

touch = TouchSensor(2)

# Set up event callbacks
touch.when_touched = pico_led.on
touch.when_touch_ends = pico_led.off

# Keep the program running
try:
while True:
sleep(1)
except KeyboardInterrupt:
pico_led.off() # Make sure LED is off when exiting
11 changes: 11 additions & 0 deletions docs/recipes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ Turn the :obj:`pico_led` on when a :class:`Button` is pressed and off when it is

.. literalinclude:: examples/button_led.py

Touch sensor
------------

Detect touch using a capacitive touch sensor:

.. literalinclude:: examples/touch_sensor.py

Use callbacks to respond to touch events:

.. literalinclude:: examples/touch_sensor_callbacks.py

Motion sensor
-------------

Expand Down
1 change: 1 addition & 0 deletions picozero/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
Switch,
Button,
MotionSensor,
TouchSensor,
AnalogInputDevice,
Potentiometer,
Pot,
Expand Down
30 changes: 30 additions & 0 deletions picozero/picozero.py
Original file line number Diff line number Diff line change
Expand Up @@ -2359,6 +2359,36 @@ def __init__(self, pin, pull_up=False, bounce_time=1.00):
MotionSensor.when_no_motion = MotionSensor.when_deactivated


class TouchSensor(Button):
"""
Represents a capacitive touch sensor (e.g. TTP223)

:param int pin:
The pin that the capacitive touch sensor is connected to.

:param bool pull_up:
If :data:`True`, the device will be pulled up to
HIGH. If :data:`False` (the default), the device will be pulled down to LOW.
Most capacitive touch sensors work with pull_up=False.

:param float bounce_time:
The bounce time for the device. If set, the device will ignore
any touch events that happen within the bounce time after a
touch event. This is useful to prevent false triggers from
electrical noise or multiple rapid touches.
Defaults to 0.02 seconds.
"""

def __init__(self, pin, pull_up=False, bounce_time=0.02):
super().__init__(pin=pin, pull_up=pull_up, bounce_time=bounce_time)


TouchSensor.is_touched = TouchSensor.is_active
TouchSensor.is_not_touched = TouchSensor.is_inactive
TouchSensor.when_touch_starts = TouchSensor.when_activated
TouchSensor.when_touch_ends = TouchSensor.when_deactivated


class AnalogInputDevice(InputDevice, PinMixin):
"""
Represents a generic input device with analogue functionality, e.g.
Expand Down