Skip to content

PIR Sensor (Motion Detection)

Level Beginner
Board V1 & V2
Parts micro:bit · PIR motion sensor · Active buzzer · Jumper wires
Source pir-buzzer.py

What you'll build

Build a simple motion alarm: when the PIR sensor sees movement, the buzzer sounds.

Wiring

Part Part pin micro:bit pin
PIR sensor OUT P2
PIR sensor VCC 3V
PIR sensor GND GND
Active buzzer − GND
Active buzzer + P1
PIR sensorOUTVCCGNDActive buzzer−+micro:bitP23VGNDP1

PIR sensor and buzzer circuit

Code

pir-buzzer.py
'''
Example: Detecting motion with PIR sensor and buzzer connected to micro:bit
By: Meqdad Darwish
'''
# Note: PIR sensor is connected to pin2.
# Note: Buzzer is connected to pin1.

from microbit import *

while True:
    motion = pin2.read_digital()
    if motion == 1:
        pin1.write_digital(1)
    else:
        pin1.write_digital(0)

How it works

A PIR (passive infrared) sensor outputs 1 while it detects a moving warm body. pin2.read_digital() reads that, and the buzzer on pin 1 mirrors it.

Try this next

  • Show Image.SURPRISED on the display when motion is detected.
  • Count how many times motion was detected and scroll the total with button A.
  • Send an alert to another micro:bit over radio.