From f640082ee56584e3e0efb1c097b9d73276365ee7 Mon Sep 17 00:00:00 2001 From: Pete Bell Date: Wed, 15 Oct 2025 15:52:04 +0100 Subject: [PATCH 1/4] Add TouchSensor class for capacitive touch sensors --- docs/api.rst | 8 +++++++ docs/examples/touch_sensor.py | 13 +++++++++++ docs/examples/touch_sensor_callbacks.py | 15 +++++++++++++ picozero/__init__.py | 11 ++-------- picozero/picozero.py | 29 +++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 9 deletions(-) create mode 100644 docs/examples/touch_sensor.py create mode 100644 docs/examples/touch_sensor_callbacks.py diff --git a/docs/api.rst b/docs/api.rst index 782d90c..5312c80 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -109,6 +109,14 @@ MotionSensor :inherited-members: :members: +TouchSensor +----------- + +.. autoclass:: TouchSensor + :show-inheritance: + :inherited-members: + :members: + Switch ------ diff --git a/docs/examples/touch_sensor.py b/docs/examples/touch_sensor.py new file mode 100644 index 0000000..fff9534 --- /dev/null +++ b/docs/examples/touch_sensor.py @@ -0,0 +1,13 @@ +from picozero import TouchSensor +from time import sleep + +touch = TouchSensor(2) + +print("Touch the sensor...") + +while True: + if touch.is_touched: + print("Touch detected!") + else: + print("No touch") + sleep(0.1) diff --git a/docs/examples/touch_sensor_callbacks.py b/docs/examples/touch_sensor_callbacks.py new file mode 100644 index 0000000..cdd640f --- /dev/null +++ b/docs/examples/touch_sensor_callbacks.py @@ -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 diff --git a/picozero/__init__.py b/picozero/__init__.py index 83cb63c..d2e9e9c 100644 --- a/picozero/__init__.py +++ b/picozero/__init__.py @@ -1,14 +1,12 @@ __name__ = "picozero" __package__ = "picozero" -__version__ = '0.4.2' +__version__ = "0.4.2" __author__ = "Raspberry Pi Foundation" from .picozero import ( PWMChannelAlreadyInUse, EventFailedScheduleQueueFull, - pinout, - DigitalOutputDevice, DigitalLED, Buzzer, @@ -16,28 +14,23 @@ PWMLED, LED, pico_led, - PWMBuzzer, Speaker, - RGBLED, Motor, Robot, Servo, - DigitalInputDevice, Switch, Button, MotionSensor, - + TouchSensor, AnalogInputDevice, Potentiometer, Pot, - TemperatureSensor, pico_temp_sensor, TempSensor, Thermistor, - DistanceSensor, ) diff --git a/picozero/picozero.py b/picozero/picozero.py index 2836e0b..56e8776 100644 --- a/picozero/picozero.py +++ b/picozero/picozero.py @@ -2027,6 +2027,35 @@ def __init__(self, pin, pull_up=False, bounce_time=1.00): MotionSensor.when_motion = MotionSensor.when_activated 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 +# Note: No alias for is_inactive - use 'not touch.is_touched' for clarity +TouchSensor.when_touched = TouchSensor.when_activated +TouchSensor.when_touch_ends = TouchSensor.when_deactivated + class AnalogInputDevice(InputDevice, PinMixin): """ From 48cf499d12433fa72bedcd0a06f37eecbfc16572 Mon Sep 17 00:00:00 2001 From: Pete Bell <104009652+pjbRPF@users.noreply.github.com> Date: Wed, 15 Oct 2025 16:02:43 +0100 Subject: [PATCH 2/4] Update picozero.py inherit from DigitalInputDevice - makes more semantic sense than Button --- picozero/picozero.py | 1 + 1 file changed, 1 insertion(+) diff --git a/picozero/picozero.py b/picozero/picozero.py index 56e8776..f0a4d59 100644 --- a/picozero/picozero.py +++ b/picozero/picozero.py @@ -2027,6 +2027,7 @@ def __init__(self, pin, pull_up=False, bounce_time=1.00): MotionSensor.when_motion = MotionSensor.when_activated MotionSensor.when_no_motion = MotionSensor.when_deactivated + class TouchSensor(Button): """ Represents a capacitive touch sensor (e.g. TTP223) From b688054cd8eaf45ea97598d256da6390e86f51d1 Mon Sep 17 00:00:00 2001 From: Pete Bell Date: Thu, 16 Oct 2025 08:33:59 +0100 Subject: [PATCH 3/4] removed advanced example as just a combo --- docs/examples/touch_sensor.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/docs/examples/touch_sensor.py b/docs/examples/touch_sensor.py index fff9534..f5fb6e5 100644 --- a/docs/examples/touch_sensor.py +++ b/docs/examples/touch_sensor.py @@ -1,13 +1,12 @@ -from picozero import TouchSensor +from picozero import TouchSensor, pico_led from time import sleep +# Capacitive touch sensor output connected to pin 2 touch = TouchSensor(2) -print("Touch the sensor...") - while True: if touch.is_touched: - print("Touch detected!") + pico_led.on() else: - print("No touch") + pico_led.off() sleep(0.1) From cefc70068008db13372ea1276a4ec7a17fe28d57 Mon Sep 17 00:00:00 2001 From: Pete Bell Date: Fri, 17 Oct 2025 16:51:14 +0100 Subject: [PATCH 4/4] Add documentation for TouchSensor class - Add TouchSensor section to recipes.rst with comprehensive examples - Update changelog.rst with v0.5.0 entry for TouchSensor class - Position TouchSensor logically after Buttons in input devices section --- docs/changelog.rst | 1 + docs/recipes.rst | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index 8c397cc..bae99d6 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -7,6 +7,7 @@ Change log ----------- + Introduced ``MotionSensor`` class for PIR sensors ++ Introduced ``TouchSensor`` class for capacitive touch sensors 0.4.2 - 2023-05-12 ------------------ diff --git a/docs/recipes.rst b/docs/recipes.rst index f0e29bf..e20b36a 100644 --- a/docs/recipes.rst +++ b/docs/recipes.rst @@ -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 -------------