The previous LED light used the output function of GPIO, and this time we finally need to use the input function of GPIO. Since the use of this human infrared sensing module is relatively simple, we will create a simple human sensing alarm system in conjunction with the previous buzzer after the experiment is complete.
1. Introduction to HC-SR501 Sensing Module
We use the HC-SR501 model human infrared sensor to detect human presence. For more detailed parameters, refer to the module’s user manual.
According to the previous parameters and circuit diagram, find the positive and negative pins on the left and right, with the middle PIN being the sensing output. When a human is detected, it outputs a high level of 3.3V, and when no signal is detected, it outputs 0. It also requires the operating voltage to be between 4.5V-20V. Coincidentally, the P1 numbered pins 2 and 4 on the Raspberry Pi are both 5V, meeting the requirements, so we will connect it to a 5V power supply.
The parameter adjustment knob is used to control some parameters, such as the detection delay time and sensitivity, etc. For specifics, refer to the HC-SR501 manual. Here we will use the default values.
However, there is a key L H mode adjustment valve to introduce. In the upper right corner, there are three pins, assuming from top to bottom they are 1, 2, and 3. There is also a yellow connector; when connected to pins 2 and 3, it represents H mode. This connector can be removed and plugged into the top to connect pins 1 and 2, representing L mode.
L mode is non-retriggerable; when a human is detected, it outputs a high level once, maintains it for a period, then returns to low level. During this time, if a human is still detected, it will not extend the high level time. It will only start detecting again after the low-level lock time (default is 2.5S) has passed.
H mode is retriggerable; if a human is continuously sensed, it will continuously output a high level until no human is detected for a short period, then it will return to low level. 2. Pull-up and Pull-down of GPIO Input
For those learning software, the software world only has 0 and 1, so I also thought this way before coming into contact with hardware. Therefore, when using human infrared sensing, I encountered a problem. I thought that as long as I set the GPIO mode to INPUT, I could simply read the pin status as 0 or 1. However, I encountered an issue. When I set pin 22 (wiringPi number 6) to INPUT mode and connected a wire, the other end was left unconnected. Logically, it should input 0V, but I found that it was not always low; sometimes it was high and sometimes low. When I held the other end, it would output high. When I connected pin 3 (wiringPi number 8), it was always high. I had to check various materials and found out about pull-up and pull-down resistors.
When we connect a pin to GND, it is low; when we connect a pin to 3.3V, it is high; the state is very definite. If we connect nothing, the pin is in a floating state, which can easily be affected by external interference, possibly high, possibly low, or possibly in between. At this point, we need to clearly specify whether this pin should be high or low, and not let it be in a floating state. Connecting a pull-up resistor can ensure that this pin is in a clear high state, while connecting a pull-down resistor can ensure it is in a clear low state. Previously, when we used it for output, we clearly specified the output state. There is a well-written English explanation about pull-up and pull-down resistors, which I also referred to; everyone can test it with Raspberry Pi (http://www.bit-101.com/blog/?p=3813).
PIN3 is always in a high state because this PIN is used for Raspberry Pi I2C devices, which internally has a physical pull-up resistor. Therefore, its state is always high, and this PIN cannot be used as INPUT.
PIN22 fluctuates between high and low due to being in a floating state. Therefore, if we want to use this PIN, we must clearly specify whether it is high or low when floating. wiringPi uses pullUpDnControl to control this variable. 3. Wiring and Testing
The left pin 1 of the module connects to PIN2 of P1 for the positive 5V, the right pin 3 connects to PIN6 for the negative, and the middle pin 2 output line connects to PIN22 (wiringPi is numbered 6). The wiring diagram is as follows:
In the diagram, I have also connected the buzzer. When we detect a human, we will use the beep function from the previous section to emit a warning sound.
First, let’s check if the module works properly by testing the HC-SR501 in different modes.
For H mode, the connector is connected to the bottom two pins. This means it can be retriggered; when a human is detected, it remains at high level.
It can be seen that detection is continuous in the middle, meaning the high level time is extended with human activity.
If L mode is chosen, the connector is connected to the top two pins. This means it cannot be retriggered; when a human is detected, it outputs high level for a period, and during a small following period, if detected again, it will not extend the high level time. It must wait until the lock time has passed before it can detect again.
It can be seen that the high level time in the middle is not continuous.
The detection code is very simple:
We will modify it slightly. When a human is detected, we will use the beep function from the fifth section to execute the alarm operation. Here we choose to use H mode for continuous sensing, and after detection, we will use beep to sound an alarm. This way, we have simply implemented an automatic alarm after human detection.
Leave a Comment
Your email address will not be published. Required fields are marked *