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

From Wiki
Jump to: navigation, search
(Get MFRC522-python library)
(Setup SPI:)
 
(30 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
=='''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.<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'll explore the reading and writing functionalities of RFID.<br>
 +
 +
[[File:Mfrc522 pic.png]]<br>
 +
 +
=='''Schematic Diagram'''==
 +
[[File:Rc522 schematic.png]]<br>
 +
 +
=='''Wiring'''==
 +
[[File:Rc522 wiring.png|800px]]<br>
 +
 
=='''Setup SPI:'''==
 
=='''Setup SPI:'''==
Run raspi-config
+
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:
 +
 
 
<pre>sudo raspi-config</pre>
 
<pre>sudo raspi-config</pre>
Go through:
+
 
'''8 Advanced Options -> A6 SPI->Yes'''
+
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>
Then press Enter three times to enable SPI as default settings
+
 
Select finish and reboot
+
=='''Install Spidev and MFRC522'''==
=='''Install python-dev and 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>sudo apt-get update
+
 
sudo apt-get upgrade
+
Update <code>spidev</code> on your Raspberry Pi using the following command:
sudo apt-get install python-dev git
+
 
</pre>
+
<pre>sudo pip3 install --upgrade spidev</pre>
=='''Wiring:'''==
+
 
{| border="1" class="wikitable" style="text-align: center;"
+
Next, install the <code>MFRC522</code> library:
|-
+
 
!scope=col | Name
+
<pre>sudo pip3 install mfrc522</pre>
!scope=col |Pin#
+
 
!scope=col |Pin Name
+
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.
|-
+
 
|VCC
+
=='''Run the code'''==
|1
+
Start by downloading the code:
|3V3
+
 
|-
+
<pre>cd ~/
|RST
+
wget https://raw.githubusercontent.com/sunfounder/raphael-kit/master/python/2.2.10_write.py</pre>
|22
+
 
|GPIO25
+
<pre>cd ~/
|-
+
wget https://raw.githubusercontent.com/sunfounder/raphael-kit/master/python/2.2.10_read.py</pre>
|GND
+
 
|Any
+
Execute the following to write a message to the card:
|Any Ground
+
 
|-
+
<pre>sudo python3 2.2.10_write.py</pre>
|MISO
+
 
|21
+
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.
|GPIO9
+
 
|-
+
To read the data from the card or tag you've just written, run:
|MOSI
+
 
|19
+
<pre>sudo python3 2.2.10_read.py</pre>
|GPIO10
+
 
|-
+
=='''Resource'''==
|SCK
+
[http://wiki.sunfounder.cc/images/c/c6/MFRC522_datasheet.pdf MFRC522 datasheet][[File:PDF.jpg]]
|23
+
|GPIO11
+
|-
+
|NSS
+
|24
+
|GPIO8
+
|-
+
|IRQ
+
|None
+
|None
+
|}
+
=='''Install SPI-Py'''==
+
<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:'''==
+
<pre># To read card
+
sudo python Read.py
+
# To write card
+
sudo python Read.py
+
</pre>
+

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