Skip to content

Commit 5a164ad

Browse files
committed
Add MotionSensor
Fixes #99
1 parent dcd67a4 commit 5a164ad

File tree

6 files changed

+940
-477
lines changed

6 files changed

+940
-477
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+
MotionSensor
105+
------------
106+
107+
.. autoclass:: MotionSensor
108+
:show-inheritance:
109+
:inherited-members:
110+
:members:
111+
104112
Switch
105113
------
106114

docs/examples/motion_sensor.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from picozero import MotionSensor
2+
from time import sleep
3+
4+
pir = MotionSensor(2)
5+
6+
print("PIR Motion Sensor Example")
7+
print("Waiting for motion...")
8+
9+
while True:
10+
if pir.motion_detected:
11+
print("Motion detected!")
12+
sleep(1)
13+
else:
14+
print("No motion")
15+
sleep(0.5)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from picozero import MotionSensor, LED
2+
from time import sleep
3+
4+
pir = MotionSensor(2)
5+
led = LED(14)
6+
7+
print("Advanced PIR Motion Sensor Example")
8+
print("Using event callbacks for motion detection...")
9+
10+
11+
def motion_detected():
12+
print("🚶 Motion detected!")
13+
led.on() # Turn on LED when motion detected
14+
15+
16+
def no_motion():
17+
print("😴 No motion detected")
18+
led.off() # Turn off LED when no motion
19+
20+
21+
# Set up event callbacks
22+
pir.when_motion = motion_detected
23+
pir.when_no_motion = no_motion
24+
25+
# Keep the program running
26+
try:
27+
while True:
28+
sleep(1)
29+
except KeyboardInterrupt:
30+
print("\nShutting down...")
31+
led.off() # Make sure LED is off when exiting

picozero/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
DigitalInputDevice,
2929
Switch,
3030
Button,
31+
MotionSensor,
3132

3233
AnalogInputDevice,
3334
Potentiometer,

0 commit comments

Comments
 (0)