Image by Alan Cleaver on Flickr
Protect your home with micro:bit!
What will you need for this project?
- A microbit
- Micro USB lead to connect micro:bit to computer
- A 2xAAA battery holder and batteries to power micro:bit, or a USB battery
- 4 x Crocodile Clips
- A 3.5mm Powered Speaker
- A laser pointer (or a flashlight)
- A light dependant resistor (LDR)
All of the code for this project and a high resolution circuit diagram can be found at
This project will help keep our home safe from intruders and scare our parents / brothers / sisters, all using the power of the micro:bit.
Protect your home! Can we use the power of the micro:bit to keep safe (or our family out of our room?) Well yes we can and this is thanks to something special about the micro:bit.
Unlike the Raspberry Pi, the micro:bit GPIO (General Purpose Input Output) pins feature analog inputs. This means that when a compatible component is connected it returns a voltage, rather than a 0 or 1 (digital input / output). This voltage is controlled by the sensor. For example, a potentiometer will have variable resistance depending on which way it is turned. In this project we shall use a Light Dependant Resistor (LDR) to monitor a light shone upon it, if the light beam is broken then our micro:bit alarm will alert us to an intruder.
In this project we shall learn:
- How to code with Python
- How to connect components to the micro:bit
- How a Light Dependant Resistor works
- How to generate sound
- How a loop works
- How a conditional statement works
Our project consists of one circuit, controlled by our micro:bit.
The first part of the circuit is our sensor, a Light Dependant Resistor (LDR), this is a resistor that changes is resistance based upon the amount of light it detects. For example when the LDR has a flashlight shone upon it, the resistance increases, changing the voltage that is seen by the micro:bit. If we cover up the sensor then the voltage increases.
Using one of the crocodile clips, connect one leg of the Light Dependant Resistor (LDR) to 3V, and then use another crocodile clip to connect the other leg to Pin 1 of the micro:bit.
The second part of the circuit is a speaker which will play audio when triggered by the sensor. Our speaker is a standard 3.5mm headphone jack, which has a tip and rings. The top part of the jack is called the “tip” and using a crocodile clip, connect this to Pin 0 of the micro:bit. The lowest “ring” of the jack, connect this to GND.
The other part of the project is our laser pointer / flashlight which will need to shine directly upon the LDR. Please note that if you are using a laser, that even a laser pointer can cause eye damage and it should be used with great care.
A flashlight will serve the same purpose and it will be much safer, especially for children.
Writing the Code
To code the micro:bit we used micro Python using the Mu editor. You can download a copy of Mu for your operating system by following the instructions on their website https://codewith.mu/
In Mu we start the code by importing two libraries of pre-written Python code. These are the micro:bit library, used to interface with the board, and the speech library, used to generate the synthesised speech.
Add this code before moving on:
from microbit import * import speech
We now move on to create a loop that will continually run the code that we shall write inside of it. This kind of loop is an “infinite loop” and in Scratch it is called “forever”, but in Python it is called “while True”. Any code inside the loop will run over and over again. We shall use it to start our project.
Inside the loop, all of our code is automatically indented by four spaces or one press of the TAB key. Never mix spaces and tabs together, otherwise Python will get cross and produce an error. Python uses indentation to identify code that is inside a loop, conditional statement, function or class. The first line inside our loop is to create a variable called “light” and in there store the value which is generated by reading the light level from the LDR. This will be a value between 0 and 1023.
light = pin1.read_analog()
Still inside the loop, we now create a conditional statement, a test that will check the value stored in our “light” variable against a hardcoded value. In this case we check to see if the light level is below 1015. This value may require tweaking to suit the light levels in your home, so a little trial and error is encouraged.
if light < 1015:
We are now inside the conditional statement, specifically the code which will be run if the light level falls below 1015. We print “ALERT” to the Python shell, to prove that an alert has been triggered. Then we instruct the micro:bit to generate simple synthesised speech, saying the phrase “INTRUDER ALERT”, we also pass some parameters that control the speed at which the voice speaks, 120 is a medium pace (0, is really fast and 255 very slow), we control the pitch, 100 being a mid range pitch (0, high pitched, 255 low pitch), throat, 100 is mid range (0 is nervous, 255 relaxed), mouth, 200 means that our speech is well pronounced but not to the standards of a Victorian English teacher (0 is tight lipped, 255 enunciates every letter).
After generating the speech we instruct the code to sleep for 1000 milliseconds, 1 second so that our sensor isn’t repeatedly triggered too quickly. Add this code before moving on:
print('ALERT') speech.say('INTRUDER ALERT', speed=120, pitch=100, throat=100, mouth=200) sleep(1000)
We now break out of the if conditional test, and now we must create a condition that will be tested when the LDR has not been triggered. This is the resting state of our alarm system and for this we use an “else” condition. If our first condition, “if” proves to be False, then our Else condition must be True. and as such it simple instructs the code to wait for 1/10th of a second (100 milliseconds) before the main loop repeats the entire process again and again.
Add this code before moving on.
Complete Code Listing
Here is all of the code for this project. Check that your code matches.
from microbit import * import speech while True: light = pin1.read_analog() if light < 1015: print('ALERT') speech.say('INTRUDER ALERT', speed=120, pitch=100, throat=100, mouth=200) sleep(1000) else: sleep(100)
Test the Alarm
In order to test the burglar alarm we need to write this code to our micro:bit. Plug in your micro:bit to your computer using the micro USB to USB lead. Your micro:bit will appear as a USB flash drive. In the Mu application click on “Flash” to write the code to the micro:bit. If you look at the micro:bit you will see a flashing yellow light on the back of the board, this tells us that the information is being written.
Once it stops flashing the micro:bit will reset and your burglar alarm code will run! Now ensure that the laser / flashlight is shining upon the LDR sensor and then break the beam of light to trigger your very own alarm system!
How Can We Take This Further?
An alarm system is really useful, but what if we didn’t want to alert the burglar / intruder to their detection? Well the micro:bit has a radio transceiver built into it, and using another micro:bit we could send a radio message between them, to silently alert us to an intruder.
Micro:Bit Official Website
Where to get a Micro:Bit
Mu Python Editor
Wikipedia: Photoresistor (Light Dependent Resistor)
TechnologyStudent.com: Light Dependent Resistors
Also In The April 2018 Issue
Who chooses new emojis? And what’s coming in the next batch?
Build your own voice-controlled digital assistant with a Raspberry Pi and an analog speaker.
Using Scratch and some simple vector math, create your own Boids algorithm to simulate the flight of birds.
6 hands-on STEM camps and clubs to join this summer.
Activities that get kids up and moving can help them learn science and technology.
With these new high-tech cards, public transit is easier than ever.
Slice digital photos into pieces and have fun pasting them back together.
Meet the cute little bot that’s helping scientists understand the courtship of frogs.
Dive into the nitty-gritty of game-making with this popular Python library.
A quick introduction to one of the world’s most fascinating puzzle toys.
Cheap new VR technology lets students take field trips around the world!
Learn about the Native soldiers and the creative cypher code that helped win WWII.
Our cars aren’t quite driving themselves, but they can help us park and avoid collisions.
Make your games even more exciting with custom user-created content!
Protect your home with micro:bit!
A clever teacher uses our favourite round robot to bring books to life.
Learn how to search through blobs of text with speed, accuracy, and elegance… like a ninja!
A language from the Lisp family with simple syntax and a new approach to writing code.
Links from the bottom of all the April 2018 articles, collected in one place for you to print, share, or bookmark.
Interesting stories about computer science, software programming, and technology for April 2018.