Here’s a fun (and fairly easy) micro:bit project to get you started.
You will need:
- 1 x BBC micro:bit, its USB lead, and its 3V battery pack.
- 1 x PIR motion sensor (I used this one from Squirrel Labs. Strictly speaking it’s a 5V motion sensor, but seems to work OK with 3V of power from the micro:bit.
- 1 x buzzer.
- 5 x crocodile clips.
- 1 x male to female jumper lead. Not essential, but makes connecting the crocodile clips to the motion sensor a lot easier.
- A clear plastic food container. Quite a big one; there’s lots of wires to fit in it.
- 1 x kitronic micro:bit case. Not essential, but makes it a lot easier to position the micro:bit in the food container.
- A drill and some different size drill bits.
First, adjust the sensitivity and time delay on the motion sensor. Turn both orange dials anti clockwise as far as they’ll go.
This sets the motion sensitivity to it’s least sensitive setting and the time delay between sensing to the shortest possible interval. Five seconds for this motion sensor.
Next, connect your jumper lead to the middle pin (labelled OUT) on your PIR motion sensor. This just makes it a bit easier to connect the motion sensor to the micro:bit with crocodile clips.
Now connect your motion sensor to your micro:bit with crocodile clips:
- The ‘GND’ pin needs to be connected to the GND ring on the micro:bit.
- The ‘OUT’ pin (jumper lead) needs to be connected to ring P0 on the micro:bit.
- The ‘VCC’ pin needs to be connected to 3V ring on the micro:bit.
Next, connect your buzzer to your micro:bit with crocodile clips:
- The short pin needs to be connected to the GND ring on the micro:bit.
- The long pin needs to be connected to ring P1 on the micro:bit.
Your wiring should now look like this:
Now you’re ready to code. I created the following code in the Block Editor:
All the time nothing is sensed, a simple line pulses across the display – – – But when something is sensed the motion sensor sends an electric signal out on its ‘OUT’ pin, which is detected by the micro:bit as a change from 0 to 1 on ring P0 (the ring the ‘OUT’ pin is connected to by the crocodile clips).
All the time a 1 is detected on ring P0, the micro:bit will change its display to ! ! ! and send an electric signal out on ring 1 (digital write 1 to pin P1). The buzzer is connected to this ring by crocodile clips, so it causes the buzzer to buzz for 1 second (1000 ms).
Now connect your micro:bit to your computer via the USB cable, write the code in Block Editor, then compile the .hex file and drag it onto your MICROBIT icon.
It takes a good minute for the sensor to start up properly. After that, you can test things are working properly by covering the motion sensor with an opaque box (or put in in a drawer) for a while. Nothing should be sensed: the display should read – – – and no buzzer noise should be heard. Once you take the box away the display should change to ! ! ! and the buzzer should start going off, as it detects your movement. If it doesn’t work, double check your crocodile clip connections and/or try increasing the sensitivity of the motion sensor by gradually turning the orange dial (the one on the right if the dome it pointing upwards) clockwise.
To turn your intruder alarm into some sort of product, try to fit it all into a clear plastic food container and power it from the micro:bit battery pack. I used my kitronic micro:bit case to attach the micro:bit to the front of the food container.
As you can hopefully see, I had to punch four small holes into the front of the food container to thread the case’s screws through.
I then drilled a big hole in the bottom of the food container so the motion sensor could poke out of the bottom. The ‘finished’, rather crude ‘product’ looks like this.
It’s not the world’s best intruder alarm. It sometimes takes quite a while to sense an intruder, and the buzzer isn’t very loud, but for a first micro:bit project I’m quite pleased with the result.
Watch this space for more micro:bit projects.