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

From Wiki
Jump to: navigation, search
(Run the code)
Line 1: Line 1:
 
=='''Introduction'''==
 
=='''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.<br>
+
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.<br>
  
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.<br>
+
This technology is widely used in applications such as retail and military supply chains, automated payments, baggage and document tracking, pharmaceutical management, among others.<br>
  
In this project, we will use RFID for reading and writing.<br>
+
In this project, we'll explore the reading and writing functionalities of RFID.<br>
  
 
[[File:Mfrc522 pic.png]]<br>
 
[[File:Mfrc522 pic.png]]<br>
Line 12: Line 12:
  
 
=='''Wiring'''==
 
=='''Wiring'''==
 
 
[[File:Rc522 wiring.png|800px]]<br>
 
[[File:Rc522 wiring.png|800px]]<br>
  
 
=='''Setup SPI:'''==
 
=='''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:
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).
+
  
 
<pre>sudo raspi-config</pre>
 
<pre>sudo raspi-config</pre>
  
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>
+
Navigate using the arrow keys to <code>3 Interfacing options</code> -> <code>Yes</code> -> <code>OK</code> to complete the SPI setup.<br>
 
+
  
 
=='''Install Spidev and MFRC522'''==
 
=='''Install Spidev and MFRC522'''==
 +
The <code>spidev</code> 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.
  
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.
+
Update <code>spidev</code> on your Raspberry Pi using the following command:
 
+
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 <code>MFRC522</code> library.
+
Next, install the <code>MFRC522</code> library:
  
 
<pre>sudo pip3 install mfrc522</pre>
 
<pre>sudo pip3 install mfrc522</pre>
  
The MFRC522 library contains two files: <code>MFRC522.py</code> and <code>SimpleMFRC522.py</code>.
+
The MFRC522 library consists of two primary files: <code>MFRC522.py</code> and <code>SimpleMFRC522.py</code>. The former handles the core interaction with the RFID RC522 through the Pi’s SPI interface. In contrast, <code>SimpleMFRC522.py</code> streamlines this by providing a more straightforward set of functions.
 
+
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.
+
 
+
<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'''==
 
+
Start by downloading the code:
Download the code.
+
  
 
<pre>cd ~/
 
<pre>cd ~/
Line 52: Line 43:
 
wget https://raw.githubusercontent.com/sunfounder/raphael-kit/master/python/2.2.10_read.py</pre>
 
wget https://raw.githubusercontent.com/sunfounder/raphael-kit/master/python/2.2.10_read.py</pre>
  
Run the code to write message to the card.
+
Execute the following to write a message to the card:
  
 
<pre>sudo python3 2.2.10_write.py</pre>
 
<pre>sudo python3 2.2.10_write.py</pre>
  
 +
After launching <code>2.2.10_write.py</code>, 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.
  
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.
+
To read the data from the card or tag you've just written, run:
 
+
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 01:37, 12 October 2023

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.

Mfrc522 pic.png

Schematic Diagram

Rc522 schematic.png

Wiring

Rc522 wiring.png

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

Resource

MFRC522 datasheetPDF.jpg