Build Your Own RFID & Face Recognition Attendance System

by fwsalim in Circuits > Electronics

89 Views, 1 Favorites, 0 Comments

Build Your Own RFID & Face Recognition Attendance System

photo_2025-06-17_09-34-37.jpg
photo_2025-06-17_09-34-39 (2).jpg
photo_2025-06-17_09-34-38.jpg
photo_2025-06-17_09-34-36.jpg

This guide will walk you through building an automated attendance system using a Raspberry Pi, an Arduino, RFID technology, and face recognition. The system will identify individuals based on their RFID card and a camera-based face scan, logging all access events locally, and providing web interfaces for managing students and viewing attendance dashboards.

Project Overview:

  1. RFID Scanning: Students tap their RFID card.
  2. Face Recognition: A webcam identifies the person's face.
  3. Verification: The system checks if the scanned RFID card belongs to the recognized person.
  4. Access Control: Green/Red LEDs and a buzzer provide feedback (GRANTED/DENIED).
  5. Local Logging: All access events (timestamp, RFID, name, status) are saved to a local JSON file.
  6. Web Interfaces:
  7. Student Management: Add, edit, and delete student RFID/name pairs (stored in local browser storage).
  8. Access Logs: View raw log data in a table.
  9. Attendance Dashboard: Visualize attendance trends and statistics with charts.

Supplies

12b919fb-b23b-4f73-8b46-44a5423d30ca.jpeg
photo_2025-06-17_09-34-41 (2).jpg
img_4118_board_in_hand.jpg
RC522-Sensor-Connection-To-Arduino.jpg
photo_2025-06-17_09-34-42.jpg
resistor-in-hand-jpg_1391131083.jpg
AngryCAM_USB_V2_120+_FrontView_Close.jpg

Based on the images you provided and our discussions, here are the components you'll need:

