4. RC522 RFID Module

RC522 is a RFID module working at 13.56MHz.

RPI Student card operates at 433MHz frequency, don’t treat your student card as Medical mouse!

4.1. Wiring and Configuring

RC522 relies on SPI protocol to transmit data.

On RPi, you need to enable SPI interface following: 1. execute sudo raspi-config in the terminal and locate to 5 Interfacing Options -> P4 SPI -> Yes 2. reboot reboot 3. install related python library

sudo pip3 install spidev mfrc522

Wiring: You need to wire up 7 wires using Dupond lines:

  1. SDA connects to Pin 24.

  2. SCK connects to Pin 23.

  3. MOSI connects to Pin 19.

  4. MISO connects to Pin 21.

  5. GND connects to Pin 6.

  6. RST connects to Pin 22.

  7. 3.3v connects to Pin 1.

IRQ is not connected.

4.2. Test Programs

Read ID from RFID Tag

import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522

reader = SimpleMFRC522()

try:
        id, text = reader.read()
        print(id)
        print(text)
finally:
        GPIO.cleanup()

Write ID to Tag

import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522

reader = SimpleMFRC522()

try:
        text = input('New data:')
        print("Now place your tag to write")
        reader.write(text)
        print("Written")
finally:
        GPIO.cleanup()

If you want to read/write arbitrary bytes from/to RFID tags, you should download the following python script and place it in your working folder

MFRC522_IOT.py

Reading and writing bytearray from/to RFID tags become

import RPi.GPIO as GPIO
from MFRC522_IOT import MFRC522_IOT
import time

reader = MFRC522_IOT()

try:
        while True:
                text = bytearray(b'RFID-TAG-013|')
                text.extend([0x01, 0x02, 0x03])

                print("Now place your tag to write")
                #reader.writebytes(text) # write bytes
                print("Written")
                time.sleep(1.0)

                print("place your tag for reading")
                id, text = reader.read()
                print("place your tag for reading again")
                id, textbytes = reader.readbytes() # read bytes
                print("id  :", id)
                print("text:", text)
                print("byte:", textbytes)
                time.sleep(1.0)
finally:
        GPIO.cleanup()