Automatic Room Light Controller with Bidirectional Visitor Counter

Published  October 13, 2015   168
S Saddam
Author
Arduino Based Visitor Counter with Automatic Light Control

Often we see visitor counters at stadium, mall, offices, class rooms etc. How they count the people and turn ON or OFF the light when nobody is inside? Today we are here with automatic room light controller project with bidirectional visitor counter by using Arduino Uno. It is very interesting project for hobbyists and students for fun as well as learning.

 

Components

  • Arduino UNO
  • Relay (5v)
  • Resisters
  • IR Sensor module
  • 16x2 LCD display
  • Bread Board
  • Connecting Wires
  • Led
  • BC547 Transistor

 

The project of “Digital visitor counter” is based on the interfacing of some components such as sensors, motors etc. with arduino microcontroller. This counter can count people in both directions. This circuit can be used to count the number of persons entering a hall/mall/home/office in the entrance gate and it can count the number of persons leaving the hall by decrementing the count at same gate or exit gate and it depends upon sensor placement in mall/hall. It can also be used at gates of parking areas and other public places.

 

This project is divided in four parts: sensors, controller, counter display and gate. The sensor would observe an interruption and provide an input to the controller which would run the counter increment or decrement depending on entering or exiting of the person. And counting is displayed on a 16x2 LCD through the controller.

When any one enters in the room, IR sensor will get interrupted by the object then other sensor will not work because we have added a delay for a while.

IR Transmitter Working

Circuit Explanation

There are some sections of whole visitor counter circuit that are sensor section, control section, display section and driver section.

 

Sensor section: In this section we have used two IR sensor modules which contain IR diodes, potentiometer, Comparator (Op-Amp) and LED’s. Potentiometer is used for setting reference voltage at comparator’s one terminal and IR sensors sense the object or person and provide a change in voltage at comparator’s second terminal. Then comparator compares both voltages and generates a digital signal at output. Here in this circuit we have used two comparators for two sensors. LM358 is used as comparator. LM358 has inbuilt two low noise Op-amp.

IR Sensor Circuit

Control Section: Arduino UNO is used for controlling whole the process of this visitor counter project. The outputs of comparators are connected to digital pin number 14 and 19 of arduino. Arduino read these signals and send commands to relay driver circuit to drive the relay for light bulb controlling. If you find any difficulty in working with relay, check out this tutorial on arduino relay control to learn more about operating relay with Arduino.

 

Display section:  Display section contains a 16x2 LCD. This section will display the counted number of people and light status when no one will in the room.

 

Relay Driver section: Relay driver section consist a BC547 transistor and a 5 volt relay for controlling the light bulb. Transistor is used to drive the relay because arduino does not supply enough voltage and current to drive relay. So we added a relay driver circuit to get enough voltage and current for relay. Arduino sends commands to this relay driver transistor and then light bulb will turn on/off accordingly.

 

Visitor Counter Circuit Diagram

The outputs of IR Sensor Modules are directly connected to arduino digital pin number 14(A0) and 19(A5). And Relay driver transistor at digital pin 2. LCD is connected in 4 bit mode. RS and EN pin of LCD is directly connected at 13 and 12. Data pin of LCD D4-D7 is also directly connected to arduino at D11-D8 respectively. Rest of connections are shown in the below circuit diagram.

Visitor Counter Circuit Diagram using Arduino

 

Code Explanation

First we have included library for LCD and defined pin for the same. And also defined input output pin for sensors and ralay.

define

Then given direction to input output pin and initialized LCD in setup loop.

void setup

In loop function we read sensors input and increment or decrement the counting depending upon enter or exit operation. And also check for zero condition. Zero condition means no one in the room. If zero condition is true then arduino turn off the bulb by deactivating the relay through transistor.

loop

And if zero condition is false then arduino turns on the light. Here is two functions for enter and exit.

enter and exit function

 

Code

#include<LiquidCrystal.h>
LiquidCrystal lcd(13,12,11,10,9,8);

#define in 14
#define out 19
#define relay 2

int count=0;

void IN()
{
    count++;
    lcd.clear();
    lcd.print("Person In Room:");
    lcd.setCursor(0,1);
    lcd.print(count);
    delay(1000);
}

void OUT()
{
  count--;
    lcd.clear();
    lcd.print("Person In Room:");
    lcd.setCursor(0,1);
    lcd.print(count);
    delay(1000);
}

void setup()
{
  lcd.begin(16,2);
  lcd.print("Visitor Counter");
  delay(2000);
  pinMode(in, INPUT);
  pinMode(out, INPUT);
  pinMode(relay, OUTPUT);
  lcd.clear();
  lcd.print("Person In Room:");
  lcd.setCursor(0,1);
  lcd.print(count);
}

void loop()
{  
  
  if(digitalRead(in))
  IN();
  if(digitalRead(out))
  OUT();
  
  if(count<=0)
  {
    lcd.clear();
    digitalWrite(relay, LOW);
    lcd.clear();
    lcd.print("Nobody In Room");
    lcd.setCursor(0,1);
    lcd.print("Light Is Off");
    delay(200);
  }
  
  else
    digitalWrite(relay, HIGH);
  
}

