The Messy Real World — Day 3 of 4
'F' → Fullscreen
Keep the three LEDs and the button with its 10K pull-down: Task 5 uses them both. You can leave the knob and the TMP36 on the board, but today's circuit doesn't use them, so the pictures leave them out. Create a new MicroPython program named 2-10-alarm.
3V3 and GND, with A2 connected to the point between them.The photocell has two legs and no flat side: either way round works. What matters is that it sits on the 3V3 side and the 10K sits on the GND side, so covering it makes the reading go down. With the USB cable unplugged, wire it in the free columns to the right of the red LED:
| Part | Where it goes |
|---|---|
Wire from the 3V3 pin | Top + rail (already there) |
| 10K resistor (brown, black, orange) | Top − rail to a24, standing upright like the LED resistors |
| Photocell legs | c24 and c26 |
| Jumper | a26 to the top + rail |
Wire from A2 | e24 (it comes up from the bottom header, like 2.9's A1) |
Column 24 is the point between the two resistances: the 10K, the photocell, and the A2 wire all meet in that one strip.
from machine import Pin, ADCimport time
light_sensor = ADC(Pin(42))
while True: print(light_sensor.read_u16()) time.sleep(0.2)read_average() from 2.9 so the reading holds still.Put your threshold nearer the covered value than the bright one. An alarm that fires when someone walks past the window is worse than one that needs a proper blocking.
from machine import Pin, ADCimport time
light_sensor = ADC(Pin(42))
def read_average(sensor, samples): total = 0 for i in range(samples): total = total + sensor.read_u16() time.sleep(0.01) return total // samples
while True: print("light:", read_average(light_sensor, 10)) time.sleep(0.3)GPIO 23.beep(speaker, hz, seconds) that sets a pitch, turns the sound on, waits, then silences it.The resistor matters. A speaker connected straight to a pin will try to draw far more current than the pin can give.
The top half is now full, so the speaker goes in the empty bottom half. The bottom rails are not connected to anything yet: one long jumper joins the two − rails, so the speaker can reach GND.
| Part | Where it goes |
|---|---|
Wire from GPIO 23 | a29 |
| 330Ω resistor (orange, orange, brown) | e29 to f29, across the center gap |
| Speaker legs | h29 and h26 (if one leg is marked +, it goes in h29) |
| Jumper | j26 to the bottom − rail |
| Long jumper | Top − rail to bottom − rail, down column 30 |
from machine import Pin, ADC, PWMimport time
light_sensor = ADC(Pin(42))speaker = PWM(Pin(23))
def read_average(sensor, samples): total = 0 for i in range(samples): total = total + sensor.read_u16() time.sleep(0.01) return total // samples
def beep(speaker, hz, seconds): speaker.freq(hz) speaker.duty_u16(32768) time.sleep(seconds) speaker.duty_u16(0)
beep(speaker, 440, 0.3)beep(speaker, 660, 0.3)beep(speaker, 880, 0.3)DARK.from machine import Pin, ADC, PWMimport time
light_sensor = ADC(Pin(42))speaker = PWM(Pin(23))
DARK = 12000ALARM_HZ = 880
def read_average(sensor, samples): total = 0 for i in range(samples): total = total + sensor.read_u16() time.sleep(0.01) return total // samples
def beep(speaker, hz, seconds): speaker.freq(hz) speaker.duty_u16(32768) time.sleep(seconds) speaker.duty_u16(0)
while True: light = read_average(light_sensor, 10) if light < DARK: print("Beam broken!") beep(speaker, ALARM_HZ, 0.2) time.sleep(0.05)The trick is a variable that remembers. armed starts True, becomes False when the beam breaks, and only the button puts it back.
from machine import Pin, ADC, PWMimport time
leds = [ Pin(28, Pin.OUT), Pin(29, Pin.OUT), Pin(30, Pin.OUT),]light_sensor = ADC(Pin(42))speaker = PWM(Pin(23))button = Pin(33, Pin.IN)
DARK = 12000ALARM_HZ = 880
def read_average(sensor, samples): total = 0 for i in range(samples): total = total + sensor.read_u16() time.sleep(0.01) return total // samples
def beep(speaker, hz, seconds): speaker.freq(hz) speaker.duty_u16(32768) time.sleep(seconds) speaker.duty_u16(0)
def all_on(leds): for led in leds: led.value(1)
def all_off(leds): for led in leds: led.value(0)
armed = Trueall_off(leds)print("Armed.")
while True: light = read_average(light_sensor, 10) if armed: if light < DARK: print("Beam broken!") armed = False else: all_on(leds) beep(speaker, ALARM_HZ, 0.2) all_off(leds) time.sleep(0.1) if button.value() == 1: armed = True print("Re-armed.") time.sleep(0.05)Once the beam breaks, the array flashes with every beep and keeps flashing, even after you uncover the sensor, until you press the button:
Your finished program should behave like this:
Armed.Beam broken!Re-armed.Beam broken!Answer the following questions before submitting your work.
DARK. Describe how you actually chose yours, and why the same number would not work in a different room.armed variable, the alarm stops as soon as the sensor is uncovered. Explain how one True/False variable changes the behavior, and why that is what an alarm should do.Submit the required files to the appropriate dropbox.