Smart locker with PIN and log
Build it step by step
0 of 7 doneTest the keypad
Print each key.
Drive the latch
MOSFET, diode and 5 second pulse.
Write the PIN check
Open on the right code.
Add the log
A line per attempt.
Add the lockout
Three wrong tries.
Fit the latch to a box
Bolt on the lid edge.
Add a key override
A mechanical release.
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
Wiring
| From | To | Why |
|---|---|---|
| Keypad rows and columns | Nano D2 to D9 | Keypad |
| LCD and RTC SDA, SCL | Nano A4 and A5 | Shared I2C bus |
| SD module CS | Nano D10 | Log file |
| SD module MOSI, MISO, SCK | Nano D11, D12, D13 | SPI |
| Nano A0 | IRLZ44N gate, with 10 kΩ to ground | Latch driver |
| 12 V adapter plus | Fuse (1 A), then solenoid plus | Latch supply |
| Solenoid minus | IRLZ44N drain | Switched ground |
| 1N4007 | Across the solenoid, stripe to plus | Flyback protection |
| IRLZ44N source and adapter minus | Nano GND | Common 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.
| Check | Expected |
|---|---|
| Right PIN | Latch opens for 5 s |
| Wrong PIN | Logged, message shown |
| Three wrong | Locked for a minute |
| Log | Each 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 →
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.
