This tutorial will help you setup an entry/exit detector at your doorstep. I paired it up with a relay to control the lights in my room. So , depending upon the number of people in the room , lights are switched on and off. I will break down the tutorial in two parts :
1.Entry/ Exit Detection
We will use two HC-SR04 sensors. These are capable of emitting and detecting ultrasonic sound waves. Here is how one of these looks :
The idea is to use them to send sound pulses continuously. We will measure the time required to detect a pulse that has bounced off some object. Using this time and the known average speed of sound in air , we will calculate the approximate distance of the object from the sensor. Now , if a person enters or exits, this value of distance will change. This change thus becomes an indication of if something has passed through the door. If we use two sensors kept next to each other , we can also detect if that someone has entered or exited , depending upon which sensor's value changes first.
About the sensor : We see it has four pins. Vcc goes to +5V of Arduino. Trig Pin when high produces a sound pulse. Echo pin is high when the reflected pulse falls on the sensor.
2. Controlling The lights
Now that we know , whenever someone has entered or exited the room, we use this information to control lights. You can use a relay or any other mechanism to switch the light on or off. I used a motor to do it. I agree that it is a very inefficient way but given that nobody at home would be happy with me opening switchboards and pulling wires out of it , I took the other road.
Now , whenever no one is in the room , the motor turn to turn the light switch off.
See my earlier posts to learn how to use motors with the arduino.
This is how my setup looked :
WIRING IT UP
Created using Fritzing
THE PROGRAM
#define trigPin1 9 // HC-SR04 pins
#define echoPin1 8
#define trigPin2 11#define echoPin2 10
int in1Pin = 6; // Motor Pins
int in2Pin = 7;
int n=0,l=0,m=0;
// n : No. of people in the room
void setup()
{
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
}
long ping(int trigPin,int echoPin)// returns distance
{
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
return distance;
}
void loop()
{
long x,xx,y,yy; // variable to store distances
x=ping(trigPin1,echoPin1);
xx=ping(trigPin1,echoPin1);
if((x-xx)>20) // Change this to increace/decrease sensitivity
{Serial.println("Entered ");
n++;
delay(1000);
}
else
{
y=ping(trigPin2,echoPin2);
yy=ping(trigPin2,echoPin2);
if((y-yy)>20)
{Serial.println("Exited ");
n--;
delay(1000);
}
}
Serial.println();
if (n==-1) n=0;
if (n==1 && l==0)
{ON();l++;m++;}
if(n==0 && m!=0)
{OFF();m=0;l=0;}
Serial.println("No. of people in room: " );
Serial.print(n);
}
void ON() // runs motor to turn on switch
{
digitalWrite(in2Pin, HIGH);
digitalWrite(in1Pin, LOW);
delay(300);
digitalWrite(in2Pin, LOW);
}
void OFF() // runs motor to turn off switch
{
digitalWrite(in2Pin, LOW);
digitalWrite(in1Pin, HIGH);
delay(300);
digitalWrite(in1Pin, LOW);
}
-----------------------------------------------------------------------------------------------------------------------------