Mission
This tutorial will provide instructions on how to detect human presence with the capacity of using two kinds of sensors. It requires an Arduino Uno, an infrared motion sensor, and an ultrasonic sensor. The detection can help trigger actions of other sensors or intervention products. For example, for the purpose of our project, we want to detect if people are staying in our targeted area in public space, so our temperature and mister products can start running and moderating extreme temperatures for them.
While the infrared motion sensor can detect motion with sensitivity calibrations and the ultrasonic sensor can measure distance, both have advantages and shortcomings. Therefore, we propose a strategy that combines the two to validate the existence of people inside our target range.
Parts list
1 Ultrasonic Distance Sensor – HC-SR04
1 Infrared IR Body Motion Module – Stemedu HC-SR501 PIR Sensor
1 Arduino Uno
1 Breadboard
1 LED light
7 male to female jumper wires
2 male to male jumper wires
Circuit diagram
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
// First let's think about your use case... // 1. What should be the time between each round of detection for your purpose? // I have it here 3 miliseconds (3 seconds) so in every 3 seconds the sensor will give an output int delayTime = 3000; // 2. How long do you want the last reading to remain valid, even if the person is no longer moving? // For our project, we want the detection of presence to remain valid for 45 seconds. // Hence, we calculate the lagValue to be 45s / delayTime = 15 int lagValue = 15; // 3. How close should the people be from the sensor to be consider "present"? // Here, we have it at 50 cm distThreshold = 50; // Now lets write these to prepare the arduino for its job! // Define pins int ledPin = 13; // LED - we will use this to symbol the action being triggered from detected presence int pirPin = 2; // pin 2 is called pirPin, communicating output from the infrared motion sensor #define TRIG_PIN 12 // pin 12 is called TRIG_PIN #define ECHO_PIN 11 // pin 11 is called ECHO_PIN // Utilize existing library #include "SR04.h" // include "the h file" in the "HC-SR04" library // Now we will define some helpers here SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN); // This creates a shorthand "sr04" that is equivalent to "SR04(ECHO_PIN,TRIG_PIN)" int pirStat = 0; // lets define a variable to represent the infrared motion status. It will be set to 0 as default. long dist; // lets define a variable to hold the value of the distance measured from the ultrasonic sensor. int pValue = 0; // lets define a presence value "pValue" that is default to 0 for the calculation of the presence lag. // the setup function runs once when you press reset or power the board void setup() { pinMode(ledPin, OUTPUT); // initializes digital pin named ledPin as output pinMode(pirPin, INPUT); // initializes pirPin as input Serial.begin(9600); // the baud rate is set to 9600. make sure to match this on the serial monitor } void loop(){ dist = sr04.Distance(); // Grab the distance result from the "sr04" function and store it in the long variable "dist pirStat = digitalRead(pirPin); // store the status of infrared sensor in pirStat if (pirStat == HIGH && dist < distThreshold) { // if the infrared sensor detected motion and the distance between the object and the ultrasonic sensor is within the predetermined range Serial.println("Motion detected"); // display in serial monitor that motion has been detected Serial.println(dist); // show the distance pValue = lagValue; // default the count down for "last present" back to the beginning } else { // if the infrared sensor did not detect motion or the object is out of range Serial.println("No motion detected"); // display in serial monitor that motion has not been detected pValue = pValue-1; // start the count down } if (pValue > 0) { // if the lag of presence is not over digitalWrite(ledPin, HIGH); // action will be triggered delay(delayTime); // the action will last until the next round of detection } else { // if the lag of presence, in our case, after 45 seconds, is over digitalWrite(ledPin, LOW); // action will stop delay(delayTime); // the action will be in halt until the next round of detection } } |