From deb59ee3bd5fcbec8f7df53e2b5dc529280e19d1 Mon Sep 17 00:00:00 2001 From: Joachim Hummel Date: Fri, 8 Dec 2023 21:00:44 +0000 Subject: [PATCH] Added Day #8 and edit Readme --- 8-dezember.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 16 ++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 8-dezember.py diff --git a/8-dezember.py b/8-dezember.py new file mode 100644 index 0000000..4270741 --- /dev/null +++ b/8-dezember.py @@ -0,0 +1,68 @@ +# Imports +import onewire, ds18x20, time +from machine import Pin, PWM + +# 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)) + +# Start PWM duty to 0% at program start +buzzer.duty_u16(0) + +# Set the data pin for the sensor +SensorPin = Pin(26, Pin.IN) + +# Tell MicroPython that we're using a DS18B20 sensor, and which pin it's on +sensor = ds18x20.DS18X20(onewire.OneWire(SensorPin)) + +# Look for DS18B20 sensors (each contains a unique rom code) +roms = sensor.scan() + +def alarm(): # Our alarm function + + buzzer.duty_u16(10000) # Buzzer duty (volume) up + + for i in range(5): # Run this 5 times + + buzzer.freq(5000) # Higher pitch + + # LEDs ON + red.value(1) + amber.value(1) + green.value(1) + + time.sleep(0.2) # wait 1 second + + buzzer.freq(1000) # Lower pitch + + # LEDs OFF + red.value(0) + amber.value(0) + green.value(0) + + time.sleep(0.2) # wait 1 second + + buzzer.duty_u16(0) # Buzzer duty (volume) off + +while True: # Run forever + + time.sleep(5) # Wait 5 seconds between readings + + for rom in roms: # For each sensor found (just 1 in our case) + + sensor.convert_temp() # Convert the sensor units to centigrade + time.sleep(1) # Always wait 1 second after converting + + reading = sensor.read_temp(rom) # Take a temperature reading + + print(reading) # Print the reading + + if reading < 25: # If reading is less than or equal to 18 + + alarm() # Call our alarm function + + diff --git a/README.md b/README.md index 583069f..70c396b 100644 --- a/README.md +++ b/README.md @@ -88,5 +88,21 @@ So what did we cover on day #7? Today you have: ------------------------------------------------------------------------------------------------------ +Day 8 Complete! + +Another day, another component completed! You now have a temperature sensor in your growing arsenal of parts to make projects with, and we're sure you'll use this one time and time again. + +As you're probably starting to realise, a lot of these sensors and components are coded in a very similar way. Sometimes we need the help of imported libraries, sometimes we need resistors/additional hardware, but the fundamental way we write code for them (with loops, if statements and variables) is pretty consistent. + +So what did we cover on day #8? Today you have: + + Built a circuit with a temperature sensor + Learnt how to use a temperature sensor with MicroPython and the Pico + Used your first 1-wire component and the 1-wire library + Created a temperature monitor and temperature alarm system + Learnt more about for loops + +------------------------------------------------------------------------------------------------------ +