BBC micro:bit
PIR Motion Sensor


Passive Infrared Sensors (PIR) are commonly used in burglar alarm systems to detect the motion of humans. The one used here was a miniature PIR made by DFRobot.


These vary in size, range, detecting angle, reaction time and price. What most have in common is the 3 pin connection. You connect power, ground and a signal pin. you'll need to make sure that your sensor will work fine with 3V3.



The diagram shows a generic PIR. Your cables will be marked on the board that you are using. In this case, the red and black cables are power and ground - they are connected to 3V and GND. The signal cable is connected to pin 0.


Here you go.

from microbit import *


while True: 
    reading = pin0.read_digital()
    if reading==1:
        print("Broken at", running_time())

You can afford a longer delay at the end of the loop for most applications.


  1. Do a better job of the burglar alarm than this example. Add some more action on the matrix or some serious buzzing, perhaps a siren.
  2. Work on the program a little more. Use some functions to separate out the states that you want your motion detector to be in. Use loops to wait for the sensor to fire. Trigger something interesting with other components.
  3. Read the section on connecting to the micro:bit with Visual Basic. Write a program that creates a log of each event on the PIR. The program can listen out for information on the serial port, adding data to a file when done.