Projects / Smart locker with PIN and log

Smart locker with PIN and log

Intermediate4 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANO

    Test the keypad

    Print each key.

  2. NANOIRLZ44

    Drive the latch

    MOSFET, diode and 5 second pulse.

  3. NANO

    Write the PIN check

    Open on the right code.

  4. NANO

    Add the log

    A line per attempt.

  5. NANO

    Add the lockout

    Three wrong tries.

  6. NANO

    Fit the latch to a box

    Bolt on the lid edge.

  7. NANO

    Add a key override

    A mechanical release.

In short

How do you build a smart locker with PIN and log?

A small safe with keypad, solenoid latch and a log of every opening. You need Arduino Nano, 4x4 matrix keypad (membrane), Solenoid lock 12 V, MOSFET IRLZ44N, Rectifier Diode 1N4007, 16x2 LCD with I2C backpack (PCF8574), MicroSD Card Module, SanDisk microSD card 32 GB (A1, Class 10), DS3231 RTC module and 12 V 2 A DC adapter. It takes 7 steps and about 240 minutes.

Level
Intermediate
Time
About 240 min
Parts
10

Parts list

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

Wiring

FromToWhy
Keypad rows and columnsNano D2 to D9Keypad
LCD and RTC SDA, SCLNano A4 and A5Shared I2C bus
SD module CSNano D10Log file
SD module MOSI, MISO, SCKNano D11, D12, D13SPI
Nano A0IRLZ44N gate, with 10 kΩ to groundLatch driver
12 V adapter plusFuse (1 A), then solenoid plusLatch supply
Solenoid minusIRLZ44N drainSwitched ground
1N4007Across the solenoid, stripe to plusFlyback protection
IRLZ44N source and adapter minusNano GNDCommon ground

Code

A solenoid bolt that is pulled back for five seconds is the whole mechanism: the MOSFET switches the 12 V coil, the diode absorbs its spike when it turns off, and the Nano never touches the coil current. Every attempt is written to the SD card with the time, so you can see who tried when, and three wrong tries lock the keypad for a minute, which slows anyone guessing. Change the PIN in the sketch before use; a PIN stored in the code is not secret from someone who can plug in a cable, so this is a cabinet or drawer lock, not a real safe. A 12 V solenoid can get warm if left on, so the sketch pulses it for only five seconds. Add a mechanical key override, because a flat supply or a failed part must never lock something important in, and do not use it for medicines, important documents or anything where access must be guaranteed. Keep the log private since it records behaviour. Arduino IDE, board "Arduino Nano" (try the Old Bootloader option if the upload fails).

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).

// Smart locker: PIN opens the solenoid latch for 5 s, each attempt is logged, three wrong tries lock the keypad for a minute.
// Libraries: Keypad, LiquidCrystal_I2C, RTClib, SD. Board: Arduino Nano.
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
#include <SD.h>
const byte ROWS = 4, COLS = 4;
char keys[ROWS][COLS] = {{'1','2','3','A'},{'4','5','6','B'},{'7','8','9','C'},{'*','0','#','D'}};
byte rowPins[ROWS] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8, 9};
Keypad pad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27, 16, 2);
RTC_DS3231 rtc;
const int LATCH = A0, SD_CS = 10;
const char PIN_CODE[] = "4829";                     // change this before use
String entry = "";
int wrong = 0;
unsigned long lockedUntil = 0;
void logEvent(const char* what) {
  DateTime t = rtc.now();
  File f = SD.open("locker.csv", FILE_WRITE);
  if (f) {
    f.print(t.year()); f.print("-"); f.print(t.month()); f.print("-"); f.print(t.day()); f.print(" ");
    f.print(t.hour()); f.print(":"); f.print(t.minute()); f.print(","); f.println(what);
    f.close();
  }
}
void show(const char* a, const char* b = "") { lcd.clear(); lcd.setCursor(0, 0); lcd.print(a); lcd.setCursor(0, 1); lcd.print(b); }
void setup() {
  pinMode(LATCH, OUTPUT); digitalWrite(LATCH, LOW);
  lcd.init(); lcd.backlight(); rtc.begin(); SD.begin(SD_CS);
  show("Enter PIN");
}
void loop() {
  if (millis() < lockedUntil) return;
  char k = pad.getKey();
  if (!k) return;
  if (k == '#') { entry = ""; show("Enter PIN"); return; }
  entry += k;
  show("PIN:", String("****").substring(0, min((int)entry.length(), 4)).c_str());
  if (entry.length() == 4) {
    if (entry == PIN_CODE) {
      wrong = 0; logEvent("OPEN");
      show("Open"); digitalWrite(LATCH, HIGH); delay(5000); digitalWrite(LATCH, LOW);
    } else {
      wrong++; logEvent("WRONG PIN");
      show("Wrong PIN");
      if (wrong >= 3) { lockedUntil = millis() + 60000UL; wrong = 0; logEvent("LOCKOUT"); show("Locked 1 minute"); }
      else delay(1500);
    }
    entry = ""; if (millis() >= lockedUntil) show("Enter PIN");
  }
}

Test checklist

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

CheckExpected
Right PINLatch opens for 5 s
Wrong PINLogged, message shown
Three wrongLocked for a minute
LogEach event has a time stamp

If it does not work

Latch does not move

Check MOSFET pins and the 12 V supply.

Keys misread

Check the row and column order.

No log lines

Format the card as FAT32 and check CS.

Wrong time

Reset the RTC.

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

Ask Lark AI

Common questions

What parts do I need for the smart locker with PIN and log?

Arduino Nano, 4x4 matrix keypad (membrane), Solenoid lock 12 V, MOSFET IRLZ44N, Rectifier Diode 1N4007, 16x2 LCD with I2C backpack (PCF8574), MicroSD Card Module, SanDisk microSD card 32 GB (A1, Class 10), DS3231 RTC module and 12 V 2 A DC adapter.

How long does the smart locker with PIN and log take to build?

About 240 minutes for a intermediate maker, in 7 steps.

How do I know the smart locker with PIN and log works?

Right PIN: Latch opens for 5 s. Wrong PIN: Logged, message shown. Three wrong: Locked for a minute.

Latch does not move: what should I check?

Check MOSFET pins and the 12 V supply.

Keys misread: what should I check?

Check the row and column order.

No log lines: what should I check?

Format the card as FAT32 and check CS.

Wrong time: what should I check?

Reset the RTC.