A ₹990 counter for a doorway. Two IR obstacle sensors sit a few centimetres apart across the door frame. If the outer sensor is blocked first and then the inner one, the person is entering; if it is the other way round, they are leaving. A small state machine counts them and shows the number inside on a screen.
Scope: intermediate. This is a design worksheet with illustrative costs. It has not been bench-tested by this site. The timing table was computed by script. Real doorways have two people walking side by side, people who stop in the beam, and children and bags, which all confuse a two-beam counter. This is a good learning project and a fair estimate in quiet use, not an accurate headcount for safety or billing.
How it works
With the sensors 10 cm apart along the walking direction, a person walking at 1 m/s blocks the second beam 100 ms after the first. At 0.5 m/s the gap is 200 ms, and at 1.5 m/s it is about 67 ms. The sketch only has to check the sensors faster than that; a loop that runs thousands of times a second is plenty.
The state machine has four states. Idle waits for a beam to be blocked. If sensor A is blocked first, it waits for sensor B and counts an entry. If B is first, it waits for A and counts an exit. A timeout of one second cancels half-finished crossings, and after a count the machine waits for both beams to clear before it listens again, so the same person is never counted twice.
Bill of materials
| Part | Qty | Unit | Total |
|---|---|---|---|
| Arduino Uno R3 (or compatible) | 1 | ₹550 | ₹550 |
| IR obstacle avoidance sensor module | 2 | ₹40 | ₹80 |
| 16×2 LCD with I2C backpack | 1 | ₹250 | ₹250 |
| Breadboard | 1 | ₹60 | ₹60 |
| Jumper wires | 1 | ₹50 | ₹50 |
| Parts total | ₹990 | ||
Scroll the table sideways to see every column.
Prices checked Nov 2026. Shipping and tools are not included.
Prices are illustrative figures for Indian sellers in November 2026, before shipping, and have not been checked against current listings.
The numbers
| Walking speed | Time between beams (10 cm apart) |
|---|---|
| 0.5 m/s (slow) | 200 ms |
| 1.0 m/s (normal) | 100 ms |
| 1.5 m/s (brisk) | 67 ms |
| 2.0 m/s (hurried) | 50 ms |
If the sensors were 30 cm apart, a person at 1 m/s would cross in 300 ms, which is easier to measure but lets two people follow each other and be read as one. Closer sensors resolve people better; further sensors tolerate slow sampling. 10 cm is a reasonable starting point.
Power and wiring
| Part | Connects to | Note |
|---|---|---|
| Sensor A (outer) OUT | D2 | Most modules output LOW when blocked |
| Sensor B (inner) OUT | D3 | |
| Sensors VCC and GND | 5V and GND | |
| LCD SDA / SCL | A4 / A5 | Address often 0x27 or 0x3F |
The sketch
// Bidirectional people counter: Uno + 2 IR sensors + I2C LCD
// Design worksheet: a two-beam counter is only approximate.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int PIN_A = 2, PIN_B = 3; // A outer, B inner
const unsigned long TIMEOUT_MS = 1000UL;
LiquidCrystal_I2C lcd(0x27, 16, 2);
enum State { IDLE, A_FIRST, B_FIRST, WAIT_CLEAR };
State state = IDLE;
unsigned long t0 = 0;
int count = 0;
void show() {
lcd.setCursor(0, 1);
lcd.print("Inside: ");
lcd.print(count);
lcd.print(" ");
}
void setup() {
pinMode(PIN_A, INPUT); pinMode(PIN_B, INPUT);
lcd.init(); lcd.backlight();
lcd.setCursor(0, 0); lcd.print("Door counter");
show();
}
void loop() {
bool a = (digitalRead(PIN_A) == LOW); // blocked
bool b = (digitalRead(PIN_B) == LOW);
unsigned long now = millis();
switch (state) {
case IDLE:
if (a && !b) { state = A_FIRST; t0 = now; }
else if (b && !a) { state = B_FIRST; t0 = now; }
break;
case A_FIRST:
if (b) { count++; show(); state = WAIT_CLEAR; }
else if (now - t0 > TIMEOUT_MS) state = IDLE;
break;
case B_FIRST:
if (a) { if (count > 0) count--; show(); state = WAIT_CLEAR; }
else if (now - t0 > TIMEOUT_MS) state = IDLE;
break;
case WAIT_CLEAR:
if (!a && !b) state = IDLE;
break;
}
}Count never goes below zero, which hides some errors but keeps the display sensible. If entries and exits are swapped, swap the two sensor pins. The IR modules have a potentiometer for range: set it just long enough to cross the doorway, so they do not see the opposite wall. The sketch has been proofread but not compiled on this site.
Test plan
- Sensor range: adjust each module until it triggers across the doorway and not on the far wall.
- Single entry and exit: walk through at a normal pace ten times each way and record the count.
- Slow and fast: try a slow shuffle and a quick step.
- Two people: walk through one behind the other and then side by side, and note the errors.
- Stopping in the beam: stand in the doorway for a few seconds and see what happens.
- Sunlight: check the sensors in bright light, which can disturb IR modules.
What usually goes wrong
| Symptom | Likely cause | Fix |
|---|---|---|
| Entry counted as exit | Sensors swapped | Swap the pins |
| Counts twice | Waiting for clear missing | Keep WAIT_CLEAR |
| Never counts | Range too short or too long | Adjust the module potentiometer |
| Miscounts in sunlight | IR interference | Shade the sensors or use a different type |
| Side-by-side people read as one | Only one beam across the width | Accept the limit or add sensors across the width |
Upgrades
- Lamp or fan control: switch a relay while the count is above zero, with a safe mains design.
- Logging: keep counts per hour on an SD card.
- Wi-Fi: use an ESP32 to publish the count.
- Better sensing: use time-of-flight sensors that give distance as well.
Cost and parts notes
The ₹990 total is an illustration and does not include shipping. Use the cost calculator to compare sellers.
Common questions
How does it know the direction?
By which beam is blocked first, the outer one for entering or the inner one for leaving.
Is it accurate?
In quiet use it can be close, but two people together, stopping, or crowded doors can break it.
Why wait for the beams to clear?
So one person is not counted twice as they leave the beams.
Why 10 cm spacing?
It makes the gap between beams long enough to measure and short enough to separate people.
Last reviewed: November 2026. This is a design worksheet with illustrative costs. It has not been bench-tested by this site; library names, pins and limits must be checked against your board and the current library versions, and measurements from your build are welcome.