Community project

Smart Occupancy Lighting Counter

Photo of Smart Occupancy Lighting Counter
Generated with AI

Mohamed El-Ekhtiar

Published November 28, 2026

This project builds a people counter that tracks room occupancy by detecting when individuals pass through a doorway. Two infrared sensors positioned at the entrance determine direction of travel, incrementing or decrementing a counter displayed on an LCD screen. The counter can trigger a relay to automatically control room lighting based on occupancy, making it useful for energy-efficient spaces like offices, classrooms, or storage areas.

The guide provides a complete wiring diagram, parts list, and step-by-step assembly instructions for mounting the sensors, connecting the display and relay module to an Arduino Uno, and uploading the firmware. Testing procedures ensure accurate detection before integrating with actual room lighting, and the design includes debouncing logic to prevent false counts from sensor noise.

Wiring diagram

Wiring diagram for Smart Occupancy Lighting Counter
Loading diagram…

Gather all the parts

QtyComponent
1

FC-51 IR Obstacle Avoidance Sensor

5V reflective infrared obstacle module; OUT is LOW on detection. Verify printed pin labels.

1

FC-51 IR Obstacle Avoidance Sensor

5V reflective infrared obstacle module; OUT is LOW on detection. Verify printed pin labels.

1

LCD 16x2 I2C

16x2 character LCD display with I2C backpack

1

Relay Module

Single channel relay module

1

LED

red

Standard 3mm/5mm through-hole LED. A current-limiting series resistor is added automatically.

1

Resistor

220 ohm, 1/4 W

Through-hole resistor (current-limiting in series with an LED)

Assemble it in 7 steps

1. Prepare the unpowered prototype

Unplug USB. Use an Uno, two FC-51 active-LOW 5V infrared obstacle sensors, a 5V 16x2 LCD with 0x27 I2C backpack, a DFR0017 active-HIGH 5V relay module, one red LED, and one 220-ohm resistor. Use a breadboard and jumper wires. Power later through the Uno USB socket using a regulated 5V USB supply rated at least 500mA (1A recommended) and USB A-to-B cable.

  • Do not connect household mains or any load to the relay contacts. Do not feed 5V into the barrel jack or VIN. Do not connect a second supply to the 5V pin while USB is connected.

2. Connect shared power

Connect Uno 5V to both sensor VCC pins, LCD VCC and relay VCC (+). Connect Uno GND to both sensor GND pins, LCD and relay GND (-). All modules use the same ground.

3. Connect and position the infrared sensors

Sensor A OUT goes to D2; sensor B OUT goes to D3. Each sensor VCC goes to 5V and GND to GND. Follow printed pin labels, not connector position. Mount A on the outside side of the passage and B on the inside side so a person triggers them in sequence. Adjust each sensitivity trimmer using its detection indicator.

  • Test with a hand first. These are short-range reflective sensors, not full doorway beams. Use a narrow single-file passage; sunlight, clothing and overlapping detection zones can cause missed or incorrect counts.

4. Connect the display

LCD SDA goes to A4 (data); SCL goes to A5 (clock). Adjust the backpack contrast trimmer if the backlight is on but letters are invisible. The firmware uses I2C address 0x27; a 0x3F backpack requires changing that constant.

5. Connect the relay and prototype light

Relay IN (D on DFR0017) goes to D8. Leave all relay screw contacts empty. Connect D9 to either end of the 220-ohm resistor, its other end to the LED long leg (anode), and LED short leg/flat-edge side to GND. The LED represents the light and follows the relay state; it is not powered through the relay contacts.

  • Use the selected active-HIGH DFR0017 relay, not an unverified active-LOW replacement. The resistor is mandatory to protect the LED and Uno output.

6. Test one person at a time

With firmware running, clear both sensors first. Trigger A then B within two seconds and clear both sensors: count becomes 1 and LED/relay turn on. Trigger B then A and clear both sensors: count becomes 0 and light turns off. Repeat an exit at zero: count stays zero. Trigger only one sensor and wait over two seconds: no count. Trigger both together: no count. Keep one sensor blocked: no repeated counting.

  • Debounce is 40ms; both sensors must be clear for 150ms before rearming. The count starts at zero after every reset, so initialize with an empty room. Tailgating, opposite-direction crossings and turnarounds can cause errors; this is a single-file prototype, not a safety-grade occupancy system.

7. Plan safe room-light integration

D8 already controls the relay from occupancyCount, independently of the LCD. The LED is only a test indicator. For a household room light, ask a qualified electrician to choose and install a properly enclosed, mains-rated switching interface suitable for the lamp load, inrush current and local regulations. This project only documents the low-voltage controller; it is not a mains wiring design.

  • Never connect household AC to the Uno, breadboard, jumper wires or exposed relay contacts. Leave this prototype relay contacts empty. A relay contact voltage marking alone does not make the exposed assembly safe for room-light installation.

Review all connections

1. Connections between "sensorA" and "Arduino"

FunctionsensorAArduino
powerVCC5V
groundGNDGND
digitalOUTGPIO 2

2. Connections between "sensorB" and "Arduino"

FunctionsensorBArduino
powerVCC5V
groundGNDGND
digitalOUTGPIO 3

3. Connections between "lcd" and "Arduino"

FunctionlcdArduino
powerVCC5V
groundGNDGND
i2cSDAGPIO 18
i2cSCLGPIO 19

4. Connections between "relay" and "Arduino"

FunctionrelayArduino
powerVCC5V
groundGNDGND
digitalINGPIO 8

5. Connections between "ledResistor" and "Arduino"

FunctionledResistorArduino
digitalP1GPIO 9
digitalP2 → LED ANODEEXT

