#3 Arduino Mega: Obstacle detection / People counter using IR Sensor | APDaga Tech

#3 Arduino Mega: Obstacle detection / People counter using IR Sensor | APDaga Tech
IoT Tutorial #3 [ Arduino Mega Tutorials #3 ]

Here I will demonstrate the mini project, obstacle detection / people counter using IR sensor using Arduino Mega (ATMega 2560).

Problem Statement:
Detect the obstacles / people using IR sensor and update the counter.

- Arduino Mega (ATMega 2560)
- USB Cable (A to B)
- Digital IR Sensor Module (ACK-SENSOR-IR-OBS-ARD)
  (Output of Module: 0 -> Obstacle Detected, 1 -> No Obstacle)
- 1 LED
- 1k Resistor
- Bread Board
- Male to Male wires (minimum 2)

Program Outcome:
Output is shown using LED
If Obstacle detected -> LED ON
If No Obstacle -> LED OFF

> Counter is incremented by 1 for every obstacle.
(Irrespective of the time for which obstacle is present)

> If Obstacle is detected then message and counter
is displayed on Arduino Terminal on Desktop using
serial communication (Tx0 and Rx0).
Program is written in such that the counter gets incremented 
by 1 only for 1 obstacle irrespective of the duration for
which obstacle is present.

> In technical terms, De-bouncing effect is removed.
Physical Connection:
Arduino Mega   ->   IR Module
Vcc -> IR Vcc
Pin 3 -> IR Out
Gnd -> IR Gnd

Pin 7 -> LED+ (Through 1K Resistor)
Gnd -> LED-

Serial Communication Through USB Cable (Tx0 and Rx0)
Download link is given in the Description of the YouTube video shown below.



const int pin_led_out = 7;
const int pin_ir_in = 3;
int prev_state =1;
int curr_state = 1;
int counter = 0;

void setup() {
// put your setup code here, to run once:
pinMode(pin_led_out, OUTPUT);
pinMode(pin_ir_in, INPUT);

void loop() {
// put your main code here, to run repeatedly:
int curr_state = digitalRead(pin_ir_in); // Check current state
if (curr_state != prev_state) // Detect Rising or Falling Edge
if(curr_state == 0) // IF Obstacle Detected
digitalWrite(pin_led_out, HIGH); // LED ON
counter++; // Increment the Counter
Serial.println("Obstacle Detected...!");
digitalWrite(pin_led_out, LOW); // LED OFF
prev_state = curr_state; // save prev state
delay(100); // Wait for 100 mSec.

Click here to see more codes for Arduino Mega (ATMega 2560) and similar Family.

Feel free to ask doubts in the comment section. I will try my best to solve it.
If you find this helpful by any mean like, comment and share the post.
This is the simplest way to encourage me to keep doing such work.

Thanks and Regards,
-Akshay P. Daga


  1. hello sir i am aparna my que is
    how to actually activate the sensor to detect the obstacle

Post a Comment
Previous Post Next Post