Hall sensor door position logger
Build it step by step
0 of 6 doneWire the board
Connect everything as in the table.
Set the clock
Run the RTClib "ds3231" example once to set the time.
Fix the magnet and sensor
Glue the magnet on the door and the A1302 on the frame so they touch when the door is shut.
Power up open
Reset with the door wide open so the resting level is stored.
Tune the thresholds
Close the door, then open it by 2 cm, and adjust CLOSED_DELTA and AJAR_DELTA to match.
Read the log
Remove the card and open DOOR.CSV on a computer.
How do you build a hall sensor door position logger?
A hall sensor and magnet tell if a door is open, ajar or closed, and log how long it stays open. You need Arduino Nano, A1302 linear hall sensor, Neodymium magnets (10 × 3 mm), DS3231 RTC module, MicroSD Card Module, SanDisk microSD card 32 GB (A1, Class 10) and SSD1306 OLED Display. It takes 6 steps and about 90 minutes.
- Level
- Beginner
- Time
- About 90 min
- Parts
- 7
Wiring
| From | To | Why |
|---|---|---|
| A1302 pin 1 (VCC) | Nano 5V | Power |
| A1302 pin 2 (GND) | Nano GND | Ground |
| A1302 pin 3 (OUT) | Nano A0 | Analog reading |
| DS3231 SDA, SCL | Nano A4, A5 | I2C |
| OLED SDA, SCL | Nano A4, A5 | I2C, same bus |
| DS3231 and OLED VCC, GND | Nano 5V, GND | Power |
| SD module CS | Nano D10 | Chip select |
| SD module MOSI, MISO, SCK | Nano D11, D12, D13 | SPI |
| SD module VCC, GND | Nano 5V, GND | Power (module has a regulator and level shifter) |
Code
Arduino IDE, board "Arduino Nano" (processor ATmega328P, or ATmega328P (Old Bootloader) for clones). Install "RTClib", "U8g2" (for the text-only U8x8 screen, which uses very little RAM) and use the built-in SD library. Power up with the magnet away from the sensor so the resting level is read correctly, then fix the magnet to the door. The thresholds suit a 10 × 3 mm magnet at 1 to 2 cm; adjust them while watching the Serial output of readHall.
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 <SD.h>
#include <U8x8lib.h>
U8X8_SSD1306_128X64_NONAME_HW_I2C oled(U8X8_PIN_NONE);
RTC_DS3231 rtc;
const byte HALL = A0, SD_CS = 10;
const int CLOSED_DELTA = 120; // reading change when the door is shut (magnet close)
const int AJAR_DELTA = 30; // reading change when it is slightly open
int rest = 512; // reading with no magnet nearby, set in setup
byte state = 0; // 0 closed, 1 ajar, 2 open
unsigned long openMs = 0, totalOpenSecs = 0;
byte today = 0;
int readHall() {
long s = 0;
for (byte i = 0; i < 16; i++) s += analogRead(HALL);
return s / 16;
}
void logLine(const char* what) {
File f = SD.open("DOOR.CSV", FILE_WRITE);
if (!f) return;
DateTime n = rtc.now();
f.print(n.timestamp()); f.print(','); f.println(what);
f.close();
}
void showState() {
oled.setFont(u8x8_font_chroma48medium8_r);
oled.clearLine(2); oled.clearLine(4);
const char* names[] = { "CLOSED", "AJAR ", "OPEN " };
oled.drawString(0, 2, names[state]);
char buf[17];
snprintf(buf, sizeof buf, "Open %lu min", totalOpenSecs / 60);
oled.drawString(0, 4, buf);
}
void setup() {
oled.begin();
rtc.begin();
SD.begin(SD_CS);
rest = readHall(); // start with the door OPEN and the magnet away to set this
today = rtc.now().day();
showState();
}
void loop() {
int d = abs(readHall() - rest);
byte now = d >= CLOSED_DELTA ? 0 : (d >= AJAR_DELTA ? 1 : 2);
if (now != state) {
if (state != 0 && now == 0) totalOpenSecs += (millis() - openMs) / 1000;
if (state == 0 && now != 0) openMs = millis();
state = now;
logLine(state == 0 ? "closed" : (state == 1 ? "ajar" : "open"));
showState();
}
if (rtc.now().day() != today) { today = rtc.now().day(); totalOpenSecs = 0; }
delay(200);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Door shut | Screen shows CLOSED |
| Door open by 2 cm | AJAR |
| Door wide open | OPEN |
| Open for 3 minutes, then shut | Open total goes up by 3 and a line is written to DOOR.CSV |
If it does not work
Always OPEN
Magnet too far or facing the wrong pole. Flip it over.
Flickers between states
Widen the gap between the thresholds.
No log file
Check the SD card is FAT32 and the wiring on D10 to D13.
Time is wrong
Set the DS3231 with the example sketch and check its battery.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the hall sensor door position logger?
Arduino Nano, A1302 linear hall sensor, Neodymium magnets (10 × 3 mm), DS3231 RTC module, MicroSD Card Module, SanDisk microSD card 32 GB (A1, Class 10) and SSD1306 OLED Display.
How long does the hall sensor door position logger take to build?
About 90 minutes for a beginner maker, in 6 steps.
How do I know the hall sensor door position logger works?
Door shut: Screen shows CLOSED. Door open by 2 cm: AJAR. Door wide open: OPEN.
Always OPEN: what should I check?
Magnet too far or facing the wrong pole. Flip it over.
Flickers between states: what should I check?
Widen the gap between the thresholds.
No log file: what should I check?
Check the SD card is FAT32 and the wiring on D10 to D13.
Time is wrong: what should I check?
Set the DS3231 with the example sketch and check its battery.
