Automatic Room Light Controller with Bidirectional Visitor Counter

Published  October 13, 2015   176
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

Have any question realated to this Article?

Ask Our Community Members

Comments

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

i am aren't able to understand your code . please can you add the fuctionality of lcd so that the counted person can be displayed on lcd . please i neeed it as early as possible because i am giong to sumbit my project within this week at uni

Submitted by ebenezer on Wed, 11/09/2016 - 22:41

Permalink

pls check and send the full program and step by step process in cricuit diagram how to fix in everything

Submitted by Andrews on Fri, 11/11/2016 - 00:38

Permalink

Sir relay connections probs but i have given the conection for relay as u did but the lcd shows values correctly but led is not turning on and telay not working

Submitted by Abhishek on Mon, 11/21/2016 - 11:08

Permalink

Hi, wonderful project idea and demonstration, I have bought all the required components, however I wish to use LED or an array of LEDs in stead of 220v AC bulb, what should I do to make it work? Like in your video I see a relay is attached to the breadboard, however I am bit confused about LED, should I directly connect LED in place of bulb(bad idea?) or relay?

Submitted by May on Tue, 11/29/2016 - 15:41

Permalink

hello guys
is there is any way to set arduino to count when the sensor didnt get any signal

I mean exactly On the contrary of this program when the IR module didnt get any IR signal instead of getting signal ?

please help

Submitted by goverdhan on Tue, 11/29/2016 - 16:40

Permalink

explain about relay connection(where the relay pins are connectd)

Submitted by arindam barat on Sun, 12/04/2016 - 10:38

Permalink

can i change ir to pir ?if i change then what need to change in ckt or programming/?

Submitted by Remix23 on Mon, 12/12/2016 - 00:11

Permalink

Hi there,

Your project is really amazing. I would like to build it myself just to have some hands on experience.

Could you please tell me what ohms are your resistors? and you have connected the circuit to the diagram as shown above?

Thanks

Submitted by Abdulaziz on Mon, 12/26/2016 - 00:06

Permalink

If you copied and pasted the code that is provided in this project and had a problem where the counter increases without any object passing through the ir module and when it passes the counter stope adjust the code like this:
if(digitalRead(in))
IN();
if(digitalRead(out))
OUT();
// will be like this:
while(digitalRead(in) == LOW){
IN();
break;}
if(digitalRead(out) == LOW){
OUT();
break;}
// define in as pin 3 and out as pin 4 on digital pins

Submitted by Jaylord on Thu, 01/19/2017 - 16:51

Permalink

thank you for this project sir. but i have a little bit of problem the counting that I saw is not stable it increases the original count and decreases immediately in every second..how will i fix it sir..thanks in advance...

Submitted by namrata on Wed, 02/01/2017 - 19:41

Permalink

why use ir sensor not proximity and pir sensor

Submitted by SAMI ULLAH on Sun, 03/19/2017 - 12:50

Permalink

I have created a new version on this project for my first semester final project. Intially idea i get from this web then worked on it finally i made it . My version is more sensible and i have used direct incidence of ir sensor . These direct incidence sensor could be made at home very easily . if any body want any help regarding this project he may ask.

Submitted by elyse on Wed, 03/22/2017 - 04:09

Permalink

i want to use 3 relays in a program to control more lights but i have used one relay and i want u help me to add them in program help me please
the program is below
#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);

}

Submitted by guettaf fouzi on Tue, 05/02/2017 - 01:33

Permalink

you must change command
if(digitalRead(out))
to
if(digitalRead(out) && count>0)
for void decrementation when you pass throug sensor 2 while count=0

Submitted by Jambulingam Hemanth on Mon, 05/15/2017 - 09:34

Permalink

Sir I am in need of using the sensor range for about 3-10 ft for sensing range. So I have decided to use ultrasonic sensor for that range of measurement. Can i use ultrasonic sensor for this project? if not can you suggest me any other sensor?

Submitted by Avigyan Roy on Tue, 05/23/2017 - 02:37

Permalink

