"""
This is example of simple intruder alarm using PIR motion sensor, typicaly HC-SR501,
Raspberry Pi Pico and Adafruit's CircuitPython.
When device starts, the alarm is disabled. To enable or disable alarm use blue button.
The green LED lights up when the device is disabled, red led is off. If motion is
detected, the pico onboard led lights up.
After button is pressed and released, alarm is enabled (armed), green led is off,
red led lights up. If motion is detected buzzer beeps and also the pico onboard
led lights up.
If alarm is enabled then after button is pressed and released, alarm is disabled.
Regarding buzzer, here, due to Wokwi portfolio of components the buzzer is passive.
But active buzzer is much more suitable, because is much more louder and needn't
PWM to beep.
"""
import board
import digitalio
import time
from adafruit_debouncer import Debouncer
import pwmio
# Set right value according to kind of your buzzer
PASSIVE_BUZZER = True
PIN_BUTTON = board.GP22
PIN_BUZZER = board.GP18
PIN_PIR = board.GP28
PIN_LED_GREEN = board.GP15
PIN_LED_RED = board.GP14
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
button = digitalio.DigitalInOut(PIN_BUTTON)
button.switch_to_input(pull=digitalio.Pull.DOWN)
pir = digitalio.DigitalInOut(PIN_PIR)
pir.direction = digitalio.Direction.INPUT
greenled = digitalio.DigitalInOut(board.GP15)
greenled.direction = digitalio.Direction.OUTPUT
redled = digitalio.DigitalInOut(PIN_LED_RED)
redled.direction = digitalio.Direction.OUTPUT
switch = Debouncer(button)
armed = False
if (PASSIVE_BUZZER):
# print("PASIVE_BUZZER: ", PASSIVE_BUZZER)
buzzer = pwmio.PWMOut(PIN_BUZZER, variable_frequency=True)
buzzer.frequency = 2000
else:
buzzer = digitalio.DigitalInOut(PIN_BUZZER)
buzzer.direction = digitalio.Direction.OUTPUT
def pipip():
if (PASSIVE_BUZZER):
buzzer.duty_cycle = 2**15
time.sleep(0.3)
buzzer.duty_cycle = 0
time.sleep(0.15)
else:
buzzer.value = True
time.sleep(0.3)
buzzer.value = False
time.sleep(0.15)
print("Device is running!")
while True:
# print("armed: ", armed)
# print(button.value)
switch.update()
if (switch.rose):
if (armed==False):
armed=True
# time.sleep(30)
else:
armed = False
# time.sleep(0.5)
if armed:
redled.value = True
greenled.value = False
# print("Armed!")
else:
redled.value = False
greenled.value = True
# print("Disarmed!")
if (pir.value):
led.value = True
if armed:
pipip()
pass
else:
led.value = False
# buzzer.value = False
# time.sleep(0.01)