The mfrc522-python library is used to interact with RFID readers that use the MFRC522 chip interfaced with a Raspberry Pi.
Project description
MFRC522-python
The mfrc522-python library is used to interact with RFID readers that use the MFRC522 chip interfaced with a Raspberry Pi.
Table of Contents
Introduction
MFRC522
The MFRC522 is a popular RFID module that supports contactless communication using the 13.56 MHz frequency. It can read and write data to RFID cards or tags, making it ideal for projects that require identification or access control. This repository provides a library and example code to interact with the MFRC522 module using various microcontroller platforms.
Sectors and Blocks
Sectors and Blocks refer to the organization and structure of memory on an RFID tag or card.
The memory of card is divided into 16 sectors (for classic MIFARE 1k). Each sector contains 4 blocks and each block can hold upto 16 bytes of data.
So a classic MIFARE 1k has 16 sectors, 64 blocks (16 sectors x 4 blocks) and can hold upto 1024 bytes (64 blocks x 16 bytes) in total.
Not Every blocks are writeable, the first block of the first sector and the last block of every sectors are not writeable.
The first block of first sector typically holds the manufaturer's details (like manufacturer's identification number (MID) and the application identifier (AID). The MID is a unique number that identifies the manufacturer of the card. The AID is a unique number that identifies the application that is stored on the card.). The last block of every sector is called a trailer block, the trailer block contains information about the sector, such as the sector number, the number of blocks in the sector, and the checksum. The checksum is used to verify the integrity of the data in the sector. If you ever write to a trailer block then the entire sector will be unwritable.
Sectors | Block Number |
---|---|
0 | 0, 1, 2, 3 |
1 | 4, 5, 6, 7 |
2 | 8, 9, 10, 11 |
3 | 12, 13, 14, 15 |
4 | 16, 17, 18, 19 |
5 | 20, 21, 22, 23 |
6 | 24, 25, 26, 27 |
7 | 28, 29, 30, 31 |
8 | 32, 33, 34, 35 |
9 | 36, 37, 38, 39 |
10 | 40, 41, 42, 43 |
11 | 44, 45, 46, 47 |
12 | 48, 49, 50, 51 |
13 | 52, 53, 54, 55 |
14 | 56, 57, 58, 59 |
15 | 60, 61, 62, 63 |
Manfacturer's details is stored in block number 0 and
Trailer blocks are 3, 7, 11, etc,.
( Tips: It's better to not store any data in the first sector. Start from the first sector )
Note: Different cards have diiferent number of sectors and blocks. For example, Classic MIFARE 4k has 40 Sectors. It's better to check the datasheet for the RFID tag/card you use before writing data to it.
Installation
1. From PyPI (Stable)
pip install mfrc522-python
2. From Github ( Beta/dev version)
pip install git+https://github.com/1AdityaX/mfrc522-python
Connections
RF522 Module | Raspberry Pi |
---|---|
SDA | Pin 24 / GPIO8 (CE0) |
SCK | Pin 23 / GPIO11 (SCKL) |
MOSI | Pin 19 / GPIO10 (MOSI) |
MISO | Pin 21 / GPIO9 (MISO) |
IRQ | – |
GND | GND |
RST | Pin 22 / GPIO25 |
3.3V | 3.3V |
Usage
Using MFRC522
class
- Import and create an instance of class
MFRC522
frommfrc522
module.
from mfrc522 import MFRC522
reader = MFRC522()
- Request communication with a PICC (Proximity Integrated Circuit Card A.K.A rfid card) and check if the communication is established.
status = None
while status != reader.MI_OK:
(status, TagType) = reader.Request(reader.PICC_REQIDL)
if status == reader.MI_OK:
print("Connection Success!")
- Perform an Anti-collision Algorithm (Picks one rfid card if multiple cards are placed ) and return the UID.
(status, uid) = reader.Anticoll()
if status == reader.MI_OK:
print(uid)
- Select the tag to Authenticate and perform funtions like reading or writing data to the card.
reader.SelectTag(uid)
- Authenticate a particular sector with a key to read or write data to it.
trailer_block = 11
#This is the default key for MIFARE Cards
key = [0xFF, 0xFF, 0xFF , 0xFF, 0xFF, 0xFF]
status = reader.Authenticate(
reader.PICC_AUTHENT1A, trailer_block , key, uid)
- Now read or write to the block numbers in the sector that you have authenticated.
To read
block_nums = [8, 9, 10]
data = []
for block_num in block_nums:
block_data = reader.ReadTag(block_num)
if block_data:
data += block_data
if data:
print(''.join(chr(i) for i in data))
To write
block_nums = [8, 9, 10]
text = "Hello, World"
data = bytearray()
data.extend(bytearray(text.ljust( len(block_addrs) * 16).encode('ascii')))
i = 0
for block_num in block_addrs:
reader.WriteTag(block_num, data[(i*16):(i+1)*16])
i += 1
- Once you business with the RFID card or Tag is over. Always Stop the Authenciation/communiction with the card. Note: If you miss out this step, you won't be able to use a different card.
reader.StopAuth()
Using SimpleMFRC522
class
- Import and create an instance of class
SimpleMFRC522
frommfrc522
module
from mfrc522 import SimpleMFRC522
reader = SimpleMFRC522()
- Use
read()
to read andwrite()
to write To read
id, text = reader.read()
print(f"ID: {id}")
print(f"Text: {text}")
To write
text = "hello_world"
id, text_written = reader.write(text)
print(f"ID: {id}")
print(f"Text Written: {text_written}")
mfrc522.SimpleMFRC522()
Methods
__init__()
Initializes a SimpleMFRC522
instance. It sets up the MFRC522 module, defines the default authentication key, sets the trailer block number to 11, and initializes the BasicMFRC522 module.
read()
Reads data from the RFID tag.
- Returns: A tuple containing the tag ID (as an integer) and the data read (as a string).
read_id()
Reads the tag ID from the RFID tag.
- Returns: The tag ID as an integer.
write(text)
Writes the given text to an RFID tag.
- Args:
text
(str): A string to be written to the RFID tag.
- Returns: A tuple containing the ID of the tag and the text that was written to the tag.
Using BasicMFRC522
class
- Import and create an instance of the class
BasicMFRC522
frommfrc522
module
from mfrc522 import BasicMFRC522
reader = BasicMFRC522()
- Use any methods.
mfrc522.BasicMFRC522
Methods
__init__(KEY=[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
Initializes a BasicMFRC522
instance.
- Args:
KEY
(list): The authentication key used for reading and writing data. The default key is[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
.
read_sector(trailer_block=11)
Reads data from a sector of the RFID tag.
- Args:
trailer_block
(int): The block number of the sector trailer.
- Returns:
tuple
: A tuple containing the tag ID (as an integer) and the data read (as a string).
read_sectors(trailer_blocks=[11])
Reds data from multiple sectors of the RFID tag.
- Args:
trailer_blocks
(list): The list of block numbers of the sector trailers.
- Returns:
tuple
: A tuple containing the tag ID (as an integer) and the concatenated data read from all sectors (as a string).
read_id()
Reads the tag ID from the RFID tag.
- Returns:
int
: The tag ID as an integer.
read_id_no_block()
Attempts to read the tag ID from the RFID tag without blocking.
- Returns:
int
: The tag ID as an integer, orNone
if the operation fails.
read_no_block(trailer_block)
Attempts to read data from the RFID tag without blocking.
- Args:
trailer_block
(int): The block number of the sector trailer.block_addr
(tuple): The block numbers of the data blocks to read.
- Returns:
tuple
: A tuple containing the tag ID (as an integer) and the data read (as a string), or(None, None)
if the operation fails.
write_sector(text, trailer_block=11)
Writes data to a sector of the RFID tag.
- Args:
text
(str): The data to write.trailer_block
(int): The block number of the sector trailer.
- Returns:
tuple
: A tuple containing the tag ID (as an integer) and the data written (as a string).
write_sectors(text, trailer_blocks=[11])
Writes data to multiple sectors of the RFID tag.
- Args:
text
(str): The data to write.trailer_blocks
(list): The list of block numbers of the sector trailers.
- Returns:
tuple
: A tuple containing the tag ID (as an integer) and the concatenated data written to all sectors (as a string).
write_no_block(text, trailer_block)
Attempts to write data to the RFID tag without blocking.
- Args:
text
(str): The data to write.trailer_block
(int): The block number of the sector trailer.block_addr
(tuple): The block numbers.
clear_sector(trailer_block=11)
Clears a sector of the RFID tag by writing empty data to all blocks.
- Args:
trailer_block
(int): The block number of the sector trailer.
- Returns:
tuple
: A tuple containing the tag ID (as an integer) and the cleared data (as an empty string).
clear_sectors(trailer_blocks=[11])
Clears multiple sectors of the RFID tag by writing empty data to all blocks.
- Args:
trailer_blocks
(list): The list of block numbers of the sector trailers.
- Returns:
tuple
: A tuple containing the tag ID (as an integer) and the concatenated cleared data (as an empty string).
clear_no_block(trailer_block)
Attempts to clear a sector of the RFID tag without blocking.
- Args:
trailer_block
(int): The block number of the sector trailer.block_addr
(tuple): The block numbers of the data blocks to clear.
- Returns:
tuple
: A tuple containing the tag ID (as an integer) and the cleared data (as an empty string), or(None, None)
if the operation fails.
Note: Clearing a sector will permanently erase the data stored in the blocks of that sector. Use with caution as this operation cannot be undone.
Example Code
Using MFRC522
class
read.py
To read a particular sector and convert the bytes to plain string/text.
from mfrc522 import MFRC522
reader = MFRC522()
def read(trailer_block, key, block_addrs):
(status, TagType) = reader.Request(reader.PICC_REQIDL)
if status != reader.MI_OK:
return None, None
(status, uid) = reader.Anticoll()
if status != reader.MI_OK:
return None, None
id = uid
reader.SelectTag(uid)
status = reader.Authenticate(
reader.PICC_AUTHENT1A, trailer_block , key, uid)
data = []
text_read = ''
if status == reader.MI_OK:
for block_num in block_addrs:
block = reader.ReadTag(block_num)
if block:
data += block
if data:
text_read = ''.join(chr(i) for i in data)
reader.StopAuth()
return id, text_read
trailer_block = 11
key = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
block_addrs = [8,9,10]
id, text = read(trailer_block, key, block_addrs)
while not id:
id, text = read(trailer_block, key, block_addrs)
print(id)
print(text)
write.py
To write a particular sector.
from mfrc522 import MFRC522
reader = MFRC522()
def write(trailer_block, key, block_addrs, text):
(status, TagType) = reader.Request(reader.PICC_REQIDL)
if status != reader.MI_OK:
return None, None
(status, uid) = reader.Anticoll()
if status != reader.MI_OK:
return None, None
reader.SelectTag(uid)
status = reader.Authenticate(
reader.PICC_AUTHENT1A, trailer_block, key, uid)
reader.ReadTag(trailer_block)
if status == reader.MI_OK:
data = bytearray()
data.extend(bytearray(text.ljust(
len(block_addrs) * 16).encode('ascii')))
i = 0
for block_num in block_addrs:
reader.WriteTag(block_num, data[(i*16):(i+1)*16])
i += 1
reader.StopAuth()
return uid, text[0:(len(block_addrs) * 16)]
trailer_block = 11
block_addrs = [8, 9, 10]
key = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
text = "some random text"
uid, text_in = write(trailer_block, key, block_addrs, text)
while not uid:
uid, text_in = write(trailer_block, key, block_addrs, text)
print(uid)
print(text_in)
Using SimpleMFRC522
class
read.py
from mfrc522 import SimpleMFRC522
reader = SimpleMFRC522()
id, text = reader.read()
print(f"ID: {id}")
print(f"Text: {text}")
write.py
from mfrc522 import SimpleMFRC522
writer = SimpleMFRC522()
text = "Hello, world"
id, text_written = writer.write(text)
print(f"ID: {id}")
print(f"Text Written: {text_written}")
Using BasicMFRC522
class
read_sector.py To read a particular sector
from mfrc522 import BasicMFRC522
reader = BasicMFRC522()
sector = 15
id, text = reader.read_sector(sector)
print(f"ID: {id}")
print(f"Text: {text}")
read_sectors.py To read multiple sectors
from mfrc522 import BasicMFRC522
reader = BasicMFRC522()
sectors = [11, 15]
id, text = reader.read_sectors(sectors)
print(f"ID: {id}")
print(f"Text: {text}")
write_sector.py To write a particular sector
from mfrc522 import BasicMFRC522
writer = BasicMFRC522()
sector = 15
text = "Example to use write_sector method"
id, text_written = writer.write_sector(text, sector)
print(f"ID: {id}")
print(f"Text Written: {text_written}")
write_sectors.py To write multiple sectors
from mfrc522 import BasicMFRC522
writer = BasicMFRC522()
sector = [11, 15]
text = "Example to use write_sector method"
id, text_written = writer.write_sectors(text, sector)
print(f"ID: {id}")
print(f"Text Written: {text_written}")
clear_sector.py To clear data written in a sector
from mfrc522 import BasicMFRC522
writer = BasicMFRC522()
sector = 11
id = writer.clear_sector(sector)
print("Cleared")
clear_sectors.py To clear data written in multiple sectors
from mfrc522 import BasicMFRC522
writer = BasicMFRC522()
sectors = [11, 15]
id = writer.clear_sectors(sectors)
print("Cleared")
Contributions
Contributions to the mfrc522-python
library are welcome. To contribute, follow these steps:
- Fork the repository on GitHub.
- Create a new branch for your changes.
- Make your changes and commit them.
- Push your changes to your forked repository.
- Submit a pull request to the main repository.
That's it! By submitting a pull request, you can contribute your changes to the mfrc522-python
library.
Provide a clear description of your changes in the pull request.
If you have any questions or need further assistance, feel free to open an issue.
License
This project is licensed under the GNU General Public License (GPL) version 3.0. You can find the full text of the license in the LICENSE file.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file mfrc522-python-0.0.7.tar.gz
.
File metadata
- Download URL: mfrc522-python-0.0.7.tar.gz
- Upload date:
- Size: 30.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 293758c4f6344a6c0f72fb0dbda72e3a003eeb8810cedefce97d26826ab20904 |
|
MD5 | 840cbc388ea155c8e3d9bc6dde52ed6d |
|
BLAKE2b-256 | e2637a5ba63fcceea25f8d2a4101a648f97dfc4466b0615b78bd17977218abce |
File details
Details for the file mfrc522_python-0.0.7-py3-none-any.whl
.
File metadata
- Download URL: mfrc522_python-0.0.7-py3-none-any.whl
- Upload date:
- Size: 27.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 027d92127daf88cbf7ad4b3cd994b1d5e0e77f764e0c91c20154fc1d2f6aa413 |
|
MD5 | ff2762a7548bb7942936df8b9fd225d3 |
|
BLAKE2b-256 | 9e010b5b40c9f5626d8438e9ff09794b3b6839de4aa2a7b4cd4cefdc85e3148e |