while doing this experiment the problem that i am facing is in my lcd continuously no. of person is getting changed in absence of any input.
That means when i bring my hands near to the either of the sensors it performs according to the porgram but the moment i am removing my hand(that means there is nothing to be sensed) lcd starts showing an infinite loop of 0 and 1 and it continues untill i again bring my hand near sensor.
please give me the solution.

Submitted by ash on Thu, 06/08/2017 - 00:14

Permalink

i have checked the connections .. they are correct..
when i run the code , the screen displays 'no. of persons in the room',
but when i give an input like move my hand across the sensor, the screen goes blank and comes back on when i remove my hand..

plz tell me what to do..
i have a week before i have to submit the project and its too late to change it.
i appreciate all the help i can get.

Submitted by munezero erienne on Wed, 06/14/2017 - 01:57

Permalink

how you can limit the peaple in the room for example if you want only 10 peaple in the room ,which code you can use it.

Submitted by Karan on Fri, 07/14/2017 - 16:31

Permalink

Hi. You've shown the connection of Sensors Output to Analog pins of Arduino. But in code, you've used digitalRead. Shouldn't it be analogRead because its connected to Analog pins. Thanks

Submitted by surmit chauhan on Wed, 08/16/2017 - 15:44

Permalink

I have connected all the components as mentioned but there is a problem when i switch it on the circuit is not working and text displaying is fluctuating Person in room 0,1,0,1..... these 1 and 0 are coming and going again and again

Submitted by shaku on Wed, 09/20/2017 - 21:08

Permalink

hello admin is this the correct code or we need to alter something??????
reply soon plz

Submitted by Subashini on Sun, 10/08/2017 - 10:49

Permalink

I am new to Arduino. I have given the same circuit connection and uploaded the program. but the counter is changing between 0 and 1 automatically. Where i have done mistake. how to find?

Submitted by Allen Paul Antony on Mon, 10/09/2017 - 17:36

Permalink

The Red led on my sensor module is always on standby. when we put our hands near the module the LED BLINKS AND AGAIN TURN ON. Some times the number of persons simply goes on increasing or decreasing. I doubt it is not working according to the sensor input.
Please look into my problem, I want to submit the project within 2 days.

Submitted by Allen Paul Antony on Wed, 10/11/2017 - 23:45

Permalink

The red colour led of my ir sensor module is always turned on, and when i move my hands infront the led blinks and again turn on. what change should i make in coding. Here is my coding
#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);

}

Submitted by praneeth on Wed, 10/18/2017 - 16:28

Permalink

I have connected a Led instead of the Relay but the Led Keeps blinking even when there is no count.
When the count is one or more the Led blinks in a fast rate. Please help

Submitted by praneeth on Wed, 10/25/2017 - 10:40

Permalink

The count keeps varying ... When the count is 1 the arduino count keeps varying between 1 and 0 . So the output relay keeps turning ON and OFF. Same thing occurs when the count is two the count keeps varying between 2 and 1
Please help!!

Hi Praneeth, the problem is most likely because your IR module is making a false trigger. Use the Serial monitor to check which creates a false trigger. Maybe a pull down resistor or 10K to the signal pin or IR module should solve the problem

Submitted by NAMRATHA B RAJ on Sat, 10/28/2017 - 19:40

Permalink

Can we add PIR Sensor along with IR sensor to this project. IR sensor for counting the persons and PIR Sensor for turning on the light on particular area in a large area/hall. So can we use PIR And IR sensor in this project. If it is possible can you explain me how can we do it.

Submitted by praneeth on Tue, 11/07/2017 - 17:20

Permalink

Thank you Aisha, Your idea was the solution, where the threshold of the IR input was crossing continuously. Adding 10 K resistor in series with the IR sensor worked.
Thanks again
by the way great project and thanks for everyone contributing to this.

Submitted by Alvin Avarachan on Mon, 11/13/2017 - 20:30

Permalink

