Skip to content

Commit cd6277e

Browse files
committed
Add TouchSensor class for capacitive touch sensors
1 parent dcd67a4 commit cd6277e

File tree

7 files changed

+954
-487
lines changed

7 files changed

+954
-487
lines changed

docs/api.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ Button
101101
:inherited-members:
102102
:members:
103103

104+
TouchSensor
105+
-----------
106+
107+
.. autoclass:: TouchSensor
108+
:show-inheritance:
109+
:inherited-members:
110+
:members:
111+
104112
Switch
105113
------
106114

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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from picozero import TouchSensor, LED
2+
from time import sleep, time
3+
4+
touch = TouchSensor(2)
5+
6+
led = LED(25)
7+
8+
9+
def on_touch():
10+
led.on()
11+
12+
13+
def on_release():
14+
led.off()
15+
16+
17+
# Method 1: Using callbacks
18+
touch.when_touched = on_touch
19+
touch.when_touch_ends = on_release
20+
21+
sleep(10)
22+
23+
# Method 2: Polling the sensor state
24+
touch.when_touched = None # Disable callbacks
25+
touch.when_touch_ends = None
26+
27+
start_time = time.time()
28+
while time.time() - start_time < 10:
29+
if touch.is_touched:
30+
led.on()
31+
else:
32+
led.off()
33+
sleep(0.5)
34+
35+
# Clean up
36+
touch.close()
37+
led.close()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from picozero import TouchSensor, LED
2+
from time import sleep
3+
4+
# Create a capacitive touch sensor on pin 2
5+
touch = TouchSensor(2)
6+
7+
# Create an LED on pin 25 (built-in LED)
8+
led = LED(25)
9+
10+
11+
def touched():
12+
led.on()
13+
14+
15+
def released():
16+
led.off()
17+
18+
19+
# Set up event callbacks
20+
touch.when_touched = touched
21+
touch.when_touch_ends = released
22+
23+
# Keep the program running
24+
try:
25+
while True:
26+
sleep(1)
27+
except KeyboardInterrupt:
28+
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,42 +1,35 @@
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,
31-
26+
TouchSensor,
3227
AnalogInputDevice,
3328
Potentiometer,
3429
Pot,
35-
3630
TemperatureSensor,
3731
pico_temp_sensor,
3832
TempSensor,
3933
Thermistor,
40-
4134
DistanceSensor,
4235
)

0 commit comments

Comments
 (0)