Build it step by step
0 of 7 doneWire the sensors and display
Check each alone first.
Give them separate addresses
XSHUT pins as in the code.
Print the distances
Wave a hand in front of each.
Mount them on the frame
Head height, side by side.
Set the trigger distance
Past the far wall.
Walk in and out
Check the count.
Try slow and fast walks
Tune the 4 second limit.
How do you build a toF people flow counter?
Two ToF sensors on a door frame tell who walks in and out. You need ESP32 DevKit V1, VL53L1X long-range ToF sensor × 2 and SSD1306 OLED Display. It takes 7 steps and about 180 minutes.
- Level
- Intermediate
- Time
- About 180 min
- Parts
- 3
Parts list
- Development BoardsBuyESP32 DevKit V1 × 1
- SensorsBuy
- DisplaysBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Both VL53L1X SDA and SCL | ESP32 GPIO21 and GPIO22 | Shared I2C bus |
| Both VL53L1X VIN and GND | ESP32 3V3 and GND | Power |
| Sensor A (outside) XSHUT | ESP32 GPIO25 | Lets the code give it its own address |
| Sensor B (inside) XSHUT | ESP32 GPIO26 | Lets the code give it its own address |
| OLED SDA and SCL | ESP32 GPIO21 and GPIO22 | Same I2C bus (address 0x3C) |
| OLED VCC and GND | ESP32 3V3 and GND | Power |
Code
Two sensors a few centimetres apart along the direction of travel tell which way someone went: if A, on the outside, sees them first and then B sees them, they are going in, and the other order means out. Both VL53L1X boards have the same I2C address at power-up, so the code holds both in reset with their XSHUT pins, releases A and gives it a new address, then does the same for B. Mount them at head height on the same side of the door frame, pointing across the doorway, with a few centimetres between them, and tune TRIG_MM so they see someone passing but not the far wall. Groups walking side by side or very close together will be counted as one, and two people passing in opposite directions at the same moment will confuse it, so treat the number as an estimate and reset it at the start of each day. This counts people without cameras or personal data, but tell people if it is in a place where they would expect to be told. Arduino IDE with the ESP32 boards package 2.0.x, board "ESP32 Dev Module".
Runs on a virtual ESP32 Dev Module 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).
// ToF people counter: two VL53L1X sensors on the door frame, A on the outside and B on the inside.
// Libraries: VL53L1X (Pololu), Adafruit SSD1306, Adafruit GFX. Board: ESP32.
#include <Wire.h>
#include <VL53L1X.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
VL53L1X a, b;
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const uint8_t XA = 25, XB = 26;
const int TRIG_MM = 1500; // something closer than 1.5 m counts as a person
int inside = 0, state = 0; // 0 idle, 1 A first, 2 B first
unsigned long stateSince = 0;
void show() {
oled.clearDisplay(); oled.setTextColor(SSD1306_WHITE); oled.setTextSize(1);
oled.setCursor(0, 0); oled.print("People inside");
oled.setTextSize(4); oled.setCursor(0, 24); oled.print(inside);
oled.display();
}
void setup() {
Wire.begin(21, 22);
pinMode(XA, OUTPUT); pinMode(XB, OUTPUT);
digitalWrite(XA, LOW); digitalWrite(XB, LOW); // both off
delay(10);
pinMode(XA, INPUT); delay(10); // release A and give it a new address
a.init(); a.setAddress(0x30);
pinMode(XB, INPUT); delay(10);
b.init(); b.setAddress(0x31);
a.setDistanceMode(VL53L1X::Short); b.setDistanceMode(VL53L1X::Short);
a.setMeasurementTimingBudget(20000); b.setMeasurementTimingBudget(20000);
a.startContinuous(20); b.startContinuous(20);
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
show();
}
void loop() {
int da = a.read(), db = b.read();
bool ta = !a.timeoutOccurred() && da > 0 && da < TRIG_MM;
bool tb = !b.timeoutOccurred() && db > 0 && db < TRIG_MM;
if (state == 0) {
if (ta && !tb) { state = 1; stateSince = millis(); }
else if (tb && !ta) { state = 2; stateSince = millis(); }
} else if (state == 1 && tb) { // A then B: walking in, count when both clear
while (true) { da = a.read(); db = b.read(); if (!(da < TRIG_MM && da > 0) && !(db < TRIG_MM && db > 0)) break; if (millis() - stateSince > 4000) break; }
inside++; state = 0; show();
} else if (state == 2 && ta) { // B then A: walking out
while (true) { da = a.read(); db = b.read(); if (!(da < TRIG_MM && da > 0) && !(db < TRIG_MM && db > 0)) break; if (millis() - stateSince > 4000) break; }
if (inside > 0) inside--; state = 0; show();
}
if (state != 0 && millis() - stateSince > 4000) state = 0; // gave up: someone peeked and turned back
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Walk in | Count rises by one |
| Walk out | Count falls by one |
| Peek and turn back | Count unchanged |
| Wave a hand in front of A only | Count unchanged |
If it does not work
Sensor not found
Check XSHUT wiring and that only A is released first.
Counts the wrong way
Swap which sensor is A and B.
Counts twice
Lower the trigger distance or add a longer clear time.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the toF people flow counter?
ESP32 DevKit V1, VL53L1X long-range ToF sensor × 2 and SSD1306 OLED Display.
How long does the toF people flow counter take to build?
About 180 minutes for a intermediate maker, in 7 steps.
How do I know the toF people flow counter works?
Walk in: Count rises by one. Walk out: Count falls by one. Peek and turn back: Count unchanged.
Sensor not found: what should I check?
Check XSHUT wiring and that only A is released first.
Counts the wrong way: what should I check?
Swap which sensor is A and B.
Counts twice: what should I check?
Lower the trigger distance or add a longer clear time.