Sir , i need the entire circuit of above showed video of automatic room light controller.
Will u please say about the maximum distance provided between 2 ir modules.
And also gave the connection to the 5v relay .
I am waiting for ur reply

Submitted by khalees on Sun, 11/19/2017 - 13:03

Permalink

please is the IR the same thing with IR proximity sensor? i did search it online in our local electronic store that is what the are giving to me

Submitted by Francis obeng on Wed, 01/10/2018 - 05:20

Permalink

please am using this as my project and won't to add a buzzer alarm so if the number in a room reach a certain number let say 100 it will sound to indicate room is full.pls can u help me with the cold. and the IR can u please give me the specific model

Submitted by nagraj on Sun, 01/21/2018 - 12:22

Permalink

how can i send the data of no. of persons in the room using mobile phone?
how can i add a limit for no. of persons to enter into the room and also voice alarm if count exceeds?
Please reply me as soon as possible

Submitted by Ernzl Estrada on Wed, 02/21/2018 - 10:54

Permalink

hay sir good day.. the arduino ide needs an drivers to run ??

Submitted by Aman Sahoo on Mon, 03/26/2018 - 09:35

Permalink

The code count the also update the person in negative value so try this 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)
{
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);

}

Submitted by Aravindhan on Mon, 03/26/2018 - 20:31

Permalink

my problem is... when (<=1) person enters the room , one led should lights up. when (<=2) person enters the room , two led should lights up...
can anyone plz provide me code over this.......

Submitted by Rahul on Fri, 03/30/2018 - 21:08

Permalink

I have trouble in output in LCD display plz advice me connection properly .
And give me proper correct Codeing
Plz help me sir

Submitted by Mahesh Chavan on Sat, 03/31/2018 - 19:40

Permalink

How to add IR Module in proteus software?

Submitted by Rahul on Mon, 04/16/2018 - 11:49

Permalink

My lcd display is not getting output it is seen blank.not a seen count or any output in lcd display pls help me what is solution of this.

Submitted by Dev on Fri, 04/20/2018 - 18:05

Permalink

I made this project but there some prblm...relay is not operated automatically ...and major prblm is that if we run the code then LCD will shows everything but there fluctuation in counting 1st count 1 and if are goes 2nd time it will show 2 but then 1 2 1 2 fluctuates please send perfect code ....i have a very less time ...to submit my project ...

Submitted by Mayuresh on Sun, 04/22/2018 - 23:56

Permalink

I've made all proper connections and even the LED's and sensors are working perfectly, but the LCD doesn't show anything even though it is ON.

Submitted by Harshil on Fri, 05/04/2018 - 08:59

Permalink

You have connected those input of ir sensor to analog pins in circuit diagram but according to your code explanation it should be connected to digital pins

Submitted by Prakash on Wed, 06/06/2018 - 08:27

Permalink

Sir Send me the total components list with there values and how many components are need.

Submitted by Arpan on Fri, 07/06/2018 - 15:09

Permalink

This is my first Arduino project attempt. I've tried to reduce some part of the code to eliminate the LCD display part. Can anyone tell if the following code is correct?

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

int count=0;

void IN()
{
    count++;
    delay(1000);
}

void OUT()
{
  count--;
    delay(1000);
}

void setup()
{
  pinMode(in, INPUT);
  pinMode(out, INPUT);
  pinMode(relay, OUTPUT);
}

void loop()
{  
  
  if(digitalRead(in))
  IN();
  if(digitalRead(out))
  OUT();
  
  if(count<=0)
    digitalWrite(relay, LOW);
  else
    digitalWrite(relay, HIGH);
  
}

Pls i need help modifying the code. is there a way to make the relay and break the relay such as activating a push button switch to control RF light source

if(count<=0)
{
count=0;
lcd.clear();
digitalWrite(relay, HIGH); Then low again to simulate a push control.

else

digitalWrite(relay, HIGH); Then low again to simulate a push control.

thanks

Sir pls send me the "correct" or updated source code for counting the people. I try the source code that mentions in the previous source code but it won't works.

The issue shows counting the people 0 and 1 before count the people