People Counter with Infrared Sensor: Build Your Own System in a Few Hours | Comuniq
This site requires Javascript to work properly. Please enable Javascript in your browser.
-->
/homemade_items
Post here everything you can do with homemade items that are truly useful. Who's going to go first?
Members: 4 Join
Moderated by: x1012
People Counter with Infrared Sensor: Build Your Own System in a Few Hours
PaulG<br>1784316190<br>[homemade_items]<br>1 comments
Ever noticed how stores, libraries, and even event venues seem to know exactly how many people walked in and out throughout the day? Behind that simple number, there's almost always an infrared sensor quietly doing the work. And here's the good news: this is one of the most rewarding projects for anyone getting started in electronics — it uses just a handful of components, the code is short, and the end result is something you can actually show off working in real time.
In this tutorial we're going to build a people counter using an infrared beam-break sensor module connected to an Arduino. Every time someone interrupts the infrared beam, the system logs a pass and updates the count. Simple in theory, but with practical details that make all the difference between a project that works well and one that gives you headaches in the first week.
## What you'll need
This project doesn't require expensive parts or specialized tools. An Arduino Uno (or any compatible clone) serves as the brain of the system. Then you'll need an infrared beam-break sensor module — usually sold with the emitter and receiver as two separate pieces, ready to place on either side of a doorway or corridor. Add a 16x2 LCD screen with an I2C module, which will make wiring things up much easier, and an optional small buzzer, which is genuinely helpful during testing since it gives you an audible cue every time the beam is broken. Jumper wires, a breadboard, and a USB power source round out the list.
## How the sensor actually works
Before wiring anything up, it's worth understanding the logic behind the system, since it'll help you enormously if something doesn't work as expected. The infrared emitter continuously sends a beam of light invisible to the human eye toward the receiver. As long as that beam arrives uninterrupted, the receiver keeps its output in one state (typically high). The moment a person walks through the corridor and blocks the beam, even for a fraction of a second, the receiver detects the absence of light and switches its output to low. That transition, from beam present to beam broken, is what the Arduino will interpret as "someone just passed."
## Building the circuit
Start by positioning the emitter and receiver facing each other, aligned at roughly waist height for an adult, with a gap between them that can be up to two or three meters depending on the module you bought. Connect the receiver's output pin to a digital input on the Arduino, pin 2 for example, and connect the power and ground of both modules to the Arduino's 5V and GND pins. If you're using the LCD screen with an I2C module, wiring gets even simpler, since you only need four wires: power, ground, SDA, and SCL, typically connected to pins A4 and A5 on an Arduino Uno.
One detail that makes a real difference in practice is making sure the emitter and receiver stay properly aligned with each other. A misalignment of just a few millimeters can already weaken the signal and cause false readings, so it's worth testing the alignment before mounting everything permanently in place.
## The code that brings it to life
The code for this project is simpler than it looks at first glance. The core idea is to continuously monitor the state of the pin connected to the receiver and detect the exact moment it switches from high to low, which is the instant the beam gets broken. Every time that transition happens, we increment a counter variable and update the LCD screen with the new value.
```cpp<br>#include<br>#include
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int sensorPin = 2;<br>int counter = 0;<br>int previousState = HIGH;
void setup() {<br>pinMode(sensorPin, INPUT);<br>lcd.init();<br>lcd.backlight();<br>lcd.setCursor(0, 0);<br>lcd.print("People: 0");
void loop() {<br>int currentState = digitalRead(sensorPin);
if (currentState == LOW && previousState == HIGH) {<br>counter++;<br>lcd.clear();<br>lcd.setCursor(0, 0);<br>lcd.print("People: ");<br>lcd.print(counter);<br>delay(500); // prevents duplicate counts from the same pass
previousState = currentState;<br>```
Notice the short half-second delay after each count. That detail exists because a single person walking through can trigger more than one beam-interruption reading, especially if they walk slowly or hesitate midway. Without that delay, your counter will start "seeing" extra people, which is frustrating to debug if you don't know where the error is coming from.
## Testing and fine-tuning the system
With everything assembled, the next step is testing under real conditions. Walk through the...