Tuesday, 16 August 2016

Micro Alarm

This is our first hardware project - an alarm. It's (very) inspired by Tech Will Save Us' Micro Guardian. The project is pretty simple, but it's a fun one to make. We set up the micro:bit with a connection between pins 0 and 3v using crocodile clips attached to two strips of copper tape. The copper tape goes across the seal of a jar, making a connection when it's closed and breaking it when it's open. We detect this in the code and, when the connection is broken, we output a pulse to pin 1 which has a buzzer connected to it (the other connection of the buzzer goes to GND). We also used some of MicroPython's built-in images to show the micro:bit asleep face when the connection is closed, and alternating between angry face and sword (Son was insistent that the micro:bit show you that it is both angry and ready to fight you) when open.


We followed the first Tech Will Save Us' first step ('The Jar') to set up our jar - coincidentally we had exactly the same jar. We found their instructions and pictures a little bit unclear, so we'll mention that we bend the copper tape back on itself to make a loop to clip the crocodile clips onto:



We also went for different wiring. Our wiring diagram looks like this:


And that looks like this when we wired it up for real on the micro:bit:


Here's our complete code:

"""
 Micro Alarm
 Inspired by http://make.techwillsaveus.com/projects/microguardian

 Makes the micro_bit into an alarm. Sets up a connection between pin 0 and 3v. When that 
 connection is broken we trigger output on pin1, which should be connected to a buzzer.

 Developed by http://giggletronics.blogspot.co.uk/

 This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 
 International License. https://creativecommons.org/licenses/by-nc-sa/4.0/
"""


from microbit import *


# We define two variables that give more meaningful names to the micro:bit's IO pins
contact = pin0
buzzer = pin1


# Main loop
while True:
    # Is the contact open(0) or closed(1)?
    if contact.read_digital() == 0:
        # Open - Make a beeping noise and show that the micro:bit is angry

        # Turn buzzer on, show angry face and wait for 1/4 second
        buzzer.write_digital(1)
        display.show(Image.ANGRY)
        sleep(250)
        
        # Turn buzzer off, show sword and wait for 1/4 second
        buzzer.write_digital(0)
        display.show(Image.SWORD)
        sleep(250)
    else:
        # Closed - Show that the micro:bit is asleep
        display.show(Image.ASLEEP)

Python and hex files as per usual:
micro_alarm.py
micro_alarm.hex

No comments:

Post a Comment