This Kitronik University resource is part of the BBC micro:bit partnership
and is an in depth tutorial into making an alarm with the BBC micro:bit
This is a simple tutorial demonstrating how to use the output pins and LED display on the BBC micro:bit. The BBC micro:bit has a triple axis accelerometer built in. This lets us measure acceleration in three directions (x,y and z). We're going to use this feature to help us detect if it is moved, by programming the BBC micro:bit to detect any acceleration across the three axis, and then sound a buzzer when the BBC micro:bit is moved.
Learn how to:
- Program your BBC micro:bit to detect movement.
- Activate a buzzer.
- Protect your BBC micro:bit!
Level of difficulty:
In order to make your BBC micro:bit alarm you will need:
You will also require the following equipment:
- A computer with a USB port and internet access.
Step by Step Guide to making your "Don’t Steal My BBC micro:bit Alarm"
Go to the BBC micro:bit website, click "lets code", then look for the MakeCode Editor
. Let's practice using the block system to build some code. The blocks are organised into categories and the categories are listed in a column down the left hand side of the screen.
First, we need to create a "forever" loop. This is the block that the rest of our code will sit inside. The "forever" loop runs all the blocks inside it starting from the top and working its way to the bottom. Once it gets to the bottom block it starts again at the top and this process will continue forever. Click on the "Basic" category to open it and then drag the "Forever" block into the workspace. (This is the big white area in the middle of the screen.) The forever block is useful as it runs the program again from the beginning once all of the steps have been completed.
We can use the "show string" command to scroll text across the BBC micro:bit's LED display. This will be useful for warning people that the BBC microbit has been alarmed. Drag the "show string" block from the "Basic" category into the workspace and put it into the "forever" block. The two should snap together when you release the mouse button. Clicking on the text will allow you to edit it, try changing "" Hello!" to "Hello World" and then "Don’t steal my micro:bit!".
Now, let's try that out! Press download and after a few moments, the code should appear as a download in your browser. If you plug your BBC micro:bit into a USB port it will show up as a storage device. Simply drag and drop the .hex file you just downloaded onto the BBC micro:bit. Once the file has been transferred the light on the BBC microbit will stop blinking rapidly. Now power up your BBC micro:bit with the batteries, then press the "reset" button next to the USB connector you should see the text you put inside the string block scrolling on the LED display.
We have a message on the LED display to warn people not to steal the BBC micro:bit, what we need now is a way to trigger it when the BBC micro:bit is moved. This will require us to store and use data from the built in accelerometer. To do this we will first create a variable, this is a location where we can store data in the BBC micro:bit's memory. We will then pull data from the accelerometer and store it in that variable. Select "Variables" from the left hand side of the screen then click on Make A Variable. We will be using this variable to track movement in the Z direction so let's call it "Zmovement".
Go into the "Input" category and take the "acceleration(mg)" block and attach it to your "Zmovement" variable block. Make sure the "acceleration(mg)" block is set to the Z axis. These two blocks combined will set the variable "Zmovement" to the Z value of the accelerometer.
Let's put that variable to use with an "If" statement. An "If" statement will perform a process but only "If" certain conditions are met. They are great as they allow us to create programs that react to changes. We can set a condition using blocks in the Logic and Maths categories and a process that will happen if the condition is met. Create a condition that checks if the variable "Zmovement" goes under -1100 or over -900. This will happen if the BBC micro:bit is moved in the Z-axis. (If the BBC micro:bit is still and the LEDs are facing upwards we should get a value of about -1000 from the Z-axis of the accelerometer.) For the "do" part of the block we can add our "Don’t Steal My micro:bit!" string. Upload the code to your BBC micro:bit and try it out. Place the BBC micro:bit onto a table with the LEDs facing upwards. The display should be clear. Then lift the BBC micro:bit and the text will scroll. Hint: The condition in the "If" statement is made up of several blocks located in the Maths and Logic categories.
We've come a long way and have the basis of an alarm. There's a few problems though, we are only checking to see if the BBC micro:bit is being lifted, it could still be sneakily slid away. For extra protection we should check for movement in the X and Y directions too. To do this we need 2 more variables, call them something easy to remember like "Xmovement" and "Ymovement". Have them set to the X and Y readings on the accelerometer. We will also need two extra "If" statements to check that those variables aren’t changing and that somebody isn't moving the BBC micro:bit. These "If" statements need to check that the Xmovement and Ymovement variables don't go any greater than 100 or less than -100.
Our BBC micro:bit is now very secure as it can detect movement in 3 directions. But what if the text isn’t enough to scare off the culprit? Adding a buzzer will let you hear if your BBC micro:bit has been moved from the other side of the room. We've already had a bit of practice changing the condition of the "If" statements, now we will make some changes to the "do" part. In the "Pins" category we have the "digital write" option. If we "digital write" the value "1" to a pin we turn that pin on, setting it to 3V. If we "digital write" the value "0" we turn the pin off, setting it to 0V (sometimes called Ground). In the "Do" section of your three "If" blocks, add a "digital write 1" block and set the pin to "P0" place this block above the text string block. Then underneath the string block add a "digital write 0" block. Our code should now set pin 0 high then scroll the text before setting pin 0 low again when the BBC micro:bit is moved.
What good is turning on a pin if it isn’t connected to anything? Clip one of your crocodile clip leads onto the big "GND" pad at the edge of the BBC micro:bit and the other end of the lead to the black
wire on the buzzer. Clip the other crocodile clip lead to the large pad on the BBC micro:bit labelled "0" and the other end of the lead to the red
wire on the buzzer. If using banana plugs, attach the wires from the buzzer into the plugs, and plug the black plug into the hole marked GND and the red plug into the hole marked "0". Upload your latest code if you haven’t already and try it out!
Try adding another output such as an LED to a different pin, for example pin "P1". The code for activating an LED is the same as the code for activating a buzzer. One key difference though is that when wiring the LED to your BBC micro:bit an LED may require a current limit resistor. Remember the long leg of the LED is positive and needs to be wired to the pin you are activating, the short leg needs to be connected to the GND pin. (If the legs of the LED have been cut, another way to tell is that the negative side of the LED has a straight edge, where the positive side is round. Download a pdf version of this page here
©Kitronik Ltd – You may print this page & link to it, but must not copy the page or part thereof without Kitronik's prior written consent.