Projects / ToF people flow counter

ToF people flow counter

Intermediate3 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. ESP32

    Wire the sensors and display

    Check each alone first.

  2. ESP32

    Give them separate addresses

    XSHUT pins as in the code.

  3. ESP32

    Print the distances

    Wave a hand in front of each.

  4. ESP32

    Mount them on the frame

    Head height, side by side.

  5. ESP32

    Set the trigger distance

    Past the far wall.

  6. ESP32

    Walk in and out

    Check the count.

  7. ESP32

    Try slow and fast walks

    Tune the 4 second limit.

In short

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

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Both VL53L1X SDA and SCLESP32 GPIO21 and GPIO22Shared I2C bus
Both VL53L1X VIN and GNDESP32 3V3 and GNDPower
Sensor A (outside) XSHUTESP32 GPIO25Lets the code give it its own address
Sensor B (inside) XSHUTESP32 GPIO26Lets the code give it its own address
OLED SDA and SCLESP32 GPIO21 and GPIO22Same I2C bus (address 0x3C)
OLED VCC and GNDESP32 3V3 and GNDPower

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.

CheckExpected
Walk inCount rises by one
Walk outCount falls by one
Peek and turn backCount unchanged
Wave a hand in front of A onlyCount 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 →

Ask Lark AI

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.