Raspberry Pi - RFID

This tutorial instructs you how to use RFID/NFC with Raspberry Pi. The RFID/NFC system consists of two components: a reader and a tag. Two of the most popular RFID/NFC readers are the RC522 and PN532. This tutorial will uses the RC522 RFID/NFC reader, which is cheap and easy to use.

The RC522 RFID/NFC reader can:

This tutorial focuses on:

Hardware Preparation

1×Raspberry Pi 4 Model B
1×RFID/NFC RC522 Kit (reader + tags)
1×RFID Key Fob
1×RFID Card
1×Jumper Wires
1×(Optional) Screw Terminal Block Shield for Raspberry Pi
1×(Optional) USB-C Power Cable with On/Off Switch for Raspberry Pi 4B
1×(Optional) Plastic Case and Cooling Fan for Raspberry Pi 4B
1×(Optional) HDMI Touch Screen Monitor for Raspberry Pi

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of RFID-RC522 Module

RFID-RC522 Module Pinout

The RFID-RC522 has 8 pins, some of which are common pins and the others are shared among three communication modes: SPI, I2C, and UART. Only one communication mode can be used at a time. The pins are:

  • GND pin: This needs to be connected to GND (0V).
  • VCC pin: This needs to be connected to VCC (3.3).
  • RST pin: This is a pin for reset and power-down. When this pin goes low, hard power-down is enabled. On the rising edge, the module is reset.
  • IRQ pin: This is an interrupt pin that can alert the microcontroller when an RFID tag comes into its vicinity.
  • MISO/SCL/TX pin: This acts as MISO when SPI interface is enabled, acts as SCL when I2C interface is enabled and acts as TX when UART interface is enabled.
  • MOSI pin: This acts as MOSI when SPI interface is enabled.
  • SCK pin: This acts as SCK when SPI interface is enabled.
  • SS/SDA/RX pin: This acts as SS when SPI interface is enabled, acts as SDA when I2C interface is enabled and acts as RX when UART interface is enabled.
RFID-RC522 pinout

※ NOTE THAT:

  • The pins of the module may be arranged differently depending on the manufacturer. It is important to use the labels printed on the module, as seen in the image above from DIYables.
  • Do not make the mistake of connecting the VCC pin to the 5V pin, as this could damage the module.
  • The MFRC522 library only supports SPI mode, so this tutorial will focus on SPI communication.

How RFID/NFC Works

RFID/NFC consists of two components: reader and tag:

  • The reader is made up of a radio frequency module and an antenna which produces a high frequency electromagnetic field.
  • The tag is generally a passive device, not requiring a power source. It has a microchip that stores and processes information, as well as an antenna to send and receive signals. The tag is used to store the information including UID (Unique ID) and data.

The tag must be close to the reader in order to read the information it contains. The process of reading is as follows:

  • The reader generates an electromagnetic field that causes electrons to flow through the antenna of the tag, powering the chip.
  • The chip inside the tag then responds by sending the requested information back to the reader in the form of a radio signal.
  • The reader detects the signal and converts it into data.
  • Raspberry Pi reads the data from the reader.

Wiring Diagram between RFID-RC522 Module and Raspberry Pi

The wiring diagram between Raspberry Pi and RFID RC522

This image is created using Fritzing. Click to enlarge image

To simplify and organize your wiring setup, we recommend using a Screw Terminal Block Shield for Raspberry Pi. This shield ensures more secure and manageable connections, as shown below:

Raspberry Pi Screw Terminal Block Shield

Wiring table of RFID/NFC RC522 Module and Raspberry Pi

RC522 RFID Reader Raspberry Pi
SS Pin 24 (GPIO8)
SCK Pin 23 (GPIO11)
MOSI Pin 19 (GPIO10)
MISO Pin 21 (GPIO9)
IRQ Not connected (can be left unconnected)
GND Any GND Pin
RST Pin 31 (GPIO12)
VCC Pin 1 or Pin 16 (3.3V)

Raspberry Pi RFID/NFC Code

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Enable the SPI interface on Raspberry Pi by following the instruction on Raspberry Pi - how to enable SPI inteface
  • Make sure you have the spidev library installed. If not, install it using the following command:
sudo apt-get install python3-pip python3-dev git sudo pip3 install spidev
  • Make sure you have the mfrc522 library installed. If not, install it using the following command:
sudo pip3 install mfrc522
  • Create a Python script file rc522_rfid.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-rfid """ import RPi.GPIO as GPIO import MFRC522 # Define the SPI and RST pins for the RC522 module SPI_PORT = 0 SPI_DEVICE = 0 RST_PIN = 12 # Create an instance of the MFRC522 class MIFAREReader = MFRC522.MFRC522() # Function to read the UID from an RFID card and print it in hexadecimal format def read_uid(): # Scan for cards (status, TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL) # If a card is found if status == MIFAREReader.MI_OK: # Get the UID of the card (status, uid) = MIFAREReader.MFRC522_Anticoll() if status == MIFAREReader.MI_OK: # Convert the UID bytes to a hexadecimal string uid_hex = ''.join(['{:02X}'.format(val) for val in uid]) print("UID (Hex): " + uid_hex) # Setup GPIO and initialize the RC522 module GPIO.setmode(GPIO.BOARD) GPIO.setup(RST_PIN, GPIO.OUT) GPIO.output(RST_PIN, GPIO.HIGH) MIFAREReader.MFRC522_Init() try: print("Press Ctrl+C to exit.") while True: read_uid() except KeyboardInterrupt: print("\nExiting the program.") GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 rc522_rfid.py
  • Tap various RFID/NFC tags to the RFID-RC522 module.
  • Check the UID on the Serial Monitor.
PuTTY - Raspberry Pi
Press Ctrl+C to exit. UID (Hex): 12AB34CD UID (Hex): 567890EF

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!