Welcome to World of Module Sensor Kit repository
7.6 Human body warning device
1. Learning target
In this course, we will learn how to use Micro:bit, human body infrared sensor module and buzzer module to achieve human body detection function.
2. Preparation
Connect the module to Micro:bit board by expansion board, as shown below.
3. About code
Human body warning-Animation
from microbit import *
import WOM_Sensor_Kit
people_1 = Image("00900:09990:90909:09090:09090")
people_2 = Image("00900:09990:90909:09090:90009")
while True:
if WOM_Sensor_Kit.WOM_pir(pin0) == 1:
display.show(people_1)
sleep(100)
display.show(people_2)
sleep(100)
display.show(people_1)
sleep(100)
display.show(people_2)
sleep(100)
if WOM_Sensor_Kit.WOM_pir(pin0) == 0:
display.show(Image.YES)people_1 = Image("00900:09990:90909:09090:09090") Indicates a custom display image, as shown below.
people_2 = Image("00900:09990:90909:09090:90009") Indicates a custom display image, as shown below.
The brightness range of each LED on the micro:bit dot matrix is 0-9. If the brightness of an LED is set to 0, then it goes out. If its brightness is set to 9, it means that the dot matrix is the brightest.
If there is human body movement is detected, and two custom human body patterns are displayed on the micro:bit dot matrix, which are displayed alternately twice to create an animation effect.
If there is not human movement is detected, “√” is displayed on micro:bit.
Human body warning-Music
# -*- coding: utf-8-*-# Encoding cookie added by Mu Editor
from microbit import *
import music
import WOM_Sensor_Kit
people_1 = Image("00900:09990:90909:09090:09090")
while True:
if WOM_Sensor_Kit.WOM_pir(pin0) == 1:
display.show(people_1)
music.play(music.CHASE)
if WOM_Sensor_Kit.WOM_pir(pin0) == 0:
display.show(Image.YES)people_1 = Image("00900:09990:90909:09090:09090") Indicates a custom display image, as shown below.
The brightness range of each LED on the micro:bit dot matrix is 0-9. If the brightness of an LED is set to 0, then it goes out. If its brightness is set to 9, it means that the dot matrix is the brightest.
If there is human body movement is detected, and human body patterns are displayed on the micro:bit dot matrix, and buzzer will play music.
If there is not human movement is detected, “√” is displayed on micro:bit.
4.Writing and download code
4.1 You should open the Mu software, and enter the code in the edit window, , as shown below.
Note! All English and symbols should be entered in English, use the Tab key (tab key) to indent and the last line must be a space.
4.2 You can click the “Check” button to check if our code has an error.
If a cursor or underline appears on a line, it indicates a syntax error, please check and modify. If there is no error in the program, the bottom left of the interface will prompt that there is no problem in detection.
4.3 Click the ‘REPL’ button to check whether the WOM_Sensor_Kit Python library has been downloaded.
If not, please refer to [Preparation before class] --> [Python Programming Guide] .
4.4 After the program is written, use a micro USB cable to connect the computer and the micro:bit board. Please click the ‘Flash’ button to download the program to the micro:bit motherboard (You need to click the ‘REPL’ button again to close the function of importing library files before you download the program).
4.5 If the download failed, please confirm whether the micro:bit is connected to the computer through the micro USB data cable, and confirm whether the WOM_Sensor_Kit Python library has been imported.
5. Phenomenon
After the program is downloaded successfully. When a human body is detected, the buzzer will make a sound or display dynamic human walking pattern.