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=1, ###################### instead of 0
sck=Pin(18),
mosi=Pin(19),
miso=Pin(16))
# RST and CS (SDA) pins
RST_PIN = 0
CS_PIN = 17
# Initialize the reader
rfid = MFRC522(spi, RST_PIN, CS_PIN)
print("System ready! Bring a badge 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()
print(stat)
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
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("UID read error (Anticollision)")
time.sleep(0.1)