The Someone Behind You Sensor idea (for Arduino) is one of the most useful but simple projects I ever made. I thought it would be worthwhile to port it over to the BBC Micro:bit. The original idea was to have some LEDs placed on a desk, where the user would position the motion sensor in such a way where anyone sneaking up behind them would trigger the LEDs and alert the user to “someone behind them.”
It is also a lot of fun for interactive projects. Once you learn how to use one PIR sensor, you can use many. The same with addressable LEDs.
Due to the complexity of adding addressable LEDs, I decided to separate this project into two halves. The motion sensing half, and the LED light up half. The LED half is posted as PART 2 here.
Now let’s gather all the necessary components you’ll need to build this project.
Lay the Micro:bit and Kitronik Inventor’s Kit on a table. Insert the edge-card end of the Micro:bit into the Inventor’s Kit socket.
Prepare the 9V power supply. I used a 9V power supply from an old device, I think a router. But, there are better versions you could use. Like the one here at element14. You will have to splice the ends into some project wires.
Wire up the PIR sensor, as shown in the picture. I used colors to indicate power (RED), ground (BLACK), and the signal (BLUE).
Attach the PIR sensor’s GND wire to one of the Kitronik Inventor’s Kit breakout pins labeled 0V, that’s a ground. Connect the signal line to the pin labeled P0. This is an analog input.
Follow the schematic below:
Connect the PIR Sensor's signal line to pin A0 on the breakout board.
Connect the 9V power supply’s positive leads to the PIR sensor directly. Then plug the 9V power supply’s ground to another of the Kitronik Inventor’s Kit 0V grounds. To complete a circuit like this, you must share grounds between the power supplies.
A side note: Why do we need to do this? The PIR sensor needs an input voltage of between 5V and 20V, the Micro:bit can only provide a 3.3V source to external devices. Also, Part 2 of the project has addressable LEDs, and we will need a beefy power supply for those.
Attach the 9V positive power to the PIR Sensor directly:
Connect the 9V power supply ground, negative, to the 0V pin on the Inventor's Kit breakout board.
STEP 6 (Optional)
Roll some construction paper into a tube big enough to house the PIR sensor. Point the white dome of the PIR sensor towards one end of the tube. What this is doing is narrowing the field of view for the PIR sensor. You could leave the PIR sensor outside of the tube, sure, but it has a near 180-degree field of vision. You’ll be triggering it with anyone moving in half the room!
Plug the MicroUSB cable into the Micro:bit, and plug the other end of the cable to a PC or MAC.
At this point, you are going to copy over the code to the Micro:bit. I am providing the program (code) in this posts that needs to be copied over.
I had to pack it up in a ZIP file called Mcode.zip. Unzip it. The file you need is called "microbit motion code.hex"
When the Micro:bit plugs into a computer, it shows up as a USB flash drive. All you have to do is copy the file, "microbit motion code.hex," over to the Micro:bit, like it’s a USB Flash Drive.
The Micro:bit will reset, and the program is active.
You could leave it plugged into your computer, or use that 5V USB power adapter to make the project a bit more mobile.
When the PIR motions sensor is not sensing any motion, there is a sad face on the Micro:bit’s LED screen.
When it detects someone, it’s happy, and you see a happy face.
Applications of this project are left to your imagination.
Part 2 is coming up next. It will turn this project into an interactive LED light show.