www.joy-it.net
Pascalstr. 8 47506 Neukirchen-Vluyn
6.2 EXAMPLE APPLICATIONS
To use the RFID module you need the micropython-mfrc522 library. Please note that this
must be present on your Raspberry Pi in order to run the sample code. Now transfer the
following sample code to your Raspberry Pi Pico:
from mfrc522 import MFRC522
import utime
# initialize RFID object
reader = MFRC522(spi_id=0,sck=18,miso=16,mosi=19,cs=17,rst=0)
print("Hold a tag near the reader")
# define parameters for acessing tag
key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
PreviousCard = [0]
writeBlock = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff,
0x0b, 0x0c, 0x0d, 0x0e, 0x0f]
blockToWrite = 9
while True:
# start commuication with RFID reader
reader.init()
(stat, tag_type) = reader.request(reader.REQIDL)
# check if a tag can be read
if stat == reader.OK:
# read tag
(stat, uid) = reader.SelectTagSN()
# check if tag is the same as before
if uid == PreviousCard:
continue
# check if reading went well
if stat == reader.OK:
# print base information about tag
print("Card detected {} uid={}".format(hex(int.from_bytes(bytes
(uid),"little",False)).upper(),reader.tohexstring(uid)))
print()
# print all blocks of the tag
print("Read from tag:")
reader.MFRC522_DumpClassic1K(uid, Start=0, End=64, keyA=key)
print()
# check if tag can be written on
status = reader.auth(reader.AUTHENT1A, blockToWrite, key, uid)
if status == reader.OK:
# write on tag
status = reader.write(blockToWrite,writeBlock)
# check if writing was succesful
if status == reader.OK:
# print newly written block
print("Newly written block:")
reader.MFRC522_DumpClassic1K(uid,Start=9, End=10, keyA=key)
print()
else:
print("Unable to write")
else:
print("Authentication error for writing")
PreviousCard = uid
else:
# reset already read tag
PreviousCard=[0]
ume.sleep_ms(50)