Bidirectional people counter with two IR sensors on Arduino: design worksheet and code

A ₹990 design worksheet for a doorway counter that knows whether a person is entering or leaving, using two IR sensors and a small state machine, with the timing maths for walking speed and sensor spacing.

About the author · Editorial & verification standards

  • Parts₹990
  • Bench time3–5 h
  • LevelIntermediate
  • ReviewedNov 2026

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

PartQtyUnitTotal
Arduino Uno R3 (or compatible)1₹550₹550
IR obstacle avoidance sensor module2₹40₹80
16×2 LCD with I2C backpack1₹250₹250
Breadboard1₹60₹60
Jumper wires1₹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 speedTime 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
Time = spacing / speed. A one second timeout covers even a very slow crossing.

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

PartConnects toNote
Sensor A (outer) OUTD2Most modules output LOW when blocked
Sensor B (inner) OUTD3
Sensors VCC and GND5V and GND
LCD SDA / SCLA4 / A5Address often 0x27 or 0x3F
Mount the two sensors on the same side of the door frame, pointing across the opening, one behind the other along the direction of walking.

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

  1. Sensor range: adjust each module until it triggers across the doorway and not on the far wall.
  2. Single entry and exit: walk through at a normal pace ten times each way and record the count.
  3. Slow and fast: try a slow shuffle and a quick step.
  4. Two people: walk through one behind the other and then side by side, and note the errors.
  5. Stopping in the beam: stand in the doorway for a few seconds and see what happens.
  6. Sunlight: check the sensors in bright light, which can disturb IR modules.

What usually goes wrong

SymptomLikely causeFix
Entry counted as exitSensors swappedSwap the pins
Counts twiceWaiting for clear missingKeep WAIT_CLEAR
Never countsRange too short or too longAdjust the module potentiometer
Miscounts in sunlightIR interferenceShade the sensors or use a different type
Side-by-side people read as oneOnly one beam across the widthAccept 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

By which beam is blocked first, the outer one for entering or the inner one for leaving.

In quiet use it can be close, but two people together, stopping, or crowded doors can break it.

So one person is not counted twice as they leave the beams.

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.

More builds

Next builds to try

Free: the final-year project pack

A shortlist with BOMs, a report outline that matches the usual chapter scheme, a costing sheet and 40 viva questions. Built for the February–April submission season.

The Build Note

One short email when something is worth your bench time.

A new build sheet, a part price that moved, or a mistake worth avoiding. No daily drip, no recycled news.

One short email when a build or a price is worth your time. Unsubscribe in one click.