3. PIR Sensor(EF05002)#
PIR Sensor is a type of electronic building block based on AM412 pyroelectric digital smart sensor. It can be used in sensing and detection of human or animal motion, sensing distance is about 4-5 meters.
Designed in RJ11 connections, easy to plug.
|Type of Connection||Digital Output|
|Size||55.8 x 23.8 mm|
3.6. Quick to Start#
3.6.1. Materials Required and Diagram#
Connect the PIR sensor to J1 port and the LED to J2 port in the Nezha expansion board as the picture shows.
3.7. MakeCode Programming#
3.7.1. Step 1#
Click “Advanced” in the MakeCode drawer to see more choices.
We need to add a package for programming, . Click “Extensions” in the bottom of the drawer and search with “PlanetX” in the dialogue box to download it.
Note: If you met a tip indicating that the codebase will be deleted due to incompatibility, you may continue as the tips say or build a new project in the menu.
3.7.2. Step 2#
3.7.3. Code as below:#
The LED lights on while the motion being detected by the PIR sensor or the LED lights off.
3.8. Python Programming#
3.8.1. Step 1#
Download the package and unzip it: PlanetX_MicroPython
Go to Python editor
We need to add enum.py and pir.py for programming. Click “Load/Save” and then click “Show Files (1)” to see more choices, click “Add file” to add enum.py and pir.py from the unzipped package of PlanetX_MicroPython.
3.8.2. Step 2#
from microbit import * from enum import * from pir import * while True: pir = PIR(J1) pir_value = pir.PIR_is_decection() if(pir_value == True): display.show(Image.HAPPY) else: display.show(Image.SAD)
A smile face displays on the micro:bit while the motion being detected by the PIR sensor or the micro:bit displays a sad face.