Dual-Sensor People Counter: ESP32 & Firebase Web Interface
by ahmedele5 in Circuits > Arduino
10 Views, 0 Favorites, 0 Comments
Dual-Sensor People Counter: ESP32 & Firebase Web Interface

This project was developed at Orange Digital Center Morocco, a hub dedicated to innovation, creativity, and rapid prototyping. At the FabLab, individuals and teams have access to cutting-edge tools, including 3D printers, laser cutters, and a wide range of electronic and mechanical resources. The center provides a collaborative environment where innovators, entrepreneurs, and students can turn their ideas into tangible solutions, with a strong focus on sustainable and impactful projects.
Imagine a system that counts the number of people in a room in real time and displays the data on a website via Firebase. More than just a sensor, it's a connected solution for efficient space management—whether for security, energy optimization, or occupancy analytics. With an intuitive web interface, you can monitor room occupancy remotely, from anywhere.
Whether you're a professional looking to optimize workspace usage, an event organizer tracking attendance, or simply a tech enthusiast, this project opens the door to smart, automated management. Because every presence counts, and every data point makes a difference!
Supplies


--> ESP32 WROOM
Acts as the central controller of the project. It continuously reads distance data from the two ultrasonic sensors, determines the direction of movement (entry or exit) based on the triggering sequence, and updates the internal people count. It can also be programmed to send the data to a server or display it on a screen.
--> 2x HC-SR04 Ultrasonic Sensors
Mounted side by side at the entrance.
- Sensor A is placed first in the path, followed closely by Sensor B.
- When someone walks in, Sensor A triggers before Sensor B → entry detected.
- When someone walks out, Sensor B triggers before Sensor A → exit detected.
- Their job is to detect the direction of movement and trigger the counting logic in the ESP32.
--> KCD1-101 Toggle Switch
Connected between the power source and the ESP32 circuit. It allows you to manually turn the system ON or OFF, useful for saving battery or resetting the system when needed.
--> Jumper Wires
Used to connect the sensors, ESP32, switch, and power module together. These ensure proper communication between components and power distribution in the circuit.
--> 2x 3.7V 2600mAh Lithium Batteries
Serve as the main power supply for the project. They provide enough voltage and current to run the ESP32 and the sensors for extended periods, making the system portable and wireless.
--> LM2596 DC-DC Buck Converter
Takes the ~7.4V from the two lithium batteries and steps it down to 5V, which is safe and stable for powering the ESP32 and sensors. The digital display helps you adjust and monitor the output voltage easily to avoid damaging components.
Building the Circuit

Hardware Assembly Instructions:
Let's start by assembling our hardware components! We'll connect two HC-SR04 ultrasonic sensors to our ESP32:
- Sensor 1:
- TRIG → GPIO 04
- ECHO → GPIO 02
- Sensor 2:
- TRIG → GPIO 05
- ECHO → GPIO 34
The LM2596 buck converter will regulate power from our 3.7V lithium battery to a stable 5V/3.3V for the ESP32.
Pro Tip:
- Use a breadboard for prototyping before making permanent connections.
Setting Up Firebase


Firebase will be our cloud database. We need to:
- Create a new project in Firebase Console
- Set up Realtime Database with a simple structure{"NbrPersonne": 0}
- Configure security rules to allow read/write access (temporarily for testing, then secure it later)
- Enable Firebase anonymous auth to obtain a temporary API key.
- Note down your Firebase credentials (API key, project ID) for the ESP32 code
Programming the ESP32
Now for the brains of our project! We'll program the ESP32 in Arduino IDE to:
- Measure distances with the ultrasonic sensor
- Implement people counting logic (detecting entry/exit when distance changes)
- Connect to WiFi and push data to Firebase
- Don't forget to install the Firebase ESP Client library! The code will run a continuous loop, updating counts every second.
Downloads
Creating the Web Interface


Let's build a simple but effective web dashboard:
- Basic HTML/CSS page with a large counter display
- JavaScript to listen for Firebase database changes
- Visual indicators when counts increase/decrease
- The page will automatically update whenever someone enters/leaves the room - no refresh needed!
Replace these values with your actual Firebase project details
3D Design




- Main Box
- Holds ESP32 and sensors
- Has special holes for the pivot stick
- Designed for easy wire management
- Cover Plate
- Simple flat piece
- Has matching holes for the pivot stick
- Clicks or screws in place
- Pivot Stick (Long Rod)
- Long rod shape (like a chopstick)
- Fits through holes in both other pieces
- Lets the main box rotate while cover stays fixed