Category Archives: Programming

How to Build a PIR Motion Sensor Intruder Alarm with the BBC micro:bit

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.

you will need

First, adjust the sensitivity and time delay on the motion sensor. Turn both orange dials anti clockwise as far as they’ll go.

sensor etup

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:

intruder alarm code

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.

microbit in case

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.

final product

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.


BBC Micro:Bit arrives!

We received our BBC Micro:Bit today and are very keen to start playing around with it to find out what it can do. In addition to the Micro:Bit itself, the package contained a few other goodies such as: micro-bit_in_box_small

  • A Quick Start guide with tutorials to get you started
  • A USB connection cable to allow transfer of projects between computer and Micro:Bit
  • A battery pack and batteries to enable the Micro:Bit to become portable
  • A pack of crocodile clips to allow the Micro:Bit to connect to devices and sensors
  • A buzzer
  • An audio jack cable (haven’t worked out what this can be used for just yet)
  • A USB stick pre-loaded with examples projects

It’s worth mentioning that all of this has cost us precisely £0, so our thanks go out to the team behind the Micro:Bit for giving us the opportunity to test out this fantastic device with the aim of using it to deliver the Computing curriculum and hopefully inspiring the next generation of computer programmers.

We will report back when we’ve unpacked everything and taken the Micro:Bit for a trial run.


Year 6 HTML club latest

Lots of de-bugging this week. One example, which had us scratching our heads for a good while, was the mysterious  appearance of the paragraph with nothing in it ( see image below- right) We finally spotted the fact that there was a closing paragraph tag which was missing the all important forward slash, so it didn’t close the <p> tag at all….in fact it created another one below!


Once this forward slash was added, however, the page sorted itself out and we all breathed a sign of relief!

forward_slash_code forward_slash_browser

I might have to have a chat with them about their choice of colours though…..