Video

Comments

Submitted by juani on Tue, 10/20/2015 - 02:14

Permalink

Hi Saddam, could you please tell me where you got those 2 IR Sensor Modules from ?
Thank you in advance and best regards.
juani

Submitted by Bhanu Shukla on Thu, 11/05/2015 - 09:51

Permalink

The IR SEnsor modules I have are not giving any output, i think it's not compatible with arduino.
Please specify proper model number for IR Sensor Module so that I can buy specific one

Submitted by umar on Thu, 12/03/2015 - 15:22

Permalink

yar this programming is not working right because it does not count correctly .
when you keep your hand in front of IR module so it will continuous the counting .
But instead of count one it count continuous i think so their is problem in programm plz someone upload correct programm other than it it has no problem reply must

Submitted by abhinav on Sat, 01/09/2016 - 11:21

Permalink

on relay wire is connected on which two terminal
C and NC
OR C and NO

Submitted by akash m kashyap on Thu, 01/21/2016 - 22:04

Permalink

i used this code iny project but the range of the ir sesnor is only of 1-2cm soo i have to increase the distance so pls give me the code to increase the rangee..

Submitted by Deenkhar Beeja… on Thu, 02/04/2016 - 01:34

Permalink

Hello Sir,

Great project. I am current doing my final year project in BEng Electronic Engineering. I am doing a solar automatic lighting control system for a house. i found your project interesting so i tried add it to my house. BUT, when i finished mounting the circuit, the lcd count goes on constantly 0, -1,0,-1,0,-1...

now when i put my hand one of the sensor either the count keeps going from 0 to 1,2,3, etc... or it decrements to -1,-2,-3, etc...

can you advice?? i tried adjust the sensitivities but in vain.

thnx in advance.

Please check your circuit connections and code twice. Do not keep any unnecessary moving things in front of IR sensors. Try understanding the code, and change it accordingly. There is one IR pair for Entry detection and another for Exit detection, if you keep your hand in front of both the IR pair, then the number will continuously goes up and down. And the value shouldn't go negative as there is condition for that in the code, please check.

Hi! I have faced the same problem with you. I had tried to modify the code and still cannot get it correctly. Would you share your altered code with me? I really need your help to complete my final year project.

Hi Deenkhar Beejad...

I don't have a lot of programming experience, and I am struggling with fixing the code. Would you please be able to send your altered code?

Thanks,
N

I am facing the same problem on my project. Can you please send me the altered program which worked for you

Submitted by Sushmitha on Fri, 02/19/2016 - 13:04

Permalink

Hi
Its a nice simple yet effective project
I face troubles in relay connections
i used a 5-pin,5v relay
It would be of great help if you could mail me relay connections with the pin specs and reqd. voltages at each pin
thank you

sir i am facing problem in connecting relay with 5 leg can u please help me.

Submitted by Deenkhar Beeja… on Sun, 02/21/2016 - 16:43

Permalink

Hello Sir,

Is there anyway to prevent the counter from moving up or down when i place my hand infront of 1 of the sensors constantly? If ever i leave my hand infront of 1 of my sensors, the count eitheir goes up or down. So in a real case scenario, if someone stays infront of the door for sometime, either the count will go continuously up or down. and it will affect when the lights should go off.

Thanks.

Good thought, for this you can either increase the Delay in code or add some condition to avoid this, like only increase the count if IR sensor is switched from Off to ON state, and dont increases count if IR sensor is previously ON or continuously getting digitalRead(in). Try around some codes and share with us.

Submitted by mansi shukla on Fri, 03/11/2016 - 17:51

Permalink

sir, i m not able to get exact circuit diagram of automatic door opener using visitor counter....so, kindly cn u suggest some help all i get is room light controller every time.....cn i get circuit diagram of defined subject

Submitted by akash m kashyap on Thu, 03/17/2016 - 18:50

Permalink

i think there is some problem in the code because if the first senses then the count value got incremented and if second sensor is sensed n it wil get decremented. i mean to say if first sensor goes high the display will be one if the second sensor goes high n then value wil be 0 n lights wil be off wat shl i do to get rid of it

Submitted by naveen on Mon, 03/21/2016 - 17:54

Permalink

i have some more doughts please help me
after burning the code which lights in arduino board will glow or blink...?
after bulilding above circuit display is showing 1 and 0 continuously help me to solve this.....?

Submitted by Nekuie on Wed, 04/13/2016 - 12:34

Permalink

i have a question!
by using this circuit how we can have an accurate result? i mean repetitive visits can't be counted

Submitted by Laura on Thu, 04/14/2016 - 16:25

Permalink

i didnt not use the IR modules you have, i built my from scratch using IR LEDs, resisters, and 741 op-amp. when i plug my circuit into the arduino it counts on its own without the IR beam being broke. what kind of output signal is the arduino looking for? it also will count negative, what can i add to the code to prevent that?

Submitted by Faisal on Mon, 04/18/2016 - 13:48

Permalink

