Projects / Drawer opening logger

Drawer opening logger

Beginner1.5 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANODS323112:50

    Set the RTC

    Run the RTClib ds3231 example once.

  2. NANO145 lux

    Wire the LDR divider

    LDR to 5V, 10 k to GND.

  3. NANO

    Find your two light levels

    With the Serial line on.

  4. NANO

    Set OPEN_LEVEL and CLOSE_LEVEL

    In the sketch.

  5. NANO147 lux

    Upload and test on the bench

    Cover and uncover the LDR.

  6. NANO148 lux

    Fit it in the drawer

    LDR facing up.

  7. NANOMICROS

    Read the card after a week

    Open drawer.csv in a spreadsheet.

In short

How do you build a drawer opening logger?

A light sensor inside a drawer records the date and time of every opening, to find out who keeps taking your charger. You need Arduino Nano, LDR Photoresistor, Resistor 10 kΩ, DS3231 RTC module, MicroSD Card Module, Battery holder 4 × AA with switch, AA alkaline battery × 4 and Jumper wires male to male (40 pcs, 20 cm). It takes 7 steps and about 90 minutes.

Level
Beginner
Time
About 90 min
Parts
8

Parts list

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

Wiring

FromToWhy
LDRNano 5V on one leg and A0 on the otherLight sensor
10 k resistorNano A0 to GNDDivider, brighter drawer gives higher reading
DS3231 SDA and SCLNano A4 and A5Real-time clock
DS3231 VCC and GNDNano 5V and GNDPower
SD module CS, MOSI, MISO, SCKNano D10, D11, D12, D13Storage
SD module VCC and GNDNano 5V and GNDPower
4 AA holder plusNano VINAbout 6 V, the Nano regulator makes 5 V
4 AA holder minusNano GNDCommon ground

Code

The drawer is dark when shut, so any jump in light from the LDR means it was opened. The sketch checks once a second, then sleeps in power-down mode using the Nano's watchdog timer as an alarm clock, and writes a line with the real time from the DS3231 each time the state changes. Two thresholds with a gap between them (a hysteresis) stop it from logging twice when light flickers at the edge. Mount the LDR facing up where it sees the drawer lid or the room when opened, and keep the electronics in a small box at the back. Before you trust it, uncomment the Serial line and note the reading when the drawer is closed and open, then set the two levels about a third and two-thirds of the way between. Expect the batteries to last days to a couple of weeks, not months: the Nano's power LED, USB chip and regulator, and the SD module all keep drawing a few milliamps even while the processor sleeps. Cutting the Nano's power LED helps. Set the clock once with the RTClib example that sets time from your computer, and format the card as FAT32. Only use it on your own belongings; recording other people without their knowledge is not acceptable. Setup: Arduino IDE, board "Arduino Nano" (try the Old Bootloader option if the upload fails). Install RTClib.

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 <avr/sleep.h>
#include <avr/wdt.h>
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <RTClib.h>

const uint8_t SD_CS = 10, LDR_PIN = A0;
const int OPEN_LEVEL = 300;        // above this the drawer is open, tune with Serial
const int CLOSE_LEVEL = 200;       // below this it is closed (hysteresis stops chatter)
RTC_DS3231 rtc;
bool isOpen = false;

ISR(WDT_vect) {}                   // the watchdog only wakes us up

void sleepOneSecond() {
  MCUSR &= ~(1 << WDRF);
  WDTCSR |= (1 << WDCE) | (1 << WDE);
  WDTCSR = (1 << WDIE) | (1 << WDP2) | (1 << WDP1);   // 1 second timeout
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  ADCSRA &= ~(1 << ADEN);
  sleep_enable();
  sleep_cpu();
  sleep_disable();
  wdt_disable();
  ADCSRA |= (1 << ADEN);
}

void logEvent(const char *what) {
  File f = SD.open("drawer.csv", FILE_WRITE);
  if (!f) return;
  f.print(rtc.now().timestamp());
  f.print(',');
  f.println(what);
  f.close();
}

void setup() {
  Serial.begin(9600);
  rtc.begin();
  SD.begin(SD_CS);
  logEvent("START");
}

void loop() {
  int v = analogRead(LDR_PIN);
  if (!isOpen && v > OPEN_LEVEL) { isOpen = true; logEvent("OPEN"); }
  else if (isOpen && v < CLOSE_LEVEL) { isOpen = false; logEvent("CLOSED"); }
  // Serial.println(v);            // uncomment to find your two levels, then remove it
  sleepOneSecond();
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Cover the LDRNothing logged
Uncover itOPEN row with the time
Cover it againCLOSED row
Wiggle the light level around the thresholdOnly one OPEN, no chatter
Check the clockTimes match your phone

If it does not work

No lines written

Check SD format and CS pin D10.

OPEN logged all the time

Drawer not dark enough; lower the levels or shade the sensor.

Wrong times

RTC not set, or the coin cell is flat.

Batteries drain in days

Expected; cut the Nano power LED and use a bigger battery pack.

Two OPEN rows in a row

Levels too close; widen the gap.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the drawer opening logger?

Arduino Nano, LDR Photoresistor, Resistor 10 kΩ, DS3231 RTC module, MicroSD Card Module, Battery holder 4 × AA with switch, AA alkaline battery × 4 and Jumper wires male to male (40 pcs, 20 cm).

How long does the drawer opening logger take to build?

About 90 minutes for a beginner maker, in 7 steps.

How do I know the drawer opening logger works?

Cover the LDR: Nothing logged. Uncover it: OPEN row with the time. Cover it again: CLOSED row.

No lines written: what should I check?

Check SD format and CS pin D10.

OPEN logged all the time: what should I check?

Drawer not dark enough; lower the levels or shade the sensor.

Wrong times: what should I check?

RTC not set, or the coin cell is flat.

Batteries drain in days: what should I check?

Expected; cut the Nano power LED and use a bigger battery pack.