Build it step by step
0 of 8 doneWire the sensor
VCC, GND and OUT to D2.
Wire the clock and display
Both on A4 and A5 with 5V and GND.
Install the libraries
RTClib, Adafruit SSD1306 and Adafruit GFX.
Upload the sketch
The clock sets from the computer on first run.
Wait 30 seconds
The sensor settles.
Walk past
Today increases by one.
Stay in view
No extra counts for ten seconds.
Check the day change
Set the clock to just before midnight to test.
How do you build a night visitor counter?
A small PIR motion sensor counts visits near a bird feeder or a plant, and an OLED shows today's and yesterday's totals, kept across power cuts. You need Arduino Nano, AM312 mini PIR motion sensor, SSD1306 OLED Display, DS3231 RTC module, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 830 Tie Points. It takes 8 steps and about 180 minutes.
- Level
- Beginner
- Time
- About 180 min
- Parts
- 6
Parts list
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| AM312 VCC | Nano 5V | Sensor power, it accepts 2.7 to 12 V |
| AM312 GND | Nano GND | Common ground |
| AM312 OUT | Nano D2 | Digital motion output, about 3.3 V when high |
| DS3231 SDA and OLED SDA | Nano A4 | Shared I2C data |
| DS3231 SCL and OLED SCL | Nano A5 | Shared I2C clock |
| DS3231 VCC and OLED VCC | Nano 5V | Power (check your OLED accepts 5 V) |
| DS3231 GND and OLED GND | Nano GND | Common ground |
Code
A PIR sensor detects the infrared change caused by a warm body moving across its field of view. The AM312 is a very small and low power version with a digital output that goes high for about two seconds on each detection, so counting every pulse would double-count one visitor who lingers. The sketch counts the rising edge of the output and then ignores new edges for ten seconds, which is a simple form of debouncing in time. A DS3231 clock keeps the date accurate, and when the day changes the total moves to the yesterday line and today starts again from zero. The totals are saved in the Nano's EEPROM so they survive a power cut. The counts are written only when a visit is counted or the day changes, which is rare enough to be safe for the EEPROM. The sensor needs about thirty seconds after power-up to settle, and the sketch ignores it during that time. It senses movement only and records no pictures or sound. If it faces a path or a boundary, point it away from neighbours and people who have not agreed to be counted. A plastic box with a hole for the sensor keeps rain and dew off the electronics.
Runs on a virtual Arduino Nano 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 <RTClib.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <EEPROM.h>
RTC_DS3231 rtc;
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const int PIR = 2;
const unsigned long WARMUP_MS = 30000UL; // the PIR needs about 30 s to settle
const unsigned long IGNORE_MS = 10000UL; // ignore repeats within ten seconds
const byte MAGIC = 0xA5;
unsigned int today = 0, yesterday = 0;
byte lastDay = 0;
bool pirWas = false, haveCounted = false;
unsigned long lastCountMs = 0, lastDraw = 0;
void save() {
EEPROM.update(0, MAGIC);
EEPROM.update(1, lastDay);
EEPROM.put(2, today);
EEPROM.put(4, yesterday);
}
void draw(const DateTime &now) {
char t[8];
snprintf(t, sizeof(t), "%02d:%02d", now.hour(), now.minute());
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
oled.setTextSize(1);
oled.setCursor(0, 0); oled.print("Visitor counter "); oled.print(t);
oled.setTextSize(2);
oled.setCursor(0, 18); oled.print("Today "); oled.print(today);
oled.setCursor(0, 40); oled.print("Prev "); oled.print(yesterday);
oled.display();
}
void setup() {
pinMode(PIR, INPUT);
Serial.begin(9600);
if (!rtc.begin()) { Serial.println("RTC not found"); while (true) {} }
if (rtc.lostPower()) rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // first run: set from the computer clock
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println("OLED not found"); while (true) {} }
DateTime now = rtc.now();
lastDay = now.day();
if (EEPROM.read(0) == MAGIC) {
byte savedDay = EEPROM.read(1);
EEPROM.get(2, today);
EEPROM.get(4, yesterday);
if (savedDay != now.day()) { yesterday = today; today = 0; }
}
save();
draw(now);
}
void loop() {
unsigned long ms = millis();
bool pir = (digitalRead(PIR) == HIGH);
if (ms > WARMUP_MS && pir && !pirWas) { // rising edge
if (!haveCounted || ms - lastCountMs >= IGNORE_MS) {
today++;
lastCountMs = ms;
haveCounted = true;
save();
Serial.print("Visit "); Serial.println(today);
}
}
pirWas = pir;
if (ms - lastDraw >= 1000) {
lastDraw = ms;
DateTime now = rtc.now();
if (now.day() != lastDay) { // new day
yesterday = today; today = 0; lastDay = now.day(); save();
}
draw(now);
}
}What the serial monitor should show
Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.
RTC not found
Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Power on | Display shows the time, Today 0 or the saved total |
| Walk past after 30 seconds | Today goes up by one |
| Wave for 5 seconds | Only one count |
| Wait 10 seconds and move again | A second count |
| Power off and on | The totals are still there |
If it does not work
No counts
Wait 30 seconds after power-up and check OUT is on D2.
Too many counts
Raise IGNORE_MS, and keep the sensor away from heat sources and moving plants in wind.
Blank screen
Check A4 and A5 and the OLED address.
Wrong time
Reflash once with the computer clock correct.
Counts reset at power-up
The EEPROM magic byte was overwritten, run once more and leave it powered.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the night visitor counter?
Arduino Nano, AM312 mini PIR motion sensor, SSD1306 OLED Display, DS3231 RTC module, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 830 Tie Points.
How long does the night visitor counter take to build?
About 180 minutes for a beginner maker, in 8 steps.
How do I know the night visitor counter works?
Power on: Display shows the time, Today 0 or the saved total. Walk past after 30 seconds: Today goes up by one. Wave for 5 seconds: Only one count.
No counts: what should I check?
Wait 30 seconds after power-up and check OUT is on D2.
Too many counts: what should I check?
Raise IGNORE_MS, and keep the sensor away from heat sources and moving plants in wind.
Blank screen: what should I check?
Check A4 and A5 and the OLED address.
Wrong time: what should I check?
Reflash once with the computer clock correct.
