Population Math: Bidirectional Visitor Counter
A simple motion sensor (PIR) turns off the lights if you sit too still on the couch. The Bidirectional Visitor Counter solves this by maintaining a strict mathematical tally. The lights only turn off when the Arduino confirms the room population relies at exact zero.

The Dual-Sensor Interlock
The system mounts two IR Obstacle Sensors in the door frame, placed 5cm apart (Sensor A on the outside, Sensor B on the inside).
- Entering the Room (A -> B):
- A person breaks Sensor A first.
if (A == blocked) { state = Entering; }. - Half a second later, they break Sensor B. The Arduino registers a successful sequence and tallies:
PeopleCount++.
- A person breaks Sensor A first.
- Exiting the Room (B -> A):
- A person breaks Sensor B first.
if (B == blocked) { state = Exiting; }. - Then they break Sensor A. The logic tallies:
PeopleCount--.
- A person breaks Sensor B first.
Controlling Mains Power
- The Threshold:
if (PeopleCount > 0) { digitalWrite(relayPin, HIGH); // Lights ON } else if (PeopleCount == 0) { digitalWrite(relayPin, LOW); // Lights OFF to save power! } - The Physical Relay: The Arduino commands a 5V Songle Relay. Warning: The other side of the relay switches 110V/220V mains electricity to the ceiling lamp. This requires extreme safe wiring practices or the use of an enclosed IoT Power Strip.
Assembly Necessities
- Arduino Uno/Nano: The logic gate.
- IR Obstacle Avoidance Sensors (x2) or Laser Diodes + LDRs.
- 5V Relay Module.
- A 16x2 LCD (To display the current tally: "People in Room: 3")