Projects / ESP32 DevKit V1 visitor counter

ESP32 DevKit V1 visitor counter

Beginner1.8 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. ESP32motion

    Wire the PIR

    VCC to VIN, GND to GND, OUT to GPIO27.

  2. ESP3214.8 mASSD1306

    Wire the OLED

    3V3, GND, SDA to GPIO21, SCL to GPIO22.

  3. ESP32

    Wire the button

    GPIO26 to GND.

  4. ESP3214.8 mASSD1306

    Install the libraries

    Adafruit SSD1306 and Adafruit GFX.

  5. ESP32

    Upload the sketch

    The display shows Warming up for 30 seconds.

  6. ESP32

    Wave a hand

    The count goes up by one.

  7. ESP32

    Wave again within ten seconds

    No new count.

In short

How do you build an ESP32 DevKit V1 visitor counter?

An ESP32 DevKit V1 counts how many times a PIR sensor sees movement at a doorway and shows the count on an OLED. You need ESP32 DevKit V1 30-pin (CH340), PIR Motion Sensor HC-SR501, SSD1306 OLED Display, Tactile push button 6 mm and Breadboard 830 Tie Points. It takes 7 steps and about 110 minutes.

Level
Beginner
Time
About 110 min
Parts
5

Parts list

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

Wiring

FromToWhy
PIR VCCESP32 VIN pin5 V from the USB supply
PIR GNDESP32 GNDCommon ground
PIR OUTESP32 GPIO27High when it sees movement (3.3 V signal)
OLED VCCESP32 3V3Power at 3.3 V
OLED GNDESP32 GNDCommon ground
OLED SDA and SCLESP32 GPIO21 and GPIO22I2C data and clock
Reset buttonESP32 GPIO26 and GNDClears the count

Code

A PIR sensor notices the change in heat radiation when a warm body moves across its view, and sets its output high. The program counts each time the output goes from low to high, which is a new movement, and ignores anything within ten seconds of the last count, because one person walking past can set off the sensor more than once. The count is saved in the board's flash memory with the Preferences library, so it survives a power cut, and the button clears it. The module needs about thirty seconds to settle after power-up, so the program shows a warming-up message first. This is a counter of movements and not of people: two people walking through together count as one, and a pet counts as well. It uses no camera and records nothing about anyone. If you use it somewhere that people go, tell them what it does. Keep the display and board away from water.

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).

#include <Wire.h>
#include <Preferences.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_SSD1306 oled(128, 64, &Wire, -1);
Preferences prefs;

const int PIR = 27, RESET_BTN = 26;
const unsigned long WARM_UP = 30000;               // the PIR needs about 30 seconds to settle
const unsigned long QUIET = 10000;                 // ignore repeat movement within ten seconds

unsigned long visits = 0;
unsigned long lastCount = 0;
bool lastPir = false;

void draw(const char* note) {
  oled.clearDisplay();
  oled.setTextColor(SSD1306_WHITE);
  oled.setTextSize(1);
  oled.setCursor(0, 0);
  oled.print("Visitor counter");
  oled.setTextSize(4);
  oled.setCursor(0, 18);
  oled.print(visits);
  oled.setTextSize(1);
  oled.setCursor(0, 56);
  oled.print(note);
  oled.display();
}

void setup() {
  pinMode(PIR, INPUT);
  pinMode(RESET_BTN, INPUT_PULLUP);
  Wire.begin(21, 22);
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { while (true) {} }
  prefs.begin("visitors", false);
  visits = prefs.getULong("count", 0);             // the count survives a power cut
  draw("Warming up...");
}

void loop() {
  unsigned long now = millis();
  if (now < WARM_UP) { delay(200); return; }

  if (digitalRead(RESET_BTN) == LOW) {
    visits = 0;
    prefs.putULong("count", 0);
    draw("Count cleared");
    delay(500);
  }

  bool pir = digitalRead(PIR) == HIGH;
  if (pir && !lastPir && now - lastCount > QUIET) {
    lastCount = now;
    visits++;
    prefs.putULong("count", visits);
    draw("Movement seen");
  } else if (now - lastCount > 2000) {
    draw("Counting");
  }
  lastPir = pir;
  delay(50);
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
During warm-upWarming up message
Wave a handCount rises by one
Wave again at onceNo change
Wave again after ten secondsRises again
Unplug and plug inThe count is kept
Press the buttonBack to zero

If it does not work

Counts by itself

Wait the full 30 seconds, and keep the PIR away from heaters and sunlight.

Never counts

Check PIR OUT is on GPIO27 and the PIR has 5 V on VIN.

Count not saved

Check the sketch has the Preferences lines.

Display blank

Check the I2C wires and the address 0x3C.

Board will not upload

Install the CH340 driver and hold BOOT while uploading.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the ESP32 DevKit V1 visitor counter?

ESP32 DevKit V1 30-pin (CH340), PIR Motion Sensor HC-SR501, SSD1306 OLED Display, Tactile push button 6 mm and Breadboard 830 Tie Points.

How long does the ESP32 DevKit V1 visitor counter take to build?

About 110 minutes for a beginner maker, in 7 steps.

How do I know the ESP32 DevKit V1 visitor counter works?

During warm-up: Warming up message. Wave a hand: Count rises by one. Wave again at once: No change.

Counts by itself: what should I check?

Wait the full 30 seconds, and keep the PIR away from heaters and sunlight.

Never counts: what should I check?

Check PIR OUT is on GPIO27 and the PIR has 5 V on VIN.

Count not saved: what should I check?

Check the sketch has the Preferences lines.

Display blank: what should I check?

Check the I2C wires and the address 0x3C.