How to use a PIR motion sensor with the MicroBit V2 board
Hello and welcome back! In this tutorial, we’ll learn how to use a PIR motion sensor with the MicroBit V2 board. This project is not only simple but also a great way to enhance your skills with MicroBit. I’ve also included an LED bulb and the built-in buzzer on the MicroBit to indicate motion detection. If you’re new to MicroBit, I recommend checking out our previous tutorials. For this project, I used the Microsoft MakeCode platform, which makes coding easy with its drag-and-drop code blocks. I believe this knowledge will help you create PIR sensor-based projects, whether for school, university or just for fun.
- Microbit introduction – Click on me
- What is the PIR sensor and How to use it with Arduino – Click on me
Ok, let’s do it step by step. The required components are given below.
- Microbit board — Our store / Amazon
- PIR motion sensor — Our store / Amazon
- LED bulb — Our store / Amazon
- Microbit sensor shield — Our store / Amazon
- Jumper wires — Our store / Amazon
- Keyestudio BBC Micro: bit starter kit — Click on me
Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.
Step 1
Firstly, identify these components.
Step 2
Secondly, connect the PIR sensor and LED to the Microbit sensor shield. For that, you can use the circuit diagram below.
Step 3
Thirdly, connect the microbit board to the shield. Then, connect it to the computer.
Step 4
Now let’s make a code using the MakeCode platform. For that, follow the steps below.
- First, go to the MakeCode website and create a new project. You can name it as you like.
- Then, you can see the MakeCode interface.
- MakeCode — Click on me
- Now copy and paste the following Python script into the Python tab. Then, you can see the automatically created blocks. Otherwise, you can make it yourself using Blocks.
- Python script and Circuit diagram — Click on me
- You can change the buzzer sound and microbit display as you like.
value = 0
def on_forever():
global value
value = pins.digital_read_pin(DigitalPin.P11)
if value == 1:
pins.digital_write_pin(DigitalPin.P12, 1)
music.play(music.tone_playable(262, music.beat(BeatFraction.HALF)),
music.PlaybackMode.IN_BACKGROUND)
basic.show_leds("""
. . # . .
. . # . .
. . # . .
. . . . .
. . # . .
""")
else:
pins.digital_write_pin(DigitalPin.P12, 0)
basic.clear_screen()
basic.forever(on_forever)
Step 5
Next, click the download button and select your Microbit board. Then, it will connect and download the program to the Microbit board.
Now you can test this project using motions. The full video guide is below. So we hope to see you in the next project. Have a good day.
How to use a PIR motion sensor with the MicroBit V2 board