from machine import Pin, SPI
from mfrc522 import MFRC522
import time
# SPI configuration for Raspberry Pi Pico
# SCK=18, MOSI=19, MISO=16
spi = SPI(0,
baudrate=100000,
polarity=0,
phase=0,
sck=Pin(2),
mosi=Pin(3),
miso=Pin(4))
# RST and CS (SDA) pins
RST_PIN = 0
CS_PIN = 5
print("---------------------------------------")
print("Initialize the reader")
print("---------------------------------------")
rfid = MFRC522(spi, RST_PIN, CS_PIN, 2)
print("---------------------------------------")
print("System ready! Bring a tag close...")
print("---------------------------------------")
while True:
# 1. Look for a card
(stat, tag_type) = rfid.request(rfid.PICC_REQIDL)
if stat == rfid.OK:
# If a card is present, attempt to read its UID
(stat, uid) = rfid.anticoll()
if stat == rfid.OK:
# Convert the byte list to a Hexadecimal string
#uid_hex = "{:02X}{:02X}{:02X}{:02X}".format(uid[0], uid[1], uid[2], uid[3])
# uid is a list of bytes: [12, 45, 120, 10]
# Convert it to a clean hex string
print("---------------------------------------")
uid_hex = ":".join(["{:02X}".format(x) for x in uid])
print("Card detected!")
print("Unique ID (HEX) :", uid_hex)
print("Unique ID (DEC) :", uid)
print("---------------------------------------")
# Pause to avoid reading the same card multiple times
time.sleep(1.5)
else:
# If detected but UID cannot be read
print("---------------------------------------")
print("UID read error (Anticollision), anticollstat:", stat)
print("---------------------------------------")
else:
# If detected but UID cannot be read
print("---------------------------------------")
print("No Card detected")
print("---------------------------------------")
time.sleep(5)
Loading
pi-pico-2w
pi-pico-2w
Loading
mfrc522
mfrc522