Hardware Components:

  1. Raspberry Pi: A Raspberry Pi 4 or 5 is recommended for performance with face recognition.
  2. MicroSD Card: 16GB or larger, Class 10, for Raspberry Pi OS.
  3. Raspberry Pi Power Supply: Official 5.1V 3A USB-C power supply.
  4. Arduino Mega 2560: Or a compatible Arduino board with multiple serial ports.
  5. RFID-RC522 Module: RFID reader/writer module.
  6. MCUFRIEND_kbv compatible TFT Display Shield (Velleman VMA412 2.8"): A touch-enabled TFT display shield (like the Velleman VMA412 2.8" TFT Shield for Arduino Mega).
  7. USB Webcam: Compatible with Raspberry Pi (e.g., a standard USB webcam).
  8. Buzzer: A small active buzzer.
  9. LEDs (Green & Red): 1 Green LED, 1 Red LED.
  10. Resistors (2 x 220 Ohm): 2 x 220 Ohm resistors (for LEDs, to protect them from high current).
  11. Jumper Wires (Assorted): Male-to-Male, Female-to-Female, Male-to-Female for connections.
  12. Breadboard: For easier prototyping and wiring.
  13. Enclosure (3D Printed): As shown in your images, a custom 3D printed case for the Raspberry Pi, Arduino, TFT, webcam, LEDs, and RFID reader. (Note: Since this is a custom 3D printed part, a direct purchase link isn't possible. You would typically provide links to the 3D model files if you designed them, or a service that prints custom enclosures).

Software & Files:

  1. Raspberry Pi OS: (Lite or Desktop version, 64-bit recommended)
  2. Arduino IDE: For programming the Arduino.
  3. main.py: The Python script for Raspberry Pi (provided in our previous conversation).
  4. best.pt: Your trained YOLOv8 face recognition model file.
  5. logs.html: Web page to view raw access logs.
  6. manage_students.html: Web page to manage student RFID/name pairs.
  7. dashboard.html: Web page to view attendance analytics.
  8. arduino_code.ino: The Arduino sketch (provided in the arduino_code immersive).

Software Setup (Raspberry Pi)

photo_2025-06-17_09-34-40.jpg
photo_2025-06-17_09-34-43.jpg

This section covers setting up your Raspberry Pi's operating system and Python environment.

2.1. Install Raspberry Pi OS

  1. Download Raspberry Pi Imager: Go to raspberrypi.com/software/ and download the Raspberry Pi Imager for your computer's OS.
  2. Choose OS: Open the Imager. Select "Choose OS" -> "Raspberry Pi OS (other)" -> "Raspberry Pi OS (64-bit)" (or "Raspberry Pi OS Full (64-bit)" if you prefer a desktop environment).
  3. Choose Storage: Select your MicroSD card.
  4. Write: Click "Write" to flash the OS to the MicroSD card. This may take some time.
  5. Initial Setup: Insert the MicroSD card into your Raspberry Pi, connect power, monitor, keyboard, and mouse. Follow the on-screen prompts to set up Wi-Fi, change password, update, etc.

2.2. Install Python Dependencies

  1. Open Terminal: On your Raspberry Pi, open a terminal.
  2. Update System:
sudo apt update
sudo apt full-upgrade -y

  1. Install Python Development Tools:
sudo apt install python3-dev python3-pip python3-venv -y

  1. Install OpenCV dependencies (Optional, but good practice):
sudo apt install libhdf5-dev libhdf5-serial-dev libatlas-base-dev libjasper-dev libqtgui4 libqt4-test -y

  1. Create a Python Virtual Environment:
mkdir -p ~/attendance_system/pi_code
cd ~/attendance_system/pi_code
python3 -m venv venv
source venv/bin/activate

  1. Install Python Libraries:
pip install pyserial opencv-python ultralytics gpiozero numpy

  1. pyserial: For serial communication with Arduino.
  2. opencv-python: For webcam access and image processing.
  3. ultralytics: For YOLOv8 model inference.
  4. gpiozero: For easy control of GPIO pins (LEDs, buzzer).
  5. numpy: A dependency for OpenCV and other scientific computing.

2.3. Transfer Project Files

Transfer your Python script (main.py), your trained YOLO model (best.pt), and the HTML files (logs.html, manage_students.html, dashboard.html) to the ~/attendance_system/pi_code/ directory on your Raspberry Pi. You can use SSH (SCP), SFTP (e.g., with FileZilla), or a USB drive.

2.4. Enable Raspberry Pi Serial Port

The Raspberry Pi's GPIO pins include a hardware serial port (UART) that needs to be enabled for communication with the Arduino.

  1. Open Raspberry Pi Configuration:
sudo raspi-config

  1. Navigate to Interfacing Options: Select 3 Interface Options -> P6 Serial Port.
  2. Disable Login Shell: It will ask "Would you like a login shell to be accessible over serial?" Select No.
  3. Enable Serial Hardware: It will ask "Would you like the serial port hardware to be enabled?" Select Yes.
  4. Finish: Select "Finish" and reboot if prompted.


Arduino Setup

This section details how to prepare and program your Arduino board for the RFID reader and TFT display.

3.1. Install Arduino IDE

  1. Download Arduino IDE: Go to arduino.cc/en/software and download the Arduino IDE for your operating system.
  2. Install: Follow the installation instructions for your OS.

3.2. Install Arduino Libraries

Open the Arduino IDE and go to Sketch -> Include Library -> Manage Libraries.... Search for and install the following libraries:

  1. MFRC522: By UIPEthernet or miguelbalboa (make sure it's the one for RFID-RC522).
  2. MCUFRIEND_kbv: By David Prentice. This is crucial for your TFT display shield.
  3. Adafruit GFX Library: By Adafruit. (A dependency for MCUFRIEND_kbv).
  4. SPI: (Usually pre-installed, but confirm it's available).

3.3. Upload Arduino Code

  1. Copy Arduino Code: Copy the content from the arduino_code Canvas (titled "Arduino TFT Display Controller (Portrait)").
  2. Create New Sketch: In the Arduino IDE, go to File -> New Sketch.
  3. Paste Code: Delete the default code and paste the copied Arduino code into the new sketch.
  4. Select Board & Port:
  5. Go to Tools -> Board and select Arduino Mega 2560.
  6. Go to Tools -> Port and select the serial port corresponding to your Arduino Mega (e.g., /dev/ttyACM0 on Linux/Raspberry Pi, or COMx on Windows).
  7. Upload: Click the "Upload" button (right arrow icon) to compile and upload the sketch to your Arduino board.


Hardware Assembly & Wiring

photo_2025-06-17_10-04-33.jpg
photo_2025-06-17_10-04-32.jpg
photo_2025-06-17_10-04-31 (2).jpg
photo_2025-06-17_10-04-31.jpg
photo_2025-06-17_10-04-30.jpg

is where you'll connect all your physical components. Always disconnect power before making or changing any connections.

4.1. Raspberry Pi and Camera

  1. Attach your USB webcam to any available USB port on the Raspberry Pi.

4.2. Arduino, TFT Display, and RFID Module

Your images show a TFT display shield (like the Velleman VMA412) that plugs directly into the Arduino Mega.

  1. Attach TFT Display Shield: Carefully align and press the TFT display shield onto the Arduino Mega 2560. It should fit snugly.
  2. Wire RFID-RC522 to Arduino:
  3. The RFID-RC522 module uses SPI communication. Your TFT shield might also use SPI, but typically it leaves the main SPI pins on the Mega (ICSP header pins or specific digital pins) available.
  4. RC522 connections (standard):
  5. SDA (SS) -> Arduino Digital Pin 53 (as defined in arduino_code.ino)
  6. SCK -> Arduino Digital Pin 52 (Mega SPI SCK)
  7. MOSI -> Arduino Digital Pin 51 (Mega SPI MOSI)
  8. MISO -> Arduino Digital Pin 50 (Mega SPI MISO)
  9. RST -> Arduino Digital Pin 49 (as defined in arduino_code.ino)
  10. 3.3V -> Arduino 3.3V pin
  11. GND -> Arduino GND pin
  12. Verify Pin Definitions: Double-check the SS_PIN and RST_PIN definitions in your arduino_code.ino match your actual wiring.

4.3. Raspberry Pi to Arduino Serial Communication

The Raspberry Pi will communicate with the Arduino using its UART (serial) pins. The Arduino Mega has multiple hardware serial ports. We are using Serial1 in the Arduino code, which corresponds to pins TX1 (Digital 18) and RX1 (Digital 19) on the Arduino Mega.

  1. Raspberry Pi GPIO to Arduino Mega:
  2. Raspberry Pi GPIO TX (pin 8) --- (connect via 1K Ohm resistor) --- Arduino Mega RX1 (Digital 19)
  3. Raspberry Pi GPIO RX (pin 10) --- (connect via 1K Ohm resistor) --- Arduino Mega TX1 (Digital 18)
  4. Raspberry Pi GND (any GND pin) --- Arduino Mega GND
  5. Important: The Raspberry Pi's GPIO operates at 3.3V, while the Arduino Mega operates at 5V. While some people connect directly, using a simple voltage divider (e.g., a 1K Ohm resistor in series on both TX/RX lines) is a safer practice to protect your Pi's GPIO pins from potential 5V input from the Arduino.
  6. For Pi TX to Arduino RX: Pi TX (3.3V) goes directly to Arduino RX (5V tolerant, typically fine).
  7. For Arduino TX to Pi RX: Arduino TX (5V) needs to be stepped down to 3.3V for Pi RX. A simple voltage divider with two resistors (e.g., 1K and 2K Ohm) can achieve this, or a simple 1K Ohm in series for level shifting can often suffice for these low-speed signals. The simplest is often just a 1K Ohm resistor in series.

4.4. LEDs and Buzzer to Raspberry Pi GPIO

These connect directly to the Raspberry Pi's GPIO pins using gpiozero.

  1. Green LED:
  2. Longer leg (Anode) -> 220 Ohm Resistor -> Raspberry Pi GPIO Pin 6 (as defined in main.py)
  3. Shorter leg (Cathode) -> Raspberry Pi GND (any GND pin)
  4. Red LED:
  5. Longer leg (Anode) -> 220 Ohm Resistor -> Raspberry Pi GPIO Pin 13 (as defined in main.py)
  6. Shorter leg (Cathode) -> Raspberry Pi GND (any GND pin)
  7. Buzzer (Active Buzzer):
  8. Positive (+) leg -> Raspberry Pi GPIO Pin 12 (as defined in main.py)
  9. Negative (-) leg -> Raspberry Pi GND (any GND pin)
  10. (Note: Active buzzers typically have built-in oscillators, so they just need power on/off).

4.5. Enclosure Assembly

Based on your images, you have a custom 3D printed enclosure.

  1. Carefully mount the Raspberry Pi, Arduino with TFT shield, RFID module, webcam, and LEDs/buzzer into the respective slots in your 3D printed case.
  2. Ensure all wired connections remain secure as you assemble.
  3. The webcam is shown mounted on top, and LEDs are visible on the front panel.


Operation Instructions

Once all hardware is connected and software is configured, you can run your system.

  1. Power On:
  2. Connect the Arduino to power (e.g., via USB to a computer, or a separate 9V power supply).
  3. Connect the Raspberry Pi to its power supply.
  4. Start Raspberry Pi Python Script:
  5. Open a terminal on your Raspberry Pi.
  6. Navigate to your project directory:
cd ~/attendance_system/pi_code

  1. Activate your Python virtual environment:
source venv/bin/activate

  1. Run the main script:
python main.py

  1. You should see output indicating that serial port, webcam, YOLO model, and GPIO pins are initialized. The script will then enter its main loop, waiting for RFID scans.
  2. Operate the System:
  3. Scan an RFID Card: Present a registered RFID card to the RC522 module.
  4. Face Verification: Look at the webcam after scanning the card.
  5. Feedback: The Arduino's TFT display will show "VERIFYING", then "GRANTED" or "DENIED". The green/red LEDs and buzzer will provide audio-visual feedback.
  6. Local Logging: Each access attempt (whether granted or denied) will be logged to the logs.json file in your ~/attendance_system/pi_code/ directory.
  7. Start the Web Server (in a new terminal):
  8. Open a second terminal on your Raspberry Pi.
  9. Navigate to your project directory:
cd ~/attendance_system/pi_code

  1. Start the simple Python HTTP server:
python3 -m http.server 8000

  1. This server needs to remain running in the background for your web pages to be accessible.
  2. Access Web Interfaces (from any device on the same network):
  3. Replace <Your_Raspberry_Pi_IP_Address> with the actual IP address of your Raspberry Pi (you can find it by typing hostname -I in a Pi terminal).
  4. Student Management Interface (manage_students.html):
  5. Open your web browser and go to: http://<Your_Raspberry_Pi_IP_Address>:8000/manage_students.html
  6. Here, you can add, edit, or delete student names and their RFID UIDs. Remember this data is stored in your browser's local storage and is not synced with the RFID_MAP in your main.py. You'll need to manually update main.py if you change the RFID_MAP there.
  7. Student Access Logs (logs.html):
  8. Open your web browser and go to: http://<Your_Raspberry_Pi_IP_Address>:8000/logs.html
  9. This page will display the raw log data from logs.json in a table.
  10. Attendance Dashboard (dashboard.html):
  11. Open your web browser and go to: http://<Your_Raspberry_Pi_IP_Address>:8000/dashboard.html
  12. This page will show graphical summaries and statistics of your attendance data from logs.json.


Troubleshooting Tips

"Serial port not found" or "Permission denied" error on Raspberry Pi:

  1. Solution: Ensure the serial port is enabled in sudo raspi-config (Interface Options -> P6 Serial Port -> No for login shell, Yes for hardware). Also, ensure your user is in the dialout group: sudo usermod -a -G dialout $USER then reboot.

Webcam not opening (cv2.VideoCapture(0) fails):

  1. Solution: Ensure the webcam is properly connected. Try lsusb in the Pi terminal to see if it's detected. Reboot the Pi.

YOLO model not loading (best.pt error):

  1. Solution: Verify best.pt is in the same directory as main.py and is not corrupted. Ensure ultralytics library is correctly installed (pip install ultralytics).

GPIO errors (gpiozero issues):

  1. Solution: Ensure gpiozero is installed (pip install gpiozero). Verify correct pin numbers are used in main.py. Make sure no other process is using the GPIO pins.

"logs.json: No such file or directory" or "Unexpected end of JSON input" on web page:

  1. Solution: Ensure main.py is running and successfully logging events. The logs.json file needs to be created and populated by main.py. If it's empty, stop main.py, delete logs.json, then restart main.py and trigger some access events.

Web page stuck on "Loading..." or not displaying anything:

  1. Solution: Open the browser's developer console (F12 or right-click -> Inspect -> Console tab) for detailed JavaScript errors. Ensure the Python HTTP server is running in the correct directory. Check for network connectivity between your viewing device and the Raspberry Pi.

Arduino TFT Display Blank or not responding:

  1. Solution: Ensure MCUFRIEND_kbv and Adafruit GFX Library are installed in Arduino IDE. Verify tft.begin(ID) has a correct ID. Check power and connections to the TFT shield.

RFID not reading:

  1. Solution: Check wiring of RC522 to Arduino. Ensure MFRC522 library is installed. Test with a known working RFID card/fob.


Enjoy Your Day!