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

From Wiki
Jump to: navigation, search
(Resource)
(Setup SPI:)
 
(14 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
=='''Introduction'''==
 
=='''Introduction'''==
The MFRC522 is a highly integrated reader/writer IC for contactless communication at 13.56 MHz. The MFRC522 reader supports ISO/IEC 14443 A/MIFARE mode.<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>
The MFRC522’s internal transmitter is able to drive a reader/writer antenna designed to communicate with ISO/IEC 14443 A/MIFARE cards and transponders without additional active circuitry. The receiver module provides a robust and efficient implementation for demodulating and decoding signals from ISO/IEC 14443 A/MIFARE compatible cards and transponders. The digital module manages the complete ISO/IEC 14443 A framing and error detection (parity and CRC) functionality.<br>
+
 
[[File:RFID0127.png]]<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>
=='''Enable Device Tree'''==
+
 
Edit /boot/config.txt
+
In this project, we'll explore the reading and writing functionalities of RFID.<br>
<pre>sudo nano /boot/config.txt</pre>
+
 
Add the line below to the bottom of config.txt
+
[[File:Mfrc522 pic.png]]<br>
<pre>device_tree=on</pre>
+
 
Hit Ctrl + O to save, and Ctrl + X to exit.
+
=='''Schematic Diagram'''==
 +
[[File:Rc522 schematic.png]]<br>
 +
 
 +
=='''Wiring'''==
 +
[[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:
  
Run raspi-config
 
 
<pre>sudo raspi-config</pre>
 
<pre>sudo raspi-config</pre>
Go through:
 
'''9 Advanced Options -> A5 SPI->Yes'''<br>
 
Then press Enter three times to enable SPI as default settings
 
Select finish and reboot
 
  
=='''Install python-dev and git:'''==
+
Navigate using the arrow keys to <code>3 Interfacing options</code> -> <code>SPI</code> <code>Yes</code> -> <code>OK</code> to complete the SPI setup.<br>
<pre>sudo apt-get update
+
 
sudo apt-get upgrade
+
=='''Install Spidev and MFRC522'''==
sudo apt-get install python-dev git
+
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.
</pre>
+
 
=='''Wiring:'''==
+
Update <code>spidev</code> on your Raspberry Pi using the following command:
<center>
+
 
{| border="1" class="wikitable" style="text-align: center;"
+
<pre>sudo pip3 install --upgrade spidev</pre>
|-
+
 
!scope=col | Name
+
Next, install the <code>MFRC522</code> library:
!scope=col |Pin#
+
 
!scope=col |Pin Name
+
<pre>sudo pip3 install mfrc522</pre>
|-
+
 
|VCC
+
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.
|1
+
 
|3V3
+
=='''Run the code'''==
|-
+
Start by downloading the code:
|RST
+
 
|22
+
<pre>cd ~/
|GPIO25
+
wget https://raw.githubusercontent.com/sunfounder/raphael-kit/master/python/2.2.10_write.py</pre>
|-
+
 
|GND
+
<pre>cd ~/
|Any
+
wget https://raw.githubusercontent.com/sunfounder/raphael-kit/master/python/2.2.10_read.py</pre>
|Any Ground
+
 
|-
+
Execute the following to write a message to the card:
|MISO
+
 
|21
+
<pre>sudo python3 2.2.10_write.py</pre>
|GPIO9
+
 
|-
+
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.
|MOSI
+
 
|19
+
To read the data from the card or tag you've just written, run:
|GPIO10
+
|-
+
|SCK
+
|23
+
|GPIO11
+
|-
+
|NSS
+
|24
+
|GPIO8
+
|-
+
|IRQ
+
|None
+
|None
+
|}
+
</center>
+
  
=='''Install SPI-Py'''==
+
<pre>sudo python3 2.2.10_read.py</pre>
<pre>cd ~
+
git clone https://github.com/lthiery/SPI-Py.git
+
cd SPI-Py/
+
sudo python setup.py install
+
</pre>
+
=='''Get MFRC522-python library'''==
+
<pre>cd ~
+
git clone https://github.com/mxgxw/MFRC522-python.git
+
cd MFRC522-python/
+
</pre>
+
=='''Now Run:'''==
+
To read card
+
sudo python Read.py
+
To write card
+
sudo python Write.py
+
  
 
=='''Resource'''==
 
=='''Resource'''==
 
[http://wiki.sunfounder.cc/images/c/c6/MFRC522_datasheet.pdf MFRC522 datasheet][[File:PDF.jpg]]
 
[http://wiki.sunfounder.cc/images/c/c6/MFRC522_datasheet.pdf MFRC522 datasheet][[File:PDF.jpg]]

Latest revision as of 01:38, 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 -> SPI 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