Door opening counter
A reed switch counts how many times a fridge or wardrobe door is opened and shows the total on a small display.
Build it step by step
0 of 8 doneInstall the libraries
Adafruit SSD1306 and Adafruit GFX Library.
Wire the reed switch
One side to D2, the other to GND.
Wire the button and OLED
Button D3 to GND, OLED on A4 and A5.
Upload the sketch
Display shows 0 openings.
Hold the magnet next to the switch
Count stays at 0, door shut.
Move the magnet away and back
Count rises to 1.
Open and close five times
Count shows 5.
Hold the button two seconds
Count returns to 0.
How do you build a door opening counter?
A reed switch counts how many times a fridge or wardrobe door is opened and shows the total on a small display. You need Arduino Nano, Door reed switch MC-38, SSD1306 OLED Display, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 90 minutes.
- Level
- Beginner
- Time
- About 90 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 |
|---|---|---|
| Reed switch one terminal | Nano D2 | Internal pull-up |
| Reed switch other terminal | Nano GND | Closed when the magnet is near |
| Magnet | Fixed on the door edge | Next to the reed switch when the door is shut |
| Reset button | Nano D3 to GND | Hold two seconds to reset the count |
| OLED SDA and SCL | Nano A4 and A5 | I2C, address 0x3C |
| OLED VCC and GND | Nano 5V and GND | Check your module voltage |
Code
The reed switch closes when the magnet is next to it, so a shut door reads LOW and an open door reads HIGH. The sketch counts one opening each time the state changes from closed to open, after a 150 millisecond debounce to ignore a door that wobbles. The count is held in RAM and written to EEPROM at most once per hour when it has changed, because EEPROM cells wear out after roughly 100,000 writes: once an hour is about 24 writes a day, which would take over ten years to wear out a cell. A power cut can lose up to an hour of counts, which is a good trade for a household counter. A blank EEPROM reads as all ones, so the sketch treats that value as zero on the first run. Hold the button for two seconds to reset the total. To count per day, add a real-time clock and reset at midnight. Install Adafruit SSD1306 and Adafruit GFX from the Library Manager.
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 <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <EEPROM.h>
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const uint8_t REED = 2, BTN = 3;
const unsigned long SAVE_EVERY_MS = 3600000UL; // save at most once an hour to protect the EEPROM
uint32_t count = 0;
uint32_t savedCount = 0;
unsigned long lastSave = 0;
unsigned long lastOpenAt = 0;
bool doorOpen = false;
unsigned long changedAt = 0;
unsigned long btnDownAt = 0;
void saveCount() {
EEPROM.put(0, count);
savedCount = count;
lastSave = millis();
}
void draw() {
oled.clearDisplay();
oled.setTextSize(1);
oled.setCursor(0, 0); oled.print("Door openings");
oled.setTextSize(3);
oled.setCursor(0, 16); oled.print(count);
oled.setTextSize(1);
oled.setCursor(0, 50);
if (lastOpenAt == 0) oled.print("None since power on");
else { oled.print("Last: "); oled.print((millis() - lastOpenAt) / 60000UL); oled.print(" min ago"); }
if (doorOpen) { oled.setCursor(100, 0); oled.print("OPEN"); }
oled.display();
}
void setup() {
pinMode(REED, INPUT_PULLUP);
pinMode(BTN, INPUT_PULLUP);
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
oled.setTextColor(SSD1306_WHITE);
EEPROM.get(0, count);
if (count == 0xFFFFFFFF) count = 0; // blank EEPROM reads as all ones
savedCount = count;
draw();
}
void loop() {
bool open = (digitalRead(REED) == HIGH); // magnet away from the switch means the door is open
if (open != doorOpen && millis() - changedAt > 150) { // 150 ms debounce
changedAt = millis();
doorOpen = open;
if (open) { count++; lastOpenAt = millis(); }
draw();
}
if (digitalRead(BTN) == LOW) {
if (btnDownAt == 0) btnDownAt = millis();
if (millis() - btnDownAt > 2000) { count = 0; saveCount(); btnDownAt = 0; draw(); delay(500); }
} else {
btnDownAt = 0;
}
if (count != savedCount && millis() - lastSave > SAVE_EVERY_MS) saveCount();
static unsigned long lastDraw = 0;
if (millis() - lastDraw > 30000) { lastDraw = millis(); draw(); } // refresh the minutes
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Magnet next to the switch | Door shut, count unchanged |
| Magnet away | Count rises by one and OPEN shows |
| Wobble the door | Only one count |
| Hold the button for two seconds | Count resets |
| Power cycle after an hour | The saved count returns |
If it does not work
Counts twice per opening
Increase the 150 ms debounce or fix the magnet more firmly.
Never counts
Check the reed wiring and the gap, reed switches work within about 1 cm.
Count lost after power cut
It saves hourly, lower SAVE_EVERY_MS for testing only.
Display blank
Try address 0x3D, and check the display voltage.
Reed switch glass broken
Mount it with tape and do not bend its leads near the glass.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the door opening counter?
Arduino Nano, Door reed switch MC-38, SSD1306 OLED Display, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the door opening counter take to build?
About 90 minutes for a beginner maker, in 8 steps.
How do I know the door opening counter works?
Magnet next to the switch: Door shut, count unchanged. Magnet away: Count rises by one and OPEN shows. Wobble the door: Only one count.
Counts twice per opening: what should I check?
Increase the 150 ms debounce or fix the magnet more firmly.
Never counts: what should I check?
Check the reed wiring and the gap, reed switches work within about 1 cm.
Count lost after power cut: what should I check?
It saves hourly, lower SAVE_EVERY_MS for testing only.
Display blank: what should I check?
Try address 0x3D, and check the display voltage.
