Using the RC522 RFID Module with the Raspberry Pi

Using the RC522 RFID Module with the Raspberry Pi

The RC522 RFID module allows the Raspberry Pi to read and write RFID (Radio Frequency Identification) tags, making it useful for access control, automation, and IoT projects. This guide will show you how to connect, configure, and read RFID cards using the Raspberry Pi and Python.


What You Will Need

  1. Raspberry Pi (any model with SPI support, e.g., Pi 3, Pi 4)
  2. RC522 RFID Module
  3. RFID Tag or Card (compatible with 13.56MHz frequency)
  4. Breadboard and Jumper Wires
  5. Python installed on your Raspberry Pi

Step 1: Wiring the RC522 to the Raspberry Pi

The RC522 communicates with the Raspberry Pi using the SPI protocol.

Connections (SPI Mode)

RC522 Pin Raspberry Pi Pin Function
VCC 3.3V (Pin 1) Power Supply
GND Ground (Pin 6) Ground
SDA (SS) GPIO8 (Pin 24) SPI Chip Select
SCK GPIO11 (Pin 23) SPI Clock
MOSI GPIO10 (Pin 19) SPI Data Input
MISO GPIO9 (Pin 21) SPI Data Output
RST GPIO25 (Pin 22) Reset

Note: The RC522 operates at 3.3V. Do not connect it to 5V, as it may damage the module.


Step 2: Enable the SPI Interface

The RC522 uses SPI (Serial Peripheral Interface) for communication, so we need to enable SPI on the Raspberry Pi.

  1. Open the Raspberry Pi configuration tool:

    sudo raspi-config
    
  2. Navigate to Interface Options > SPI and enable it.

  3. Reboot the Raspberry Pi:

    sudo reboot
    
  4. Verify that SPI is enabled:

    ls /dev/spidev*
    

    You should see /dev/spidev0.0 and /dev/spidev0.1.


Step 3: Install Required Libraries

  1. Update your Raspberry Pi:
    sudo apt update && sudo apt upgrade -y
    
  2. Install the SPI and RFID libraries:
    sudo apt install -y python3-pip python3-spidev python3-rpi.gpio
    pip3 install mfrc522
    

Step 4: Python Code to Read RFID Data

Here’s a Python script to read RFID tag/card data using the RC522 module.

Python Code Example

import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522

reader = SimpleMFRC522()

try:
    print("Place your RFID tag or card near the reader...")
    id, text = reader.read()
    print(f"RFID Tag ID: {id}")
    print(f"Stored Text: {text}")

except KeyboardInterrupt:
    print("Exiting...")
finally:
    GPIO.cleanup()

Step 5: Writing Data to an RFID Tag

You can also write custom text to an RFID tag.

Python Code Example for Writing Data

import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522

reader = SimpleMFRC522()

try:
    text = input("Enter text to write to the RFID tag: ")
    print("Place your RFID tag near the reader...")
    reader.write(text)
    print("Data written successfully!")

except KeyboardInterrupt:
    print("Exiting...")
finally:
    GPIO.cleanup()

Step 6: Applications of the RC522 RFID Module

  1. Access Control – Use RFID cards to unlock doors or grant access to restricted areas.
  2. Attendance Systems – Log entries and exits in schools, offices, or events.
  3. Inventory Management – Track items using RFID tags.
  4. IoT and Automation – Trigger smart home devices or industrial systems with RFID authentication.

Troubleshooting

  1. RFID Tag Not Detected

    • Ensure the tag is within 2-3 cm of the reader.
    • Check the wiring and connections.
  2. Permission Denied Error

    • Run the script with sudo:
      sudo python3 rfid_read.py
      
  3. No SPI Device Found

    • Ensure SPI is enabled (ls /dev/spidev* should list devices).
    • Try re-enabling SPI via sudo raspi-config.

Conclusion

The RC522 RFID module is a powerful tool for integrating RFID-based identification and automation into Raspberry Pi projects. By following this guide, you can easily read and write RFID tag data, enabling applications in access control, security, and IoT projects. Experiment with different use cases to unlock its full potential! 🚀

Leave a comment

Notice an Issue? Have a Suggestion?
If you encounter a problem or have an idea for a new feature, let us know! Report a problem or request a feature here.