Projects / Bidirectional visitor counter with room light

Bidirectional visitor counter with room light

Intermediate1.3 hGuided buildReviewed

Build it step by step

0 of 4 done
  1. UNO

    Mount the sensors

    Fix both sensors on one side of the doorway at waist height, about 10 to 15 cm apart along the direction of walking. The outer one faces the corridor side.

  2. UNO10.2 mA

    Set their range

    Turn each sensor's trimmer so its LED lights when a person passes about 50 cm away, but not from the opposite wall.

  3. UNO10.2 mAPCF8574ON

    Wire

    Outer to D2, inner to D3, LCD on A4/A5, relay on D7 switching the 12 V strip.

  4. UNO

    Test

    Walk in slowly, then out. Then try two people one after the other.

In short

How do you build a bidirectional visitor counter with room light?

Two IR sensors at a doorway tell whether someone walked in or out, count the people inside on an LCD, and switch a light on while the room is occupied. You need Arduino Uno R3, IR obstacle avoidance sensor module × 2, 16x2 LCD with I2C backpack (PCF8574), Relay Module 5V, 12 V LED strip warm white (5 m, 2835), 12 V 2 A DC adapter, DC barrel to screw terminal adapter (female) and Jumper wires male to female (40 pcs). It takes 4 steps and about 75 minutes.

Level
Intermediate
Time
About 75 min
Parts
8

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Outer sensor OUTUno D2LOW when something passes on the corridor side
Inner sensor OUTUno D3LOW when something passes on the room side
Sensor VCC / GNDUno 5V / GNDPower
LCD SDA / SCLUno A4 / A5Count
Relay INUno D7Room light
12 V +Relay COM, NO to LED strip +Light switched by the relay

Code

Arduino IDE, board "Arduino Uno". Install LiquidCrystal I2C.

Runs on a virtual Arduino Uno in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
const int OUTER = 2, INNER = 3, RELAY = 7;
const bool ON = LOW;
int inside = 0, entered = 0;
int first = 0;                       // which sensor tripped first: 0 none, 1 outer, 2 inner
unsigned long firstAt = 0;

void show() {
  lcd.setCursor(0, 0); lcd.print("Inside: "); lcd.print(inside); lcd.print("     ");
  lcd.setCursor(0, 1); lcd.print("Today:  "); lcd.print(entered); lcd.print("     ");
  digitalWrite(RELAY, inside > 0 ? ON : !ON);
}

void setup() {
  pinMode(OUTER, INPUT); pinMode(INNER, INPUT);
  pinMode(RELAY, OUTPUT);
  lcd.init(); lcd.backlight(); show();
}

void loop() {
  bool o = digitalRead(OUTER) == LOW, i = digitalRead(INNER) == LOW;
  if (first == 0) {
    if (o) { first = 1; firstAt = millis(); } else if (i) { first = 2; firstAt = millis(); }
  } else {
    if (first == 1 && i) { inside++; entered++; }           // outer then inner: walked in
    if (first == 2 && o && inside > 0) inside--;             // inner then outer: walked out
    if ((first == 1 && i) || (first == 2 && o) || millis() - firstAt > 1500) {
      show();
      while (digitalRead(OUTER) == LOW || digitalRead(INNER) == LOW) {}   // wait until the doorway is clear
      delay(200);
      first = 0;
    }
  }
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Walk inInside 1, Today 1, light on
Walk outInside 0, light off
Stop halfway and step backNo change after 1.5 s

If it does not work

Counts the wrong way

Swap the D2 and D3 wires.

Counts twice

Sensors too far apart or set too far. Move them closer and shorten their range.

Misses people

IR modules struggle with black clothes and sunlight. Use them indoors, or use two VL53L0X sensors for longer range.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the bidirectional visitor counter with room light?

Arduino Uno R3, IR obstacle avoidance sensor module × 2, 16x2 LCD with I2C backpack (PCF8574), Relay Module 5V, 12 V LED strip warm white (5 m, 2835), 12 V 2 A DC adapter, DC barrel to screw terminal adapter (female) and Jumper wires male to female (40 pcs).

How long does the bidirectional visitor counter with room light take to build?

About 75 minutes for a intermediate maker, in 4 steps.

How do I know the bidirectional visitor counter with room light works?

Walk in: Inside 1, Today 1, light on. Walk out: Inside 0, light off. Stop halfway and step back: No change after 1.5 s.

Counts the wrong way: what should I check?

Swap the D2 and D3 wires.

Counts twice: what should I check?

Sensors too far apart or set too far. Move them closer and shorten their range.

Misses people: what should I check?

IR modules struggle with black clothes and sunlight. Use them indoors, or use two VL53L0X sensors for longer range.