ESP32 DevKit V1 visitor counter
Build it step by step
0 of 7 doneWire the PIR
VCC to VIN, GND to GND, OUT to GPIO27.
Wire the OLED
3V3, GND, SDA to GPIO21, SCL to GPIO22.
Wire the button
GPIO26 to GND.
Install the libraries
Adafruit SSD1306 and Adafruit GFX.
Upload the sketch
The display shows Warming up for 30 seconds.
Wave a hand
The count goes up by one.
Wave again within ten seconds
No new count.
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
| From | To | Why |
|---|---|---|
| PIR VCC | ESP32 VIN pin | 5 V from the USB supply |
| PIR GND | ESP32 GND | Common ground |
| PIR OUT | ESP32 GPIO27 | High when it sees movement (3.3 V signal) |
| OLED VCC | ESP32 3V3 | Power at 3.3 V |
| OLED GND | ESP32 GND | Common ground |
| OLED SDA and SCL | ESP32 GPIO21 and GPIO22 | I2C data and clock |
| Reset button | ESP32 GPIO26 and GND | Clears 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.
| Check | Expected |
|---|---|
| During warm-up | Warming up message |
| Wave a hand | Count rises by one |
| Wave again at once | No change |
| Wave again after ten seconds | Rises again |
| Unplug and plug in | The count is kept |
| Press the button | Back 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 →
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.
