diff --git a/GemmaM0_Vibration_Switch_Motion_Alarm/main.py b/GemmaM0_Vibration_Switch_Motion_Alarm/main.py new file mode 100644 index 000000000..699fc138f --- /dev/null +++ b/GemmaM0_Vibration_Switch_Motion_Alarm/main.py @@ -0,0 +1,29 @@ +# Motion Sensor Alarm +# uses Gemma M0, vibration sensor on A0/GND, & piezo on D0/GND +import pulseio +from digitalio import DigitalInOut, Direction, Pull +from analogio import AnalogIn +import board +import time + +piezo = pulseio.PWMOut(board.D0, duty_cycle=0, frequency=440, + variable_frequency=True) + +vibrationPin = AnalogIn(board.A0) + +def get_voltage(pin): + return (pin.value * 3.3) / 65536 + +while True: + print((get_voltage(vibrationPin),)) + vibration = get_voltage(vibrationPin) + + if vibration < 1: # the sensor has been tripped, you can adjust this value + # for sensitivity + for f in (2620, 4400, 2620, 4400, 2620, 4400, 2620, 4400): + piezo.frequency = f + piezo.duty_cycle = 65536//2 # on 50% + time.sleep(0.2) # on 1/5 second + piezo.duty_cycle = 0 # off + time.sleep(0.02) # pause + time.sleep(0.01) # debounce delay