Skip to content

Activity

PIR movement alarm

Advanced | MakeCode, Python | LED display, Pins, Radio | Communication, Electricity, Electronics, Input/output, Product design, Radio waves, Sensors

Step 1: Make it

What is it?

A wireless intruder alarm using a movement detector.

PIR movement sensor attached to pins 0, 3v and GND on micro:bit

How it works

  • PIR (passive infra red) sensors are commonly used in burglar alarms and office lighting systems to detect movement. You can connect one to a micro:bit's pins (see diagram) to trigger an alarm when movement is detected.
  • Connect the sensor’s power input (which may be labelled 'VCC' or '+3v') to the micro:bit’s 3v pin, then the sensor’s GND (ground or Earth pin) to GND on the micro:bit. Next connect the signal output ('S') on the sensor to pin 0 on the micro:bit.
  • Every second, the sensor micro:bit uses a loop to send a radio message to the alarm. It uses selection to send the message ‘movement’ if movement has been detected, or if no movement is detected it sends the message ‘still’.
  • The alarm micro:bit shows a stick-figure on its LED display output and sounds an audible alarm when there’s movement.
  • You may need to adjust the sensitivity and timing of the sensor using a small screwdriver. Check the documentation that came with your sensor for details.

What you need

  • 2 micro:bits and battery packs
  • a PIR (passive infra red) sensor that runs on 3v and 3 suitable leads to connect it to a micro:bit
  • small screwdriver to adjust the sensor
  • optional headphones, buzzer or speaker and crocodile clip leads to attach it to the alarm micro:bit

Step 2: Code it

Sensor / transmitter:

1from microbit import *
2import radio
3radio.config(group=73)
4radio.on()
5
6while True:
7    if pin0.read_digital():
8        display.show(Image.DIAMOND_SMALL)
9        radio.send('moving')
10    else:
11        display.clear()
12        radio.send('still')
13    sleep(1000)
14

Alarm / receiver:

1from microbit import *
2import music
3import radio
4radio.config(group=73)
5radio.on()
6
7while True:
8    message = radio.receive()
9    if message:
10        if message == 'moving':
11            display.show(Image.STICKFIGURE)
12            music.play(["C4:4"])
13        if message == 'still':
14            display.clear()
15

Step 3: Improve it

  • The batteries won’t last very long in the sensor as it sends a radio message every second whether or not it senses any movement. Make it more energy efficient by only sending a message when it senses movement. You may need to modify the alarm program as well.
  • Add more sensor micro:bits to track movement in different rooms, using unique radio messages, e.g. ‘kitchen movement’.
  • Add other sensors like the Light alarm project and Pressure switch alarm project to make a networked alarm system.