Differences

This shows you the differences between two versions of the page.

Link to this comparison view

pm:prj2026:jan.vaduva:mihai.paunescu1801 [2026/05/18 16:39]
mihai.paunescu1801
pm:prj2026:jan.vaduva:mihai.paunescu1801 [2026/05/22 20:01] (current)
mihai.paunescu1801
Line 285: Line 285:
  
 ===== Software Design ===== ===== Software Design =====
 +
 +The software implementation is currently functional and integrates all the main hardware components of the project.
 +
 +The implemented features are:
 +  * RFID card detection using the RC522 module
 +  * validation of two authorized RFID cards
 +  * PIN input using a 4x4 matrix keypad
 +  * PIN verification using a 4-digit code
 +  * PIN change functionality using the D key
 +  * PIN storage in EEPROM memory
 +  * OLED display messages for each system state
 +  * visual feedback using green and red LEDs
 +  * audio feedback using a buzzer
 +  * electromagnetic lock control through a MOSFET driver
 +  * warning mechanism after three failed authentication attempts
 +  * UART debug messages for testing and validation
 +
 +The system starts in the default state where the OLED display shows:
 +
 +  * SMART LOCKER
 +  * SCAN CARD
 +
 +In this state, the microcontroller continuously checks for an RFID card. If the scanned card is authorized, the system moves to the PIN input state. If the card is not authorized, access is denied and the user is asked to scan again.
 +
 +After a valid RFID card is detected, the user must enter the correct PIN. If the PIN is correct, the electromagnetic lock is activated for a few seconds, the green LED turns on, the buzzer plays a success sound and the display shows that the locker is unlocked. If the PIN is incorrect, the red LED turns on, the buzzer emits an error sound and the display shows an access denied message.
 +
 +The software also supports changing the current PIN. This is done by pressing the D key while the system is waiting for an RFID card. The user must scan an authorized RFID card, enter the old PIN, enter the new PIN and confirm it. If both new PIN entries match, the new PIN is saved in EEPROM and will remain stored even after the board is powered off.
 +
 +==== Libraries and Low-Level Modules Used ====
 +
 +The project was implemented in C using AVR-specific libraries. I chose to use low-level AVR libraries instead of high-level Arduino libraries because they provide direct control over the microcontroller registers and make the project more relevant to the laboratory concepts.
 +
 +The main libraries used are:
 +
 +  * avr/io.h
 +  * util/​delay.h
 +  * stdint.h
 +  * avr/​eeprom.h
 +
 +The avr/io.h library is used for direct access to the microcontroller registers. This allows the program to configure pins as inputs or outputs, enable pull-up resistors, control PORT registers and use hardware peripherals such as SPI, TWI/I2C and UART.
 +
 +The util/​delay.h library is used for generating small delays required by the hardware modules. For example, delays are used for keypad debouncing, buzzer tone generation, RFID reset timing, OLED initialization and keeping the lock open for a fixed time.
 +
 +The stdint.h library is used for fixed-width integer types such as uint8_t and uint16_t. This is important in embedded systems because the size of each variable must be predictable.
 +
 +The avr/​eeprom.h library is used to store the PIN code in the internal EEPROM memory of the ATmega328P. This allows the PIN to remain saved even after the system is restarted or disconnected from power.
 +
 +No external Arduino libraries were used for the main components. The RFID, SPI, I2C, OLED, keypad, buzzer and lock control logic were implemented manually. This gives more control over how each peripheral works and demonstrates a better understanding of the microcontroller.
 +
 +==== Project Structure ====
 +
 +The software was organized into multiple modules in order to make the project easier to understand, maintain and debug.
 +
 +The main files of the project are:
 +
 +  * main.c - contains the main initialization and the main loop
 +  * config.h - contains pin definitions,​ RFID register definitions and constants
 +  * app_state.c / app_state.h - contains the global state of the application
 +  * uart.c / uart.h - contains UART debugging functions
 +  * spi.c / spi.h - contains SPI communication functions
 +  * rfid.c / rfid.h - contains the RC522 RFID logic
 +  * twi.c / twi.h - contains I2C/TWI communication functions
 +  * oled.c / oled.h - contains OLED display functions
 +  * keypad.c / keypad.h - contains keypad scanning logic
 +  * buzzer.c / buzzer.h - contains buzzer sound functions
 +  * pin_manager.c / pin_manager.h - contains PIN verification,​ EEPROM loading/​saving and PIN change helpers
 +  * lock_control.c / lock_control.h - contains lock, error and warning feedback functions
 +  * app_logic.c / app_logic.h - contains the higher-level authentication logic
 +
 +The project is based on a state machine. The main states are:
 +
 +  * STATE_WAIT_RFID
 +  * STATE_WAIT_PIN
 +  * STATE_CHANGE_WAIT_RFID
 +  * STATE_CHANGE_OLD_PIN
 +  * STATE_CHANGE_NEW_PIN
 +  * STATE_CHANGE_CONFIRM_PIN
 +
 +The state machine makes the program easier to control because each user action is interpreted depending on the current state.
 +
 +For example:
 +  * in STATE_WAIT_RFID,​ the system only waits for a valid RFID card
 +  * in STATE_WAIT_PIN,​ the system accepts PIN digits
 +  * in STATE_CHANGE_OLD_PIN,​ the system verifies the old PIN
 +  * in STATE_CHANGE_NEW_PIN,​ the system stores the new PIN temporarily
 +  * in STATE_CHANGE_CONFIRM_PIN,​ the system checks if the confirmation PIN matches
 +
 +==== Novelty Element ====
 +
 +The novelty of the project consists of combining two authentication methods in a small locker system: RFID card verification and PIN code verification.
 +
 +A normal locker usually uses either a physical key or only a PIN code. In this project, access is granted only if both conditions are satisfied:
 +  * the user has an authorized RFID card
 +  * the user knows the correct PIN code
 +
 +This makes the system more secure than a simple PIN-based locker, because observing the PIN is not enough to unlock the locker without the authorized RFID card.
 +
 +Another important aspect is that the project behaves like a complete access control system, not only like a simple lock circuit. The system includes:
 +  * OLED messages for each authentication step
 +  * visual feedback using red and green LEDs
 +  * audio feedback using a buzzer
 +  * automatic warning after multiple failed attempts
 +  * PIN change functionality
 +  * permanent PIN storage after restart
 +
 +Because of this, the project is closer to a real smart locker system, where the user receives clear feedback and the access process is controlled by multiple security checks.
 +
 +==== Laboratory Functionalities Used ====
 +
 +The project uses several concepts and functionalities studied during the laboratories.
 +
 +Digital input/​output was used for the LEDs, buzzer, keypad and electromagnetic lock control. The LEDs indicate the current authentication state, the buzzer provides audio feedback, and the lock is controlled through a digital output connected to the MOSFET driver. The keypad is also handled using GPIO pins: the rows are configured as outputs, while the columns are configured as inputs with internal pull-up resistors.
 +
 +UART communication was used during development for debugging and validation. The microcontroller sends messages to the computer, such as pressed keypad keys, scanned RFID UIDs, authentication results and PIN change steps. This made it easier to test each module separately before validating the complete system.
 +
 +Timing concepts were used for keypad debouncing, buzzer sound generation, LED blinking, RFID reset timing, OLED initialization and the fixed unlock duration of the electromagnetic lock. The buzzer sounds are generated by rapidly switching a digital output pin between HIGH and LOW with different delays.
 +
 +SPI communication was used for the RFID RC522 module. The ATmega328P acts as the SPI master and communicates with the RFID reader using MOSI, MISO, SCK and SS lines. Through SPI, the microcontroller reads and writes RC522 registers and obtains the UID of the scanned RFID card.
 +
 +I2C communication was used for the OLED display. The display receives commands and data from the microcontroller through the SDA and SCL lines. It is used to show the current state of the system, such as scanning the card, entering the PIN, access denied, unlocked state and successful PIN change.
 +
 +==== Interaction Between Functionalities ====
 +
 +The interaction between modules follows the authentication flow of the locker.
 +
 +First, the main loop continuously scans the keypad and checks the RFID reader when no key is pressed. The RFID module communicates with the microcontroller through SPI. When a card is detected, the UID is read and compared with the authorized UIDs stored in the program.
 +
 +If the RFID card is valid, the application state changes to PIN input mode. The OLED display is updated using the I2C interface and the buzzer emits a success sound. The green and red LEDs blink alternately while the system waits for the PIN.
 +
 +The keypad module scans the 4x4 matrix keypad by activating one row at a time and reading the column inputs. Each digit entered by the user is stored in an array. The OLED display shows progress using star characters instead of showing the actual digits, which improves security.
 +
 +When four digits are entered, the PIN manager compares the entered PIN with the current PIN. If the PIN is correct, the lock control module activates the electromagnetic lock through the MOSFET driver. At the same time, the OLED, LEDs and buzzer provide positive feedback.
 +
 +If the PIN is incorrect, the error feedback function is called. The red LED turns on, the buzzer plays an error sound and the OLED displays an access denied message. After three failed attempts, the warning function is triggered and a stronger buzzer pattern is used.
 +
 +The PIN change functionality uses the same modules, but in a different sequence. The user presses D, scans an authorized RFID card, enters the old PIN, enters the new PIN and confirms it. If the confirmation is correct, the new PIN is saved to EEPROM.
 +
 +==== Validation and Testing ====
 +
 +The project was validated step by step, by testing each component separately and then testing the complete system.
 +
 +The OLED display was tested by showing different messages such as:
 +
 +  * SMART LOCKER / SCAN CARD
 +  * RFID OK / ENTER PIN
 +  * PIN OK / UNLOCKED
 +  * ACCESS / DENIED
 +  * PIN SAVED / SUCCESS
 +
 +This confirmed that the I2C communication and OLED control functions work correctly.
 +
 +The keypad was tested by printing each pressed key over UART. This helped verify that the row-column scanning logic correctly detects all keys.
 +
 +The RFID module was tested by reading and printing the UID of scanned cards through UART. After that, the UID values of the authorized cards were added in the program and compared with the scanned UID.
 +
 +The buzzer and LEDs were tested using different feedback patterns:
 +  * success sound and green LED for valid authentication
 +  * error sound and red LED for invalid authentication
 +  * warning sound after multiple failed attempts
 +
 +The electromagnetic lock was tested by activating the MOSFET driver from the microcontroller. The lock opens only after both authentication steps are passed: valid RFID card and correct PIN.
 +
 +The complete project was validated by testing the following scenarios:
 +  * valid RFID card and correct PIN
 +  * valid RFID card and wrong PIN
 +  * invalid RFID card
 +  * three consecutive wrong attempts
 +  * PIN change with correct old PIN
 +  * PIN change with wrong old PIN
 +  * PIN confirmation mismatch
 +  * EEPROM PIN persistence after restart
 +
 +==== Sensor Calibration ====
 +
 +The project does not use analog sensors that require numerical calibration,​ such as temperature,​ light or distance sensors. However, the RFID reader and keypad required practical validation and calibration.
 +
 +For the RFID module, I tested the reading distance and the orientation of the RFID cards. The card was placed at different distances and angles from the RC522 reader in order to determine a position where the UID is read consistently. The final placement of the RFID reader was chosen so that the card can be scanned reliably without requiring physical contact with the module.
 +
 +The authorized RFID cards were calibrated through an enrollment-like process. Each card was scanned, and its UID was printed through UART. After that, the UID values were added in the program as authorized cards. During operation, every scanned UID is compared with these stored values.
 +
 +For the keypad, calibration consisted of verifying that each physical key corresponds to the correct character in the software matrix. I used UART debug messages to print every pressed key and checked that the detected values matched the real keypad layout.
 +
 +The keypad also required debounce handling. A short delay is used after detecting and releasing a key, so that a single physical press is not interpreted as multiple presses.
 +
 +For the electromagnetic lock, I tested the activation time and selected a duration long enough for the locker to be opened, but short enough to avoid keeping the solenoid powered unnecessarily.
 +
 +==== Optimizations ====
 +
 +Several optimizations were made in the software and hardware behavior of the project.
 +
 +The first optimization was using a state machine. Instead of writing the entire logic as a long sequence of conditions without structure, the program uses explicit states. This makes the logic clearer and avoids accepting inputs in the wrong context.
 +
 +The second optimization was using EEPROM for PIN storage. Without EEPROM, the PIN would reset to the default value every time the system loses power. By storing the PIN in EEPROM, the system behaves more like a real locker, where the changed PIN remains saved.
 +
 +The third optimization was displaying stars instead of the actual PIN digits. This improves privacy and prevents nearby users from reading the PIN directly from the display.
 +
 +The fourth optimization was using internal pull-up resistors for the keypad columns. This reduces the number of external components needed and makes the circuit simpler.
 +
 +The fifth optimization was using SPI for the RFID reader and I2C for the OLED display. These are hardware-supported communication interfaces on the ATmega328P and are more efficient and organized than using many separate GPIO pins.
 +
 +The sixth optimization was separating the lock power circuit from the microcontroller. The electromagnetic lock is powered through an external supply and controlled through a MOSFET driver. This protects the microcontroller from high current consumption.
 +
 +Another practical optimization was adding a timeout mechanism to the I2C communication. Because the electromagnetic lock can introduce electrical noise, the OLED communication may occasionally become unstable. A timeout prevents the program from remaining blocked forever inside an I2C waiting loop.
  
 ===== Results ===== ===== Results =====
 +
 +==== Final Project Behavior ====
 +
 +The final system successfully implements a smart locker with two-step authentication.
 +
 +The normal authentication flow is:
 +  - the system displays SMART LOCKER / SCAN CARD
 +  - the user scans an RFID card
 +  - if the card is valid, the system displays RFID OK / ENTER PIN
 +  - the user enters the 4-digit PIN
 +  - if the PIN is correct, the lock opens and the display shows PIN OK / UNLOCKED
 +  - after a few seconds, the lock closes again and the system returns to SCAN CARD mode
 +
 +If the RFID card is invalid, access is denied and the system asks the user to scan again.
 +
 +If the PIN is wrong, access is denied and the system returns to the initial state.
 +
 +If the user enters a wrong PIN or scans invalid cards multiple times, the system activates a warning behavior using the buzzer and red LED.
 +
 +The PIN change flow is:
 +  - the user presses D
 +  - the system asks for an authorized RFID card
 +  - the user enters the old PIN
 +  - the user enters a new 4-digit PIN
 +  - the user confirms the new PIN
 +  - if the confirmation matches, the new PIN is saved in EEPROM
 +
 +==== Demo Video ====
 +
 +The first demo video presents the main functionality of the Smart Locker system.
 +
 +In this video, I first demonstrate the normal unlocking flow. The locker starts in the initial state and waits for an RFID card. After scanning an authorized RFID card, the system asks for the PIN code. When the correct PIN is entered, the electromagnetic lock is activated, the green LED turns on, the buzzer gives a success sound and the OLED display shows that the locker is unlocked.
 +
 +The video also demonstrates the PIN change functionality. This mode is entered by pressing the D key. After that, the system asks for an authorized RFID card, then for the old PIN code. If the old PIN is correct, the user enters a new PIN and then confirms it. If the confirmation matches, the new PIN is saved and the display shows that the PIN was successfully changed.
 +
 +At the end of the demo, I confirm that the PIN was changed successfully by using the new PIN for authentication.
 +
 +[[https://​drive.google.com/​file/​d/​1yjfFrMsylPaND1jU6PLwlV5uNAcqbV_M/​view?​usp=sharing|Smart Locker Main Demo]]
 +
 +The second demo video presents the security warning mechanism. After three consecutive wrong PIN attempts, the system activates the alarm behavior using the buzzer and red LED, then blocks access for a short waiting period.
 +
 +[[https://​drive.google.com/​file/​d/​1yvxkDKo3Rw6DNS0Kf5T5cH0UWKtEfO21/​view?​usp=sharing|Smart Locker Alarm and Lockout Demo]]
  
 ===== References ===== ===== References =====
 +
 +  * ATmega328P Datasheet - Microchip Technology
 +  * ATmega328P-XMINI User Guide - Microchip Technology
 +  * MFRC522 RFID Reader Datasheet - NXP Semiconductors
 +  * SSD1306 OLED Display Datasheet
 +  * AVR Libc Documentation - avr/io.h, util/​delay.h,​ avr/​eeprom.h
  
pm/prj2026/jan.vaduva/mihai.paunescu1801.1779111570.txt.gz · Last modified: 2026/05/18 16:39 by mihai.paunescu1801
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0