6. Connections between "light" and "Arduino"

FunctionlightArduino
groundGNDGND

Deploy the firmware

#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

constexpr uint8_t SENSOR_A_PIN = 2, SENSOR_B_PIN = 3;
constexpr uint8_t RELAY_PIN = 8, LED_PIN = 9;
constexpr uint8_t RELAY_ON = HIGH, RELAY_OFF = LOW; // DFR0017 active HIGH
constexpr unsigned long DEBOUNCE_MS = 40;
constexpr unsigned long SEQUENCE_TIMEOUT_MS = 2000;
constexpr unsigned long CLEAR_MS = 150;
constexpr uint16_t MAX_OCCUPANCY = 9999;
uint16_t occupancyCount = 0; // Reset/power loss starts at zero; not persistent.
LiquidCrystal_I2C lcd(0x27, 16, 2);

struct DebouncedSensor {
  uint8_t pin;
  explicit DebouncedSensor(uint8_t p) : pin(p) {}
  bool raw = false, active = false, rose = false;
  unsigned long changedAt = 0;
  void update(unsigned long now) {
    rose = false;
    bool sample = digitalRead(pin) == LOW;
    if (sample != raw) { raw = sample; changedAt = now; }
    if (raw != active && now - changedAt >= DEBOUNCE_MS) {
      active = raw;
      rose = active;
    }
  }
};
DebouncedSensor sensorA{SENSOR_A_PIN}, sensorB{SENSOR_B_PIN};
enum class DetectionState { WAIT_CLEAR, IDLE, A_FIRST, B_FIRST };
DetectionState state = DetectionState::WAIT_CLEAR;
unsigned long sequenceStarted = 0, clearStarted = 0;
bool clearing = false;

void updateOccupancy(bool entering) {
  if (entering) {
    if (occupancyCount < MAX_OCCUPANCY) ++occupancyCount;
  } else if (occupancyCount > 0) --occupancyCount;
  Serial.print(F("People inside: ")); Serial.println(occupancyCount);
}

void waitForClear() {
  state = DetectionState::WAIT_CLEAR;
  clearing = false;
}

void detectPassage(unsigned long now) {
  sensorA.update(now); sensorB.update(now);
  // After startup, a count, ambiguity, or timeout, require both sensors
  // clear continuously before accepting a new person. No repeat counts
  // while someone remains in front of either sensor.
  if (state == DetectionState::WAIT_CLEAR) {
    if (!sensorA.active && !sensorB.active && !sensorA.raw && !sensorB.raw) {
      if (!clearing) { clearing = true; clearStarted = now; }
      if (now - clearStarted >= CLEAR_MS) state = DetectionState::IDLE;
    } else clearing = false;
    return;
  }
  if (state == DetectionState::IDLE) {
    if (sensorA.rose && sensorB.rose) { waitForClear(); return; }
    if (sensorA.rose) { state = DetectionState::A_FIRST; sequenceStarted = now; }
    else if (sensorB.rose) { state = DetectionState::B_FIRST; sequenceStarted = now; }
    return;
  }
  // A second activation must occur before the timeout. The first sensor
  // may have cleared already, allowing separated detection zones.
  if (now - sequenceStarted >= SEQUENCE_TIMEOUT_MS) { waitForClear(); return; }
  if (sensorA.rose && sensorB.rose) { waitForClear(); return; }
  if (state == DetectionState::A_FIRST) {
    if (sensorA.rose) { waitForClear(); return; } // first sensor retrigger: invalid
    if (sensorB.rose) { updateOccupancy(true); waitForClear(); }
  } else if (state == DetectionState::B_FIRST) {
    if (sensorB.rose) { waitForClear(); return; }
    if (sensorA.rose) { updateOccupancy(false); waitForClear(); }
  }
}

void controlLighting() {
  bool on = occupancyCount > 0;
  digitalWrite(RELAY_PIN, on ? RELAY_ON : RELAY_OFF);
  digitalWrite(LED_PIN, on ? HIGH : LOW);
}

void updateDisplay() {
  static uint16_t displayed = 65535;
  if (displayed == occupancyCount) return; // no constant redraw/flicker
  displayed = occupancyCount;
  char text[17];
  // Full label fits only a single digit on a 16-column screen.
  if (occupancyCount < 10) snprintf(text, sizeof(text), "People Inside: %u", occupancyCount);
  else snprintf(text, sizeof(text), "People In: %u", occupancyCount);
  lcd.setCursor(0, 0);
  lcd.print(F("                "));
  lcd.setCursor(0, 0); lcd.print(text);
  lcd.setCursor(0, 1);
  // No space after ':' so OFF fits in 16 columns.
  lcd.print(occupancyCount > 0 ? "Light Status:ON " : "Light Status:OFF");
}

void setup() {
  // Preload the OFF output level before enabling the relay pin.
  digitalWrite(RELAY_PIN, RELAY_OFF); pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW); pinMode(LED_PIN, OUTPUT);
  // FC-51 outputs are LOW when an obstacle is detected.
  // Internal pull-ups hold the input HIGH when the output is released.
  pinMode(SENSOR_A_PIN, INPUT_PULLUP); pinMode(SENSOR_B_PIN, INPUT_PULLUP);
  Serial.begin(9600);
  Wire.begin();
  lcd.init(); lcd.backlight();
  controlLighting(); updateDisplay();
}

void loop() {
  detectPassage(millis());
  controlLighting();
  updateDisplay();
}

Remix this project

Make it yours in one click

Open a full copy of this project in your own Schematik workspace, diagram, code, parts, and assembly steps included. Swap the sensor, add features, or redesign the whole thing with AI. The author's original stays untouched.

Open in Schematik