Projects / Hall sensor door position logger

Hall sensor door position logger

Beginner1.5 hGuided buildReviewed

Build it step by step

0 of 6 done
  1. NANO

    Wire the board

    Connect everything as in the table.

  2. NANODS323119:33

    Set the clock

    Run the RTClib "ds3231" example once to set the time.

  3. NANOA1302

    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.

  4. NANO

    Power up open

    Reset with the door wide open so the resting level is stored.

  5. NANO

    Tune the thresholds

    Close the door, then open it by 2 cm, and adjust CLOSED_DELTA and AJAR_DELTA to match.

  6. NANOMICROSSANDIS

    Read the log

    Remove the card and open DOOR.CSV on a computer.

In short

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

Parts list

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

Wiring

FromToWhy
A1302 pin 1 (VCC)Nano 5VPower
A1302 pin 2 (GND)Nano GNDGround
A1302 pin 3 (OUT)Nano A0Analog reading
DS3231 SDA, SCLNano A4, A5I2C
OLED SDA, SCLNano A4, A5I2C, same bus
DS3231 and OLED VCC, GNDNano 5V, GNDPower
SD module CSNano D10Chip select
SD module MOSI, MISO, SCKNano D11, D12, D13SPI
SD module VCC, GNDNano 5V, GNDPower (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.

CheckExpected
Door shutScreen shows CLOSED
Door open by 2 cmAJAR
Door wide openOPEN
Open for 3 minutes, then shutOpen 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 →

Ask Lark AI

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.