Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,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 @@ -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
------------------
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
11 changes: 2 additions & 9 deletions picozero/__init__.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,36 @@
__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,
PWMOutputDevice,
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,
)
30 changes: 30 additions & 0 deletions picozero/picozero.py
Original file line number Diff line number Diff line change
Expand Up @@ -2028,6 +2028,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
# 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):
"""
Represents a generic input device with analogue functionality, e.g.
Expand Down