The given task is to design a password-protected locker system using a microcontroller that secures access through two types of PINs — a Master PIN and a User PIN.
Requirements
Why Use EEPROM?
This task will be implemented with the following microcontrollers:
We are using the ESP32 DevKit v4 development board and programming it using the Arduino IDE.
ESP32 does not have a real EEPROM chip inside. Instead, part of its Flash memory is used to simulate EEPROM behaviour.
Connect the ESP32 development board to your computer using a USB cable.
#include <EEPROM.h>
#define EEPROM_USER_PIN_ADDR 0 // Starting address for user PIN in EEPROM
#define EEPROM_SIZE 32 // >= 4 bytes for the PIN; adjust as needed
#define MASTER_PIN "1234" // Master PIN for reset (string literal)
String pin12;
void setup() {
Serial.begin(115200);
Serial.println("Password-Protected Locker System Started!");
// REQUIRED on ESP32: start EEPROM emulation before any read/write
EEPROM.begin(EEPROM_SIZE);
// Check if a user PIN is already set (0xFF means erased/uninitialized)
if (EEPROM.read(EEPROM_USER_PIN_ADDR) == 0xFF) {
setInitialPIN();
} else {
Serial.println("User PIN is already set. Enter your PIN to access.");
}
}
void loop() {
Serial.println("\nMain Menu:");
Serial.println("1. Enter PIN to Unlock");
Serial.println("2. Update User PIN");
Serial.println("3. Reset PIN with Master PIN");
Serial.print("Choose an option (1-3): ");
while (Serial.available() == 0)
; // Wait for user input
int option = Serial.parseInt();
Serial.println(option);
switch (option) {
case 1: verifyPIN(); break;
case 2: updateUserPIN(); break;
case 3: resetPINWithMaster(); break;
default: Serial.println("Invalid option. Please try again."); break;
}
// flush any trailing characters for cleanliness
while (Serial.available()) Serial.read();
}
void setInitialPIN() {
Serial.println("No PIN found. Set a new 4-digit PIN:");
String newPIN = getValidatedPIN();
for (int i = 0; i < 4; i++) {
EEPROM.write(EEPROM_USER_PIN_ADDR + i, newPIN[i]);
}
// REQUIRED on ESP32: persist writes to Flash
EEPROM.commit();
Serial.println("User PIN set successfully!");
}
void verifyPIN() {
Serial.println("Enter your 4-digit PIN to unlock:");
String enteredPIN = getValidatedPIN();
String storedPIN = readStoredPIN();
if (enteredPIN == storedPIN) {
Serial.println("Access Granted!");
} else {
Serial.println("Incorrect PIN. Access Denied.");
}
}
void updateUserPIN() {
Serial.println("Enter your current 4-digit PIN:");
String currentPIN = getValidatedPIN();
String storedPIN = readStoredPIN();
if (currentPIN == storedPIN) {
Serial.println("Enter your new 4-digit PIN:");
String newPIN = getValidatedPIN();
for (int i = 0; i < 4; i++) {
EEPROM.write(EEPROM_USER_PIN_ADDR + i, newPIN[i]);
}
// Commit ONCE after all bytes
EEPROM.commit();
Serial.println("User PIN updated successfully!");
} else {
Serial.println("Incorrect current PIN. Update failed.");
}
}
void resetPINWithMaster() {
Serial.println("Enter the Master PIN to reset user PIN:");
String enteredMasterPIN = getValidatedPIN();
if (enteredMasterPIN == MASTER_PIN) {
Serial.println("Enter a new 4-digit User PIN:");
String newPIN = getValidatedPIN();
for (int i = 0; i < 4; i++) {
EEPROM.write(EEPROM_USER_PIN_ADDR + i, newPIN[i]);
}
// Single commit after the loop (more efficient and correct)
EEPROM.commit();
Serial.println("User PIN reset successfully!");
pin12 = readStoredPIN();
Serial.println("Verifying stored PIN:");
Serial.println(pin12);
} else {
Serial.println("Incorrect Master PIN. Reset failed.");
}
}
String getValidatedPIN() {
while (true) {
while (Serial.available() == 0)
; // Wait for user input
String recv = Serial.readStringUntil('\n'); // Read until newline
recv.trim(); // Remove CR/LF and spaces
// Must be exactly 4 digits
if (recv.length() == 4 && isDigits(recv)) {
return recv;
}
if (recv.length() > 0) {
Serial.println("Invalid input. Please enter a 4-digit PIN:");
}
}
}
bool isDigits(String str) {
for (int i = 0; i < str.length(); i++) {
if (!isDigit(str[i])) return false;
}
return true;
}
String readStoredPIN() {
char pin[5]; // 4 digits + null
for (int i = 0; i < 4; i++) {
pin[i] = EEPROM.read(EEPROM_USER_PIN_ADDR + i);
}
pin[4] = '\0';
return String(pin);
}setInitialPIN() to create a new 4-digit PIN.setInitialPIN();verifyPIN();updateUserPIN();resetPINWithMaster();getValidatedPIN();isDigits(String str);readStoredPIN();EEPROM.begin(8000)) can corrupt other Flash data or cause runtime errors.EEPROM.commit() only when values actually change.EEPROM.commit() after writingWe are using the Arduino UNO development board and programming it using the Arduino IDE.
Connect the Arduino UNO development board to your computer using a USB cable.
We are going to use EEPROM.h library to implement this task. In Arduino UNO, there is a separate EEPROM chip available of 1KB.
#include <EEPROM.h>
#define EEPROM_USER_PIN_ADDR 0 // Starting address for user PIN in EEPROM
#define MASTER_PIN "1234" // Master PIN for reset (stored as a string)
void setup() {
Serial.begin(9600);
Serial.println("Password-Protected Locker System Started!");
// Check if a user PIN is already set
if (EEPROM.read(EEPROM_USER_PIN_ADDR) == 255) { // Uninitialized EEPROM value
setInitialPIN(); // Prompt user to set initial PIN
} else {
Serial.println("User PIN is already set. Enter your PIN to access.");
}
}
void loop() {
Serial.println("\nMain Menu:");
Serial.println("1. Enter PIN to Unlock");
Serial.println("2. Update User PIN");
Serial.println("3. Reset PIN with Master PIN");
Serial.print("Choose an option (1-3): ");
while (Serial.available() == 0)
; // Wait for user input
int option = Serial.parseInt();
Serial.println(option);
switch (option) {
case 1:
verifyPIN();
break;
case 2:
updateUserPIN();
break;
case 3:
resetPINWithMaster();
break;
default:
Serial.println("Invalid option. Please try again.");
}
}
void setInitialPIN() {
Serial.println("No PIN found. Set a new 4-digit PIN:");
String newPIN = getValidatedPIN();
for (int i = 0; i < 4; i++) {
EEPROM.write(EEPROM_USER_PIN_ADDR + i, newPIN[i]);
}
Serial.println("User PIN set successfully!");
}
void verifyPIN() {
Serial.println("Enter your 4-digit PIN to unlock:");
String enteredPIN = getValidatedPIN();
String storedPIN = readStoredPIN();
if (enteredPIN == storedPIN) {
Serial.println("Access Granted!");
} else {
Serial.println("Incorrect PIN. Access Denied.");
}
}
void updateUserPIN() {
Serial.println("Enter your current 4-digit PIN:");
String currentPIN = getValidatedPIN();
String storedPIN = readStoredPIN();
if (currentPIN == storedPIN) {
Serial.println("Enter your new 4-digit PIN:");
String newPIN = getValidatedPIN();
for (int i = 0; i < 4; i++) {
EEPROM.write(EEPROM_USER_PIN_ADDR + i, newPIN[i]);
}
Serial.println("User PIN updated successfully!");
} else {
Serial.println("Incorrect current PIN. Update failed.");
}
}
void resetPINWithMaster() {
Serial.println("Enter the Master PIN to reset user PIN:");
String enteredMasterPIN = getValidatedPIN();
if (enteredMasterPIN == MASTER_PIN) {
Serial.println("Enter a new 4-digit User PIN:");
String newPIN = getValidatedPIN();
for (int i = 0; i < 4; i++) {
EEPROM.write(EEPROM_USER_PIN_ADDR + i, newPIN[i]);
}
Serial.println("User PIN reset successfully!");
} else {
Serial.println("Incorrect Master PIN. Reset failed.");
}
}
String getValidatedPIN() {
while (true) {
while (Serial.available() == 0)
; // Wait for user input
String recv = Serial.readStringUntil('\n'); // Read input until newline
recv.trim(); // Remove leading/trailing whitespace and CR/LF
// Check if input is exactly 4 digits
if (recv.length() == 4 && isDigits(recv)) {
return recv; // Return valid 4-digit PIN
}
if (recv.length() > 1)
Serial.println("Invalid input. Please enter a 4-digit PIN:");
}
}
bool isDigits(String str) {
for (int i = 0; i < str.length(); i++) {
if (!isDigit(str[i])) {
return false; // Return false if any character is not a digit
}
}
return true;
}
String readStoredPIN() {
char pin[5]; // Buffer for 4 digits + null terminator
for (int i = 0; i < 4; i++) {
pin[i] = EEPROM.read(EEPROM_USER_PIN_ADDR + i);
}
pin[4] = '\0'; // Null-terminate the string
return String(pin);
}setup();loop();setInitialPIN();verifyPIN();updateUserPIN();resetPINWithMaster();getValidatedPIN();isDigits(String str);readStoredPIN();