How to Use an RFID RC522 on Raspberry Pi
Contents
Introduction
Radio Frequency Identification (RFID) is a technology that enables wireless communication between a tag and a reader, allowing for automatic tracking and identification of objects.
This technology is widely used in applications such as retail and military supply chains, automated payments, baggage and document tracking, pharmaceutical management, among others.
In this project, we'll explore the reading and writing functionalities of RFID.
Schematic Diagram
Wiring
Setup SPI:
To begin, enable the SPI port on your Raspberry Pi. If it's already enabled, you can skip this step. If you're uncertain, continue with the following:
sudo raspi-config
Navigate using the arrow keys to 3 Interfacing options
-> Yes
-> OK
to complete the SPI setup.
Install Spidev and MFRC522
The spidev
library facilitates interactions with the SPI, making it crucial for this tutorial. We'll need it to allow the Raspberry Pi to communicate with the RFID RC522.
Update spidev
on your Raspberry Pi using the following command:
sudo pip3 install --upgrade spidev
Next, install the MFRC522
library:
sudo pip3 install mfrc522
The MFRC522 library consists of two primary files: MFRC522.py
and SimpleMFRC522.py
. The former handles the core interaction with the RFID RC522 through the Pi’s SPI interface. In contrast, SimpleMFRC522.py
streamlines this by providing a more straightforward set of functions.
Run the code
Start by downloading the code:
cd ~/ wget https://raw.githubusercontent.com/sunfounder/raphael-kit/master/python/2.2.10_write.py
cd ~/ wget https://raw.githubusercontent.com/sunfounder/raphael-kit/master/python/2.2.10_read.py
Execute the following to write a message to the card:
sudo python3 2.2.10_write.py
After launching 2.2.10_write.py
, input your desired message and press Enter. Place the card on the MFRC522 module and wait for the "Data writing is complete" notification. You can then remove the card, or rewrite the message on a different card. Exit using Ctrl+C.
To read the data from the card or tag you've just written, run:
sudo python3 2.2.10_read.py