Added Day #7 and edit Readme

This commit is contained in:
Joachim Hummel 2023-12-07 15:40:12 +00:00
parent 542334c49f
commit 2f82574cbf
2 changed files with 79 additions and 0 deletions

63
7-dezember.py Normal file
View File

@ -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

View File

@ -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
------------------------------------------------------------------------------------------------------
<img src="https://git.unixweb.net/unixweb/pihut-advent-kalender/raw/branch/master/images/pihut-advent-kalender.jpg"> <img src="https://git.unixweb.net/unixweb/pihut-advent-kalender/raw/branch/master/images/pihut-advent-kalender.jpg">