Introduction: Lights Activated by Entry/Exit

This project is a prototype. I have built a project which uses two ultrasonic sensors to monitor entry and exit of a person through a hallway or door. the two ultrasonic sensors sense for changes in distance. If one of them is activated, then the other sensor for 3 seconds searching for changes in distance. If the second sensor is triggered the arduino toggles the state of the led. It also monitors the number of people entering and exiting the room. Feel free to edit and adjust my project and make recommendations in the comments below. I am still a beginner so keep an open mind.

Step 1: Parts Required

The project I am building is a prototype which can be adjusted to a an individual's own needs. I will be using a led to represent the light. The led can be replaced throughout the project by a relay to control an actual light bulb.

So we require,

  • An Arduino (UNO)
  • 3 LEDs
  • 2 HC-SR04 Ultrasonic Sensors
  • Jumper Cables
  • Relay (if you want to control a light bulb)

Step 2: The Setup

The setup can be seen as in the diagram. The connections go like this

Ultrasonic Sensor 1:

  • VCC -> 5V
  • Trig -> 12
  • Echo -> 13
  • Gnd -> Gnd

Ultrasonic Sensor 2:

  • VCC -> 5V
  • Trig -> 3
  • Echo -> 2
  • Gnd -> Gnd

Led 1:

  • +ve ->11
  • -ve -> Gnd

Led 2:

  • +ve ->10
  • -ve -> Gnd

Led (Light to be controlled):

  • +ve ->8
  • -ve -> Gnd

By default by moving from sensor 1 to sensor 2 the light turns on and from sensor 2 to sensor 1 the lights turn off.

Step 3: The Code

The program is designed to turn on the lights from a movement from sensor 1 to 2 and off for sensor 2 to 1. The distance variables are in cm, so you can edit the variable to suit your needs. By default the range is set to 100 cm.

The program is as follows,

/* The program uses two ultrasonic sensors to provide details for entry and exits used for turning lights on or off.

* Code Written By Siddak Bakshi

* Distribution Rights Reserved

*/

//Global Variables

#define trig1Pin 12 //Defining Pin for Trigger1 (Output Port)

#define echo1Pin 13 //Defining Pin for Echo1 (Input Port)

#define trig2Pin 2 //Defining Pin for Trigger2 (Output Port)

#define echo2Pin 2 //Defining Pin for Echo2 (Input Port)

int led = 8; /*The led represents the light that is being controlled in this project. If you want to use a normal light bulb replace the led with a relay, which you can use as a toggle switch for your lightbulb */

int led1 = 11; /* Led left and right are just for the sake of convenience. While prototyping you can't actually watch which ultrasonic sensor is actually working or is activated. So the leds act as indicators. you can remove the left and right leds from the code for the final project.*/

int led2 = 10;

int counter = 0;

unsigned long time_since_last_reset = 0; //For setting time reference before loops int interval_one = 3000; //3 seconds for 1st while loop

int interval_two = 3000; //3 seconds for 2nd while loop

//Global Variables End

void setup() { Serial.begin (9600); //Serial Monitor set to 9600 Bauds

pinMode(trig1Pin, OUTPUT);

pinMode(echo1Pin, INPUT);

pinMode(trig2Pin, OUTPUT);

pinMode(echo2Pin, INPUT);

pinMode(led, OUTPUT);

pinMode(led1, OUTPUT);

pinMode(led2, OUTPUT); }

void loop() {

int duration1, distance1;

digitalWrite(trig1Pin, HIGH);

delayMicroseconds(1000);

digitalWrite(trig1Pin, LOW);

duration1 = pulseIn(echo1Pin, HIGH);

distance1 = (duration1/2) / 29.1;

if (distance1 <= 100 && distance1 >= 0) {

digitalWrite(led2, HIGH);

int t=0;

time_since_last_reset = millis(); //obtain time referenece while(t==0 && (millis() - time_since_last_reset) < interval_one) //While Loop No. 1 for 'Lights On'

{ int duration3, distance3;

digitalWrite(trig2Pin, HIGH);

delayMicroseconds(1000);

digitalWrite(trig2Pin, LOW);

duration3 = pulseIn(echo2Pin, HIGH);

distance3 = (duration3/2) / 29.1;

if(distance3 <= 100 && distance3 >= 0)

{

counter++; Serial.println("Counter =" +counter);

digitalWrite(led2, LOW);

Serial.println("Lights On");

digitalWrite(led, HIGH); t=2000; delay(1000);

}

}

if((millis() - time_since_last_reset) >= interval_one) //To turn off led1 if time runs out {

digitalWrite(led2, LOW);

}

}

int duration2, distance2;

digitalWrite(trig2Pin, HIGH);

delayMicroseconds(1000);

digitalWrite(trig2Pin, LOW);

duration2 = pulseIn(echo2Pin, HIGH);

distance2 = (duration2/2) / 29.1;

if(distance2 <= 100 && distance2 >=0)

{ digitalWrite(led1, HIGH); //Sensor 1 detects motion therefore led int t=0;

time_since_last_reset = millis(); //obtain time reference while(t==0 && (millis() - time_since_last_reset) < interval_two) //While Loop No. 2 for 'LightsOff'

{

int duration4, distance4;

digitalWrite(trig1Pin, HIGH);

delayMicroseconds(1000);

digitalWrite(trig1Pin, LOW);

duration4 = pulseIn(echo1Pin, HIGH);

distance4 = (duration4/2) / 29.1;

if(distance4 <= 100 && distance4 >= 0)

{ if(counter==0) { digitalWrite(led1, LOW); break;

}

counter--;

Serial.println("Counter =" +counter);

if(counter==0) { digitalWrite(led1, LOW);

delay(3000);

Serial.println("Lights off");

digitalWrite(led, LOW);

t=t+1;

}

else if(counter!=0 && counter>0)

{

digitalWrite(led1, LOW);

break;

}

}

else if((millis() - time_since_last_reset) >= interval_two) //To turn off led1 if time runs out {

digitalWrite(led1, LOW);

}

}

}

delay(300);

}

Step 4: Edit As Much As You Want

Feel free to make changes and adjust the project according to your needs. If you use the code somewhere just give me some credit to make me happy. Sorry for the bad quality of this post but again I say I am a beginner. Put your suggestions and edits in the comments section below.