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 * sleep(5000) print("Ready") while True: reading = pin0.read_digital() if reading==1: display.show(Image.HAPPY) print("Broken at", running_time()) print("Pausing") sleep(5000) display.clear() print("Ready") sleep(20)
You can afford a longer delay at the end of the loop for most applications.
- 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.
- 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.
- 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.