Difference between revisions of "How to Use an RFID RC522 on Raspberry Pi"

From Wiki
Jump to: navigation, search
(Install Spidev and MFRC522)
Line 21: Line 21:
 
<pre>sudo raspi-config</pre>
 
<pre>sudo raspi-config</pre>
  
Use the arrow keys on the keyboard to select <3 Interfacing options> -> <Yes> -> <OK> to complete the setup of the SPI.<br>
+
Use the arrow keys on the keyboard to select <code>3 Interfacing options</code> -> <code>Yes</code> -> <code>OK</code> to complete the setup of the SPI.<br>
  
  
 
=='''Install Spidev and MFRC522'''==
 
=='''Install Spidev and MFRC522'''==
  
The spidev library helps handle interactions with the SPI and is a key component to this tutorial as we need it for the Raspberry Pi to interact with the RFID RC522.
+
The <code>spidev</code> library helps handle interactions with the SPI and is a key component to this tutorial as we need it for the Raspberry Pi to interact with the RFID RC522.
  
Run the following command to upgrade '''spidev''' to your Raspberry Pi via '''pip'''.
+
Run the following command to upgrade <code>spidev</code> to your Raspberry Pi via <code>pip3</code>.
  
 
<pre>sudo pip3 install --upgrade spidev</pre>
 
<pre>sudo pip3 install --upgrade spidev</pre>
  
Continue to install the MFRC522 library.
+
Continue to install the <code>MFRC522</code> library.
  
 
<pre>sudo pip3 install mfrc522</pre>
 
<pre>sudo pip3 install mfrc522</pre>
  
The MFRC522 library contains two files: MFRC522.py and SimpleMFRC522.py.
+
The MFRC522 library contains two files: <code>MFRC522.py</code> and <code>SimpleMFRC522.py</code>.
  
Among them MFRC522.py is the realization of RFID RC522 interface, this library handles all the heavy work of communicating with RFID through Pi’s SPI interface.
+
Among them <code>MFRC522.py</code> is the realization of RFID RC522 interface, this library handles all the heavy work of communicating with RFID through Pi’s SPI interface.
  
SimpleMFRC522.py takes the MFRC522.py file and greatly simplifies it by allowing you to deal with only a few functions instead of a few functions.
+
<code>SimpleMFRC522.py</code> takes the <code>MFRC522.py</code> file and greatly simplifies it by allowing you to deal with only a few functions instead of a few functions.
  
 
=='''Run the code'''==
 
=='''Run the code'''==
Line 57: Line 57:
  
  
After running 2.2.10_write.py. You need to write a message first, press Enter to confirm, then put the card on the MFRC522 module, wait for “Data writing is complete” to appear and take the card away, or rewrite the message to another card and exit by Ctrl+C.
+
After running <code>2.2.10_write.py</code>. You need to write a message first, press Enter to confirm, then put the card on the MFRC522 module, wait for “Data writing is complete” to appear and take the card away, or rewrite the message to another card and exit by Ctrl+C.
  
Now run 2.2.10_read.py to read the information of the tag or card you have written.
+
Now run <code>2.2.10_read.py</code> to read the information of the tag or card you have written, then exit by Ctrl+C..
  
 
<pre>sudo python3 2.2.10_read.py</pre>
 
<pre>sudo python3 2.2.10_read.py</pre>

Revision as of 11:01, 11 October 2023

Introduction

Radio Frequency Identification (RFID) refers to technologies that use wireless communication between an object (or tag) and interrogating device (or reader) to automatically track and identify such objects.

Some of the most common applications for this technology include retail supply chains, military supply chains, automated payment methods, baggage tracking and management, document tracking and pharmaceutical management, to name a few.

In this project, we will use RFID for reading and writing.

Mfrc522 pic.png

Schematic Diagram

Rc522 schematic.png

Wiring

Rc522 wiring.png

Setup SPI:

Enable the SPI port of your Raspberry Pi (If you have enabled it, skip this; if you do not know whether you have done that or not, please continue).

sudo raspi-config

Use the arrow keys on the keyboard to select 3 Interfacing options -> Yes -> OK to complete the setup of the SPI.


Install Spidev and MFRC522

The spidev library helps handle interactions with the SPI and is a key component to this tutorial as we need it for the Raspberry Pi to interact with the RFID RC522.

Run the following command to upgrade spidev to your Raspberry Pi via pip3.

sudo pip3 install --upgrade spidev

Continue to install the MFRC522 library.

sudo pip3 install mfrc522

The MFRC522 library contains two files: MFRC522.py and SimpleMFRC522.py.

Among them MFRC522.py is the realization of RFID RC522 interface, this library handles all the heavy work of communicating with RFID through Pi’s SPI interface.

SimpleMFRC522.py takes the MFRC522.py file and greatly simplifies it by allowing you to deal with only a few functions instead of a few functions.

Run the code

Download 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

Run the Code

sudo python3 2.2.10_write.py


After running 2.2.10_write.py. You need to write a message first, press Enter to confirm, then put the card on the MFRC522 module, wait for “Data writing is complete” to appear and take the card away, or rewrite the message to another card and exit by Ctrl+C.

Now run 2.2.10_read.py to read the information of the tag or card you have written, then exit by Ctrl+C..

sudo python3 2.2.10_read.py

Resource

MFRC522 datasheetPDF.jpg