Index>BBC Micro:bit>Adeept Sensor Kit for BBC Micro:bit>Lesson 37 Control LED with PIR Sensor
No related product information!

Lesson 37 Control LED with PIR Sensor

3175

    In this lesson, we will carry out an interesting experiment on how to control LED with PIR sensor.

37.1 Components to be prepared

image.png


37.2 The PIR Sensor

37.2.1 PIR Sensor 

The PIR sensor is a kind of sensor that can sense the infrared radiation of the target and make measurements based on the physical properties of infrared. The figure below is the passive infrared sensor we use in this class, also known as human infrared sensor. It has three pins, namely VCC(3.3V), OUT(S) and GND. It can detect every living and non-living thing whose temperature is above absolute zero.PIR sensors will respond to animals such as cats and dogs, as well as humans and other heat sources.When a motion is detected by PIR sensor, the value of the output end will rise.

image.png

image.png 


37.2.2 The working principle of the PIR Movement Sensor

PIR movement sensor, also known as human infrared sensor, it can detect the human body in the form of heat emission of infrared radiation, so can be used to detect the presence of people or animals. The human body has a constant body temperature, generally at 37 degrees, so it will emit a specific wavelength of about 10UM of infrared. The passive infrared probe is used to detect about 10UM of infrared emitted by the human body. The infrared ray about 10UM emitted by the human body is enhanced through the Fresnel filter and then gathered to the infrared sensor. Infrared induction source usually adopts pyroelectric element, which will lose the charge balance when it receives the human body's infrared radiation temperature changes, and release the charge outward. After the subsequent circuit is detected and processed, the alarm signal can be generated.

Within the range of the setting, the high level will be output when someone is detected to be active, otherwise the low level will be output. It can repeatedly trigger the output high level. After sensing the human body, keep the output high level for a delay period and no longer sense, and re-detect after the delay is over. If someone is active within its sensing range, its output will be It keeps high level until the person leaves, and then changes the high level to low level (the sensor module will automatically extend the delay time for each activity detected again after the end of a delay period, and take the time of the last activity as the starting point of the delay time).

    (1) Pyroelectric effect

When some crystals are heated, an equal but opposite amount of charge will be generated at both ends of the crystal. This phenomenon of electrical polarization due to thermal change is called pyroelectric effect.

(2) Fresnel lens

The Fresnel lens is made according to the Fresnel principle. The Fresnel lens is divided into two forms: refraction and reflection. Second, the detection area is divided into several bright areas and dark areas, so that moving objects entering the detection area can generate infrared signals on PIR in the form of temperature changes, so that PIR can generate electrical signals of changes. The sensitivity of pyroelectric human body infrared sensor (PIR) is greatly increased.


37.3 Low level and high level


In circuit, the form of binary (0 and 1) is presented as low level and high level.

Low level is generally equal to ground voltage (0V). High level is generally equal to the operating voltage of components.

The low level of Micro:bit is 0V and high level is 3.3V, as shown below. When IO port on Micro:bit outputs high level, low-power components can be directly driven,like LED.

image.png


37.4 Circuit


You need to connect the components according to the circuit diagram below.


image.png


37.5 MakeCode programming

Next, we will use the online MakeCode Editor to complete the experiment in this lesson.


37.5.1 Start programming

(1) Log in to the website

1. You need to enter the URL in the address bar of Google Browser:

https://makecode.microbit.org/

2. After the website is successfully opened, the interface as shown below will appear:

image.png



(2) Import a project

1. In the HOME interface, click the "Import" button to import the external ".hex" file:

image.png


In the pop-up dialog box, select the "Import File", as shown in the following figure:

image.png



Click the "Choose File"

image.png



Find the code file for this lesson:

BBC_Microbit_Sensor\Code\Lesson_37\BlockCode

Select the file in ".hex" format and click the Open:

image.png


2. Notice whether the file has been loaded into the following window, and then click the "Go ahead!" button, as shown in the following figure:

image.png


3.You can see the following interface when successfully opening the file:

image.png


37.5.2 Run the program

1. After the program is written, connect micro:bit and PC with a Micro USB cable.

2. After micro:bit is connected to the computer, you need to first "Pair device". Click the image.png button on the right of image.png in the lower left corner, and then click the image.png option, as shown in the following figure:

image.png


Then click image.png in the lower right corner

image.png


Then the following dialog box will pop up, select image.png, and then click image.png

image.png


After the device is successfully paired, the image.png button changes to image.png

image.png


3. Start to download the program to Micro:bit, and click the image.png button. Generally, the program will be downloaded directly to the Micro:bit. After the download is completed, your Micro:bit will restart and run the program just downloaded. If any moving creature is approaching PIR Sensor, LED will light up, as shown below:

image.png


[Note]

If Micro:bit doesn't respond after clicking the image.png, you need to click the image.png button on the right of the image.png, and then click the image.png , and observe the situation of the LED again, as shown in the following figure:

image.png


  If you have problems, please send us an email: support@adeept.com


37.5.3 Learn the code program

The following instruction blocks will be applied in the program. Please see the description of the function as follows:

image.png 

(1)If reading == 1, it means that the PIR Sensor detects that a moving creature is approaching, and then controls the LED to light up; if it does not detect the biological movement, then turn off the LED.

image.png



37.6 Python programming

37.6.1 Run the program

1.Connect micro:bit and PC with a Micro USB cable.

2. Open the Mu Editor installed on the computer, and click the button [Load] in the upper left corner to open the source code program of this lesson:

image.png 


Find the code file for this lesson:

BBC_Microbit_Sensor\Code\Lesson_37\PythonCode

Select the file in ".py" format and click the Open:

image.png 


3. Click the [Flash] button to download the program to Micro:bit, as shown in the following figure:

image.png 


4.When the download is successful, if any moving creature is approaching PIR Sensor, LED will light up, as shown below:

image.png


    If you have problems, please send us an email: support@adeept.com


37.6.2 Learn the code program


If reading == 1, it means that the PIR Sensor detects that a moving creature is approaching, and then controls the LED to light up; if it does not detect the biological movement, then turn off the LED.


11

12

13

14

15

16

while True:

    reading = pin0.read_digital()

    if reading == 1:

        pin1.write_digital(0)

    else:

        pin1.write_digital(1)


Note

Since there will be some delay when PIR Sensor detects, so when you are testing, it is recommended to keep moving slowly close to or away from PIR Sensor. The PIR Sensor is more accurate in detecting the living creatures in motion. If the creature is stationary after approaching the PIR Sensor, the PIR Sensor will not be able to detect it, so it is best to keep some movement.