sir I am doing a mini project on your idea. but I am facing some problems that my LCD is not showing any single alphabet. I have checked all the circuitry, its fine. is there any change should made in code for this. actually sir I am from mechanical dept that's why I don't know much programming. can you please help me in this?

Submitted by prabhakaran on Tue, 04/19/2016 - 20:12

Permalink

if there any IR module available for proteus ....if available where can i download it..??
pls help me bro....

Submitted by shiva kumar on Sun, 05/15/2016 - 17:21

Permalink

i went through the program and what I didn't understand is how you you are determining if the person is leaving or entering the room. and also you seem to have connected the IR sensors to analog pins, so shouldnt it say " analogRead()" instead of "digitalRead()" ?
Thank You.

Submitted by Ateeq Anjum on Thu, 06/02/2016 - 21:16

Permalink

Hello everyone, i selected this project as final year project. but i also try to add more things... and i try to make it for a class room where the student and teacher separately count and total persons too. and for teachers count use a keypad. and also give me some idea which is possible to add in mine project..

Submitted by shiva on Fri, 06/17/2016 - 12:01

Permalink

hey, i made this project and i saw that the led that is operating through the relay glows when the relay goes LOW and vice versa. I checked my hardware and the wiring. Why is that?

Submitted by charlei on Tue, 07/12/2016 - 03:10

Permalink

please send me the program you used while codding coz am having troubles with ir sensors ,they are counting the no of people in the room continously
thnks

Submitted by hareesh tm on Wed, 07/20/2016 - 10:53

Permalink

Can you please get me the modified code for counting the total number of persons entered in the room for a particular period.?

Submitted by Sk.Kamrul Hassan on Wed, 08/10/2016 - 21:11

Permalink

Sir,
I am from Bangladesh, we have a problem with electricity in our country.
Can u help me to save last status in epprom at ATMEGA328 ? If you pls send me..

Submitted by Riku on Wed, 09/28/2016 - 11:59

Permalink

Sir can you send me all detail about this project like source code, component and etc because i want make this project for project last year for my college

Submitted by Riku on Fri, 09/30/2016 - 15:02

Permalink

Sir plz send me correct code ,no ic arduino uno

Submitted by AMANJEET on Sat, 10/22/2016 - 15:17

Permalink

Sir, how will the code change accordingly if I use microcontroller 8051 in place of aurdino board.please suggest me if there is modification needed on this program.

Submitted by Jonas Dautel on Tue, 11/08/2016 - 22:19

Permalink

hey i created a ne verison ofyour code just place it into your exciting code ..it will count the people WARNING the pins i used for the sensors ar 3 and 4 . 3 is the one you pass first wen you go in :
boolean sensor1 = false ;
boolean sensor2 = false ;
int count = 0 ;

void setup() {
// put your setup code here, to run once:
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(2, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
delay (200);
sensor();
if (sensor1 == true) {
S1();
}
//if (sensor2 == true){
// S2();
//}
}
void sensor() {
if (digitalRead(3) == HIGH) {
sensor1 = true;
}
else if (digitalRead(3) == LOW) {
sensor1 = false;
}
if (digitalRead(4) == HIGH) {
sensor2 = true;
}
else if (digitalRead(4) == LOW) {
sensor2 = false;
}
else {
loop();}
}
void S1() {
while (sensor1 == true && sensor2 == false) {
sensor();
}
if (sensor1 == false && sensor2 == false) {

}
else if (sensor1 == true && sensor2 == true) {
S1S2();
}
else {
loop();
}
}
void S1S2() {
while (sensor1 == true && sensor2 == true) {
sensor();
}
if (sensor1 == false && sensor2 == true){
S2F();
}
else if (sensor1 == true && sensor2 == false){
S1();
}
else {
loop();
}
}

void S2F() {
while (sensor2 == true && sensor1 == false){
sensor();
}
if (sensor2 == false && sensor1 == false){
count = count + 1;
loop();
}
else if (sensor1 == true && sensor2 == true){
S1S2();
}
else {
loop();
}

}
void S2() {
while (sensor2 == true && sensor1 == false) {
sensor();
}
if (sensor1 == false && sensor2 == false) {

}
else if (sensor1 == true && sensor2 == true) {
S2S1();
}
else {
loop();
}
}
void S2S1() {
while (sensor2 == true && sensor1 == true) {
sensor();
}
if (sensor2 == false && sensor1 == true){
S1F();
}
else if (sensor2 == true && sensor1 == false){
S2();
}
else {
loop();
}
}

void S1F() {
while (sensor1 == true && sensor2 == false){
sensor();
}
if (sensor1 == false && sensor2 == false){
count = count - 1;
loop();
}
else if (sensor2 == true && sensor1 == true){
S1S2();
}
else {
loop();
}

}

Sir what exactly does your modified code do? And how did you implement it physically like did you place two sensors at the same door at some distance to track the count? Also, can the sensors be deployed vertically on the ceiling instead of horizontally?

 And how did you implement it physically like did you place two sensors at the same door at some distance to track the count? 

No the code is already fully functional. You just have to build it on your own

 

Also, can the sensors be deployed vertically on the ceiling instead of horizontally?

Yes you can mount it vertically also it will work. But it will not suit the application