Build an ATmega1284P fingerprint door lock that opens with a stored fingerprint or a 6-digit keypad PIN, shows status on an OLED and drives a 12V solenoid lock through a relay.
Need it ready-made? Buy Ready Project
This ATmega1284P fingerprint door lock is a complete access-control system for a home, lab or office door. It opens with any enrolled fingerprint or with a 6-digit PIN typed on a 4×4 keypad, then drives a 12V solenoid lock through a relay for five seconds and locks again by itself.
It combines several modules in one build: an R307 optical fingerprint sensor, a keypad, an OLED status screen, a relay, a reed sensor that warns when the door is left open, an inside exit button, a buzzer and two status LEDs. The ATmega1284P is a good fit because it has two hardware UARTs (one for the fingerprint sensor and one free for debugging), 128 KB of flash and 16 KB of RAM, all in a breadboard-friendly 40-pin DIP package.

How It Works
- Fingerprint check: the loop keeps asking the R307 for an image. When a finger is placed, the sensor turns it into a template and searches its own on-board library (up to 127 prints in this code). A match with a confidence score of 50 or more unlocks the door and the OLED shows which ID opened it.
- PIN entry: digits typed on the keypad show as stars.
#confirms and*clears. The PIN is kept in the chip’s EEPROM, so it survives power cuts. The first-boot PIN is123456, so change it straight away. - Admin menu:
Aenrolls a new finger (two scans of the same finger),Bdeletes a stored ID,Cchanges the PIN andDshows how many prints are stored. A, B and C all ask for the current PIN first. - Lockout: three wrong fingers or PINs in a row lock the keypad and sensor for 30 seconds with a long beep, which slows down guessing.
- Relay and auto re-lock: PB0 switches the relay, which feeds 12V to the solenoid for 5 seconds. A 1N4007 diode across the coil absorbs the voltage spike when it switches off.
- Exit button and door sensor: the inside exit button always opens the door, even during lockout. The reed switch reads HIGH when the door is open, and if it stays open for more than 30 seconds the buzzer beeps and the OLED asks you to close it.
Components Required
- ATmega1284P (40-pin DIP) microcontroller – 1
- 16 MHz crystal with two 22 pF capacitors – 1 set
- R307 or AS608 optical fingerprint sensor module – 1
- 4×4 membrane keypad – 1
- 0.96 inch SSD1306 I2C OLED display (128×64) – 1
- 5V single-channel relay module (with transistor driver and diode) – 1
- 12V solenoid door lock (or electric strike) with 1N4007 flyback diode – 1
- Magnetic reed door sensor – 1
- Exit push button (inside of the door) – 1
- 5V active buzzer – 1
- Green and red LEDs with 220 Ω resistors – 1 each
- 10 kΩ resistor (RESET pull-up) and 100 nF decoupling capacitors – 1 + 3
- 12V 2A adapter and LM2596 buck module (or 7805) set to 5V – 1 each
- USBasp programmer and USB-to-TTL serial adapter (for debug) – 1 each
- Breadboard or perfboard, jumper wires and screw terminals – 1 set
Wiring Table
| Part / pin | ATmega1284P pin | Notes |
|---|---|---|
| Fingerprint TX (green wire on most R307) | RXD1 / PD2 (pin 16) | Arduino pin 10 in MightyCore standard pinout |
| Fingerprint RX (white wire on most R307) | TXD1 / PD3 (pin 17) | Arduino pin 11 |
| Fingerprint VCC / GND | 5V / GND | AS608 modules need 3.3V, check your module |
| Keypad rows R1, R2, R3, R4 | PA0, PA1, PA2, PA3 (pins 40 to 37) | Arduino pins 24 to 27 |
| Keypad columns C1, C2, C3, C4 | PA4, PA5, PA6, PA7 (pins 36 to 33) | Arduino pins 28 to 31 |
| OLED SCL / SDA | PC0 (pin 22) / PC1 (pin 23) | I2C address 0x3C |
| Relay IN | PB0 (pin 1) | Arduino pin 0, active HIGH relay module |
| Relay COM / NO | +12V / solenoid + | Solenoid – to 12V supply GND, 1N4007 across the coil |
| Active buzzer + | PB1 (pin 2) | Buzzer – to GND |
| Green LED (unlocked) | PB2 (pin 3) through 220 Ω | Cathode to GND |
| Red LED (locked) | PB3 (pin 4) through 220 Ω | Cathode to GND |
| Reed door sensor | PB4 (pin 5) | Other wire to GND, internal pull-up, LOW = door closed |
| Exit button | PD4 (pin 18) | Other leg to GND, internal pull-up |
| USB-TTL adapter (debug) | RXD0 PD0 (pin 14) / TXD0 PD1 (pin 15) | 115200 baud, optional |
| 16 MHz crystal | XTAL2 (pin 12) / XTAL1 (pin 13) | 22 pF from each pin to GND |
| VCC / AVCC / GND | Pins 10, 30 / pins 11, 31 | 5V from the buck module, 100 nF at each VCC pin |
| RESET | Pin 9 | 10 kΩ to 5V, also to the USBasp RST line |
Keep the 12V lock wiring away from the logic side and join only the grounds of the 12V supply and the 5V buck output. Never power the solenoid from the 5V rail.
Connection Diagram
The illustration above shows the parts laid out on a bench. It is a layout guide only. Follow the pin-accurate drawing below, which matches the wiring table exactly.
Arduino Code
Install MightyCore in the Arduino IDE (Boards Manager URL https://mcudude.github.io/MightyCore/package_MCUdude_MightyCore_index.json), then pick ATmega1284, variant 1284P, pinout Standard and clock 16 MHz external. Install the Adafruit Fingerprint Sensor Library, Keypad (Mark Stanley, Alexander Brevig), Adafruit SSD1306 and Adafruit GFX libraries. Click Burn Bootloader once with the USBasp to set the fuses, then use Upload Using Programmer. The sketch below was compiled for this board and uses about 22 KB of flash.
/*
ATmega1284P Fingerprint + Keypad Door Lock
R307 / AS608 fingerprint sensor, 4x4 keypad, 0.96" SSD1306 OLED,
5V relay (or MOSFET) for a 12V solenoid lock, reed door sensor,
exit button, buzzer and status LEDs.
Board: MightyCore "ATmega1284", standard pinout, 16 MHz external crystal.
Arduino Project Hub - original example code.
*/
#include <Adafruit_Fingerprint.h>
#include <Keypad.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <EEPROM.h>
// ---------- Pins (MightyCore standard pinout) ----------
const uint8_t PIN_RELAY = 0; // PB0 -> relay IN (lock coil)
const uint8_t PIN_BUZZER = 1; // PB1 -> active buzzer +
const uint8_t PIN_LED_G = 2; // PB2 -> green LED (unlocked)
const uint8_t PIN_LED_R = 3; // PB3 -> red LED (locked / error)
const uint8_t PIN_REED = 4; // PB4 -> reed switch to GND (LOW = door closed)
const uint8_t PIN_EXIT = 12; // PD4 -> inside exit button to GND
// Fingerprint sensor on Serial1: RX1 = PD2 (D10), TX1 = PD3 (D11)
// OLED on I2C: SCL = PC0 (D16), SDA = PC1 (D17)
// ---------- Keypad on PORTA ----------
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] = {24, 25, 26, 27}; // PA0..PA3
byte colPins[COLS] = {28, 29, 30, 31}; // PA4..PA7
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
Adafruit_Fingerprint finger(&Serial1);
// ---------- Settings ----------
const unsigned long UNLOCK_MS = 5000; // relay on time
const unsigned long DOOR_ALARM_MS = 30000; // door left open warning
const unsigned long LOCKOUT_MS = 30000; // after too many wrong tries
const uint8_t MAX_WRONG = 3;
const int EE_MAGIC_ADDR = 0;
const int EE_PIN_ADDR = 1; // 6 bytes + terminator
const uint8_t EE_MAGIC = 0x5A;
const char DEFAULT_PIN[] = "123456"; // change it after first boot!
char pinCode[7];
char entry[7];
uint8_t entryLen = 0;
uint8_t wrongCount = 0;
unsigned long lockoutUntil = 0;
unsigned long unlockUntil = 0;
unsigned long doorOpenSince = 0;
bool unlocked = false;
// ---------- Helpers ----------
void beep(uint16_t ms) { digitalWrite(PIN_BUZZER, HIGH); delay(ms); digitalWrite(PIN_BUZZER, LOW); }
void show(const char *l1, const char *l2 = "", const char *l3 = "") {
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
oled.setTextSize(2); oled.setCursor(0, 0); oled.println(l1);
oled.setTextSize(1); oled.setCursor(0, 28); oled.println(l2);
oled.setCursor(0, 44); oled.println(l3);
oled.display();
}
void showIdle() {
char stars[12] = "PIN: ";
for (uint8_t i = 0; i < entryLen; i++) strcat(stars, "*");
show("LOCKED", "Place finger or", stars);
}
void loadPin() {
if (EEPROM.read(EE_MAGIC_ADDR) != EE_MAGIC) {
for (uint8_t i = 0; i < 7; i++) EEPROM.update(EE_PIN_ADDR + i, DEFAULT_PIN[i]);
EEPROM.update(EE_MAGIC_ADDR, EE_MAGIC);
}
for (uint8_t i = 0; i < 7; i++) pinCode[i] = EEPROM.read(EE_PIN_ADDR + i);
pinCode[6] = '\0';
}
void savePin(const char *p) {
for (uint8_t i = 0; i < 7; i++) EEPROM.update(EE_PIN_ADDR + i, p[i]);
strcpy(pinCode, p);
}
void lockDoor() {
unlocked = false;
digitalWrite(PIN_RELAY, LOW);
digitalWrite(PIN_LED_G, LOW);
digitalWrite(PIN_LED_R, HIGH);
showIdle();
}
void unlockDoor(const char *who) {
unlocked = true;
wrongCount = 0;
unlockUntil = millis() + UNLOCK_MS;
digitalWrite(PIN_RELAY, HIGH);
digitalWrite(PIN_LED_G, HIGH);
digitalWrite(PIN_LED_R, LOW);
show("OPEN", who, "Door unlocked 5 s");
beep(120);
Serial.print(F("Unlocked by ")); Serial.println(who);
}
void wrongTry(const char *why) {
wrongCount++;
show("DENIED", why);
for (uint8_t i = 0; i < 3; i++) { beep(80); delay(80); }
if (wrongCount >= MAX_WRONG) {
lockoutUntil = millis() + LOCKOUT_MS;
wrongCount = 0;
show("WAIT", "Too many wrong tries", "Locked for 30 s");
beep(1000);
} else {
delay(1000);
}
showIdle();
}
// Read up to 'maxLen' digits from the keypad, '#' confirms, '*' cancels.
// Returns number of digits, or -1 on cancel / 15 s timeout.
int readDigits(char *buf, uint8_t maxLen, const char *title, const char *hint) {
uint8_t n = 0; buf[0] = '\0';
unsigned long start = millis();
show(title, hint, "");
while (millis() - start < 15000) {
char k = keypad.getKey();
if (!k) continue;
start = millis();
if (k == '*') return -1;
if (k == '#') return n;
if (k >= '0' && k <= '9' && n < maxLen) {
buf[n++] = k; buf[n] = '\0';
show(title, hint, buf);
beep(20);
}
}
return -1;
}
bool askAdminPin() {
char p[7];
int n = readDigits(p, 6, "ADMIN", "Enter PIN then #");
if (n == 6 && strcmp(p, pinCode) == 0) return true;
show("DENIED", "Wrong admin PIN"); beep(400); delay(1000);
return false;
}
// Wait for a finger image and convert it into the given char buffer slot.
bool captureTo(uint8_t slot, const char *prompt) {
show("ENROLL", prompt);
unsigned long start = millis();
while (finger.getImage() != FINGERPRINT_OK) {
if (millis() - start > 10000) return false;
delay(50);
}
return finger.image2Tz(slot) == FINGERPRINT_OK;
}
void enrollFinger() {
if (!askAdminPin()) return;
char idBuf[4];
int n = readDigits(idBuf, 3, "ENROLL", "ID 1-127 then #");
int id = (n > 0) ? atoi(idBuf) : 0;
if (id < 1 || id > 127) { show("ERROR", "Invalid ID"); beep(400); delay(1000); return; }
if (!captureTo(1, "Place finger")) { show("ERROR", "No finger / bad image"); beep(400); delay(1500); return; }
show("ENROLL", "Remove finger"); beep(60);
delay(1500);
while (finger.getImage() != FINGERPRINT_NOFINGER) delay(50);
if (!captureTo(2, "Same finger again")) { show("ERROR", "Second scan failed"); beep(400); delay(1500); return; }
if (finger.createModel() == FINGERPRINT_OK && finger.storeModel(id) == FINGERPRINT_OK) {
char msg[22]; snprintf(msg, sizeof(msg), "Saved as ID %d", id);
show("DONE", msg); beep(200);
} else {
show("ERROR", "Prints did not match"); beep(400);
}
delay(1500);
}
void deleteFinger() {
if (!askAdminPin()) return;
char idBuf[4];
int n = readDigits(idBuf, 3, "DELETE", "ID 1-127 then #");
int id = (n > 0) ? atoi(idBuf) : 0;
if (id >= 1 && id <= 127 && finger.deleteModel(id) == FINGERPRINT_OK) {
show("DONE", "Fingerprint deleted"); beep(200);
} else {
show("ERROR", "Delete failed"); beep(400);
}
delay(1500);
}
void changePin() {
if (!askAdminPin()) return;
char a[7], b[7];
if (readDigits(a, 6, "NEW PIN", "6 digits then #") != 6) { show("ERROR", "PIN must be 6 digits"); delay(1500); return; }
if (readDigits(b, 6, "REPEAT", "Same PIN then #") != 6 || strcmp(a, b) != 0) { show("ERROR", "PINs differ"); beep(400); delay(1500); return; }
savePin(a);
show("DONE", "PIN changed"); beep(200); delay(1500);
}
void checkFinger() {
if (finger.getImage() != FINGERPRINT_OK) return; // no finger
if (finger.image2Tz() != FINGERPRINT_OK) { wrongTry("Bad image, retry"); return; }
if (finger.fingerSearch() == FINGERPRINT_OK && finger.confidence >= 50) {
char who[22]; snprintf(who, sizeof(who), "Finger ID %d", finger.fingerID);
unlockDoor(who);
} else {
wrongTry("Unknown finger");
}
}
void handleKey(char k) {
if (k >= '0' && k <= '9') {
if (entryLen < 6) { entry[entryLen++] = k; entry[entryLen] = '\0'; beep(20); showIdle(); }
} else if (k == '*') {
entryLen = 0; entry[0] = '\0'; showIdle();
} else if (k == '#') {
if (entryLen == 6 && strcmp(entry, pinCode) == 0) unlockDoor("PIN code");
else wrongTry("Wrong PIN");
entryLen = 0; entry[0] = '\0';
} else if (k == 'A') { enrollFinger(); showIdle(); }
else if (k == 'B') { deleteFinger(); showIdle(); }
else if (k == 'C') { changePin(); showIdle(); }
else if (k == 'D') {
char msg[22]; finger.getTemplateCount();
snprintf(msg, sizeof(msg), "Stored prints: %d", finger.templateCount);
show("INFO", msg); delay(2000); showIdle();
}
}
void setup() {
pinMode(PIN_RELAY, OUTPUT); digitalWrite(PIN_RELAY, LOW);
pinMode(PIN_BUZZER, OUTPUT); digitalWrite(PIN_BUZZER, LOW);
pinMode(PIN_LED_G, OUTPUT);
pinMode(PIN_LED_R, OUTPUT);
pinMode(PIN_REED, INPUT_PULLUP);
pinMode(PIN_EXIT, INPUT_PULLUP);
Serial.begin(115200); // debug on USB-serial (PD0/PD1)
Wire.begin();
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) Serial.println(F("OLED not found"));
loadPin();
finger.begin(57600);
if (finger.verifyPassword()) {
show("READY", "Fingerprint OK", "A=add B=del C=PIN");
} else {
show("ERROR", "Sensor not found", "Check TX/RX wires");
Serial.println(F("Fingerprint sensor not found"));
}
beep(60);
delay(1500);
lockDoor();
}
void loop() {
unsigned long now = millis();
// Inside exit button always works, even during lockout.
if (digitalRead(PIN_EXIT) == LOW && !unlocked) unlockDoor("Exit button");
// Auto re-lock after UNLOCK_MS.
if (unlocked && (long)(now - unlockUntil) >= 0) lockDoor();
// Door left open warning (reed switch HIGH = door open).
if (digitalRead(PIN_REED) == HIGH) {
if (doorOpenSince == 0) doorOpenSince = now;
if (now - doorOpenSince > DOOR_ALARM_MS && !unlocked) {
show("CLOSE", "Door left open!");
beep(200); delay(300);
}
} else if (doorOpenSince != 0) {
doorOpenSince = 0;
if (!unlocked) showIdle();
}
// Lockout after too many wrong tries.
if (lockoutUntil && (long)(now - lockoutUntil) < 0) return;
if (lockoutUntil) { lockoutUntil = 0; showIdle(); }
char k = keypad.getKey();
if (k) handleKey(k);
if (!unlocked) checkFinger();
}
Setting Up and Testing
- Power the board from USB first, without the 12V lock. The OLED should show READY and Fingerprint OK. If it says Sensor not found, swap the sensor TX and RX wires.
- Press
C, enter123456and#, then set your own 6-digit PIN twice. - Press
A, enter your PIN, type an ID such as1and#, then scan the same finger twice when asked. Repeat for each family member with a new ID. - Place an enrolled finger. The green LED and relay should switch on for 5 seconds. Try a finger that is not enrolled and check that the red LED stays on and the buzzer beeps three times.
- Type three wrong PINs and confirm the 30-second lockout, then press the exit button and confirm it still opens the lock.
- Finally connect the 12V supply and solenoid, and mount the reed switch on the door frame with its magnet on the door.
Safety tip: choose a fail-secure or fail-safe lock to suit the door, and always keep a mechanical key or another way to open the door in case of a power cut.
Troubleshooting
| Problem | Fix |
|---|---|
| OLED shows Sensor not found | Swap sensor TX and RX, check it is on PD2/PD3 (Serial1), and confirm the module voltage. Some sensors use 9600 or 115200 baud, change finger.begin() to match. |
| Board resets when the lock fires | The solenoid is pulling down the supply. Power it from 12V only, add the 1N4007 across the coil and a 470 µF capacitor on the 5V rail. |
| Keypad keys are mixed up | The row and column wires are in the wrong order. Swap the order in rowPins or colPins to match your keypad. |
| Upload works but timing is wrong | Clock fuses are not set. Select 16 MHz external in the board menu and run Burn Bootloader once. |
| Enrolled finger is often rejected | Enroll again with the finger pressed flat, clean the sensor glass, or lower the confidence limit from 50 to 40. |
| Door-open warning keeps beeping | The reed switch is not closing. Bring the magnet within 10 mm of the switch or check its wire to PB4 and GND. |
Ideas to Extend
- Log each unlock with the time from a DS3231 RTC to an SD card.
- Add an HC-05 Bluetooth module on Serial0 to open the door from a phone.
- Use a second relay to switch on a porch light for 30 seconds after each unlock.