From 2f82574cbfdf19f6341c85ee686cb53266acf5d7 Mon Sep 17 00:00:00 2001 From: Joachim Hummel Date: Thu, 7 Dec 2023 15:40:12 +0000 Subject: [PATCH] Added Day #7 and edit Readme --- 7-dezember.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 16 +++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 7-dezember.py diff --git a/7-dezember.py b/7-dezember.py new file mode 100644 index 0000000..0bce716 --- /dev/null +++ b/7-dezember.py @@ -0,0 +1,63 @@ + +# Imports +from machine import Pin, PWM +import time + +# Set up the LED pins +red = Pin(18, Pin.OUT) +amber = Pin(19, Pin.OUT) +green = Pin(20, Pin.OUT) + +# Set up the Buzzer pin as PWM +buzzer = PWM(Pin(13)) + +# Set PWM duty to 0% at program start +buzzer.duty_u16(0) + +# Set up PIR pin with pull down +pir = Pin(26, Pin.IN, Pin.PULL_DOWN) + +# Warm up/settle PIR sensor +print("Warming up...") +time.sleep(10) # Delay to allow the sensor to settle +print("Sensor ready!") + +def alarm(): # Our alarm function + + # Set PWM duty (volume up) + buzzer.duty_u16(500) + + for i in range(5): # Run this 5 times + + buzzer.freq(1000) # Higher pitch + + red.value(1) # Red ON + amber.value(1) # Amber ON + green.value(1) # Green ON + + time.sleep(1) + + buzzer.freq(500) # Lower pitch + + red.value(0) # Red OFF + amber.value(0) # Amber OFF + green.value(0) # Green OFF + + time.sleep(1) + + # Set PWM duty (volume off) + buzzer.duty_u16(0) + +while True: # Run forever + + time.sleep(0.01) # Delay to stop unnecessary program speed + + if pir.value() == 1: # If PIR detects movement + + print("Bewegung erkannt!") + + alarm() # Call our function + + print("Sensor aktiv") # Let us know that the sensor is active again + + diff --git a/README.md b/README.md index f358daa..583069f 100644 --- a/README.md +++ b/README.md @@ -72,5 +72,21 @@ So, what did we learn today? Today we: ------------------------------------------------------------------------------------------------------ +Day 7 Complete! + +That last activity included a lot of detail so we're going to leave it there for today and not overload your fresh coder brains! + +The PIR sensor can be so much fun to use and they come in all different shapes and sizes too, you'll be making your own home security system in no time. + +So what did we cover on day #7? Today you have: + + Built a circuit with a PIR sensor + Learnt how to use a PIR sensor with MicroPython and the Pico + Created a mini alarm system! + Learnt about the range function + Revisited functions + +------------------------------------------------------------------------------------------------------ +