nocturnal0608
Published © GPL3+

Automatic Room Lights Control

This device is a proximity based device which keeps count of the number of people in a room and turns off the light when the room is empty.

IntermediateProtip638
Automatic Room Lights Control

Things used in this project

Hardware components

Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×2
Breadboard (generic)
Breadboard (generic)
×2
Jumper wires (generic)
Jumper wires (generic)
×1
Alphanumeric LCD, 16 x 2
Alphanumeric LCD, 16 x 2
×1
Arduino UNO
Arduino UNO
×1
LED (generic)
LED (generic)
×1
Resistor 220 ohm
Resistor 220 ohm
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Overview diagram of the device

Code

two_ultsonic_with_people_counter_final.ino

Arduino
Uses the tripwire concept to detect if person is entering or leaving the room.
#include <LiquidCrystal.h>
const int rs = 13, en = 6, d4 = 4, d5 = 10, d6 = 9, d7 = 7; 
int trigPin1 = 2;
int echoPin1 = 3;
int led1 = 8;
int trigPin2 = 12;
int echoPin2 = 11;
int loop_count = 0;
int counter1 = 0;
int counter2 = 0;
int people_count = 0;
int s1 = 0 , s2 = 0;
int s1_prev = 0 , s2_prev = 0;
int led2 = 5; 

void setup() {
  Serial.begin(9600); 
   pinMode(led1, OUTPUT);
   pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
   pinMode(trigPin2, OUTPUT);
    pinMode(echoPin2, INPUT);
    pinMode(led2, OUTPUT);
   // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("No of People :");

}

void loop() 
{
  long duration1, distance1, distance2, duration2;
  digitalWrite(trigPin1,HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin1, LOW);
  duration1=pulseIn(echoPin1, HIGH);
  distance1 =(duration1/2)/29.1;
  //Serial.print("distance 1: ");
  //Serial.print(distance1);
  //Serial.println("CM");
  delay(10);
  digitalWrite(trigPin2,HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin2, LOW);
  duration2=pulseIn(echoPin2, HIGH);
  distance2 =(duration2/2)/29.1;
  //Serial.print("distance 2: ");
  //Serial.print(distance2);
  //Serial.println("CM");
  delay(10);
 
 if(distance1<=50)
  {
    digitalWrite(led1, HIGH);
    s1 = 1;
  }
   else if(distance1>50)
  {
     digitalWrite(led1, LOW);
     s1 = 0;
   }
   if (distance2<=50)
   {
    digitalWrite(led1, HIGH);
    s2 = 1;
    }
   else if(distance2>50 )
   {
    digitalWrite(led1, LOW);
    s2 = 0;
   }
   loop_count++;
   if ((s1_prev ==0) && (s1 == 1))
     {
      counter1 = loop_count;
     }
   if ((s2_prev == 0) && (s2 == 1))
   {
      counter2 = loop_count;
    }
   s1_prev = s1;
   s2_prev = s2;

   if ((counter1>0) && (counter2>0))
      {
        if (counter1>counter2)
        {
          people_count ++;   
        }
        else
        {
          people_count --;
        }
        if (people_count < 0)
        {
          people_count = 0;
        }
    if (people_count>0)
    {
    digitalWrite(led2, HIGH);  
    }
     else
     {
      digitalWrite(led2, LOW);
     }
       
        Serial.print("Counter 1 :");
        Serial.println(counter1);
        Serial.print("Counter 2 :");
        Serial.println(counter2);
        Serial.print("Number of people : ");
        Serial.println(people_count);
        counter1 = 0;
        counter2 = 0;
      }

  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(people_count);
}

Credits

nocturnal0608
0 projects • 0 followers

Comments