Obstacle Detection using IR Sensor with Arduino. (Bidirectional Visitor Counter example)

Obstacle Detection using IR Sensor with Arduino. (Bidirectional Visitor Counter example)
Obstacle detection using IR sensors with Arduino has a number of applications in robotics, security systems and industrial assembly lines. In this tutorial I will show how the FC-51 IR sensor works and how these sensors are used for visitor counting in spaces like conference rooms, classrooms, stadia and others.

How the IR Sensor works.

We are using the FC-51 IR sensor which has a builtin IR transmitter and IR receiver that sends out IR energy and looks for reflected IR energy to detect presence of any obstacle in front of the sensor module. The module has on board potentiometer that lets user adjust detection range.
fc-51 ir sensor parts and working
Infrared Transmitter is a light emitting diode (LED) which emits infrared radiations. Hence, they are called IR LED’s. Even though an IR LED looks like a normal LED, the radiation emitted by it is invisible to the human eye.
Infrared receivers detect the radiation from an IR transmitter. IR receivers come in the form of photodiodes and phototransistors. Infrared Photodiodes are different from normal photo diodes as they detect only infrared radiation.
The basic concept of an Infrared Sensor which is used as Obstacle detector is to transmit an infrared signal, this infrared signal bounces from the surface of an object and the signal is received at the infrared receiver.

Schematic for connecting the IR Sensor with Arduino.

The IR sensor VCC and GND pins are connected to the corresponding pins of the Arduino board and the OUT pin of the sensor is connected to Arduino pin 6.
We have included a buzzer and an Led connected to pin 5 and pin 4 of the Arduino board respectively.

Code for IR sensor with Arduino.

This code is basically for reading the state of pin 6 where the IR sensor is connected. This is because the sensor gives logic LOW as output when there is no obstacle in front of it, and when an obstacle is in front of it, it will give logic HIGH output.
#define led 4            // led at pin 4
#define buzzer 5      // buzzer at pin 5
#define sensor 6      // ir sensor at pin 6
int sound=250;         // set buzzer sound
void setup()
{
Serial.begin(9600);
pinMode(sensor,INPUT);
pinMode(led,OUTPUT);
pinMode(buzzer,OUTPUT);
}

void loop()
{
int detect=digitalRead(sensor);   // read status of sensor
if(detect==HIGH)               // if sensor detects obstacle
{
digitalWrite(led,HIGH);         // led on
tone(buzzer,sound);            // buzzer sounds
}
else{
digitalWrite(led,LOW);
noTone(buzzer);
}
delay(300);

}    
On uploading the code to the Arduino board, when an object gets close to the sensor, the led turns on and the buzzer sounds.

Bidirectional Visitor Counter.

IR sensors object detection can help us in a very important application which is the Bidirectional Visitor counter which is vital in automatic room control. This system enables us to automatically control a number of factors like lighting, heating, cooling and other aspects which may depend on the number of people in a given space or room.
In this project I will use two IR sensors for detecting movement of the person entering or leaving the room. There is also a 16×2 LCD for showing the number of people and a relay module for controlling the lighting of a light bulb.
The way this setup works is that when there is no one in the room, the light bulb will be off and the LCD shows that there is no visitor. If a person enters the room, the light will automatically turn on and the LCD will begin showing the number of people in the room as they are being detected by the IR sensors.

Arrangement of IR sensors for the Bidirectional Visitor Counter.

The sensors can be arranged in a number of ways depending on how you want the set up to work.

One IR sensor at each entrance and exit.

The basic set up will be to place one IR sensor at the entrance and another at the exit so that when a person enters the room, the counter is increased and when the person leaves the counter decreases.
one ir sensor at each entrance and exit

Code for the above setup of IR sensors.

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 

#define in 7
#define out 8
#define relay 3

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.backlight();
  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);
  
} 
This set up has some major limitations and the first one is that when a person stands near the sensor with out entering or leaving the room, the counter will continue increasing or decreasing because the IR sensor is continuously detecting the presence of the person!

Two IR sensors at each entrance and exit.

To overcome the challenges that arise from the first setup suggested above, you can place two IR sensors at intervals on the entrance of the room. In this case the counter will increase or decrease depending on the direction of movement of the person and this is determined by which of the two sensors detects the person first.
two ir sensors at each entrance or exit
Also the counter will not change if both the sensors have not detected the person which helps to solve the situation that we were facing earlier of the counter increasing or decreasing even when the person was just standing in one position without entering or leaving the room.

Code for the above setup of IR sensors.

 #include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 
#define sensorPin1 7
#define sensorPin2 8
#define relay 3

int sensorState1 = 0;
int sensorState2 = 0;
int count=0;

void setup()
{
  pinMode (sensorPin1, INPUT_PULLUP);
  pinMode (sensorPin2, INPUT_PULLUP);
  pinMode(relay, OUTPUT);
  
  lcd.begin(16,2);
  lcd.backlight();
  lcd.setCursor(4,0);
  lcd.print("COUNTER");
   lcd.setCursor(0,1);
  lcd.print("No Visitors     ");
  delay(200);
}

void loop()
{  
  sensorState1 = digitalRead(sensorPin1);
  sensorState2 = digitalRead(sensorPin2);

  if(sensorState1 == LOW){
    count++; 
    delay(500);
  }

  if(sensorState2 == LOW){
    count--; 
    delay(500);
  }
  

   if(count<=0)
  {
    digitalWrite(relay, LOW); 
    lcd.setCursor(0,1);
    lcd.print("No visitors    ");
  }
  else if (count>0 && count<10){
    digitalWrite(relay, HIGH);
    lcd.setCursor(0,1);
    lcd.print("Visitors:   ");
    lcd.setCursor(12,1);
    lcd.print(count);
    lcd.setCursor(13,1);
    lcd.print("  "); 
    
  }
  else {
    digitalWrite(relay, HIGH);
    lcd.setCursor(0,1);
    lcd.print("Visitors:   ");
    lcd.setCursor(12,1);
    lcd.print(count);  
  }
}
 

One thought on “Obstacle Detection using IR Sensor with Arduino. (Bidirectional Visitor Counter example)

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top