
Como funciona
O script contém o próprio pequeno driver de MFRC522: uma classe que configura os registradores do leitor e depois usa os comandos Transceive e MFAuthent para conversar com uma etiqueta MIFARE Classic 1K virtual. O ESP32-C6 acessa o leitor por SPI de hardware (SCK G6, MOSI G7, MISO G2), com chip select no G20. O pino RST do leitor está ligado ao 3V3, então nunca mantém o chip em reset. O G1 aciona um LED verde e o G0 um vermelho, cada um com 330 Ω.
A cada 300 ms, o script envia uma requisição REQA. Quando uma etiqueta responde, ele executa a anticolisão para obter o UID de 4 bytes e seleciona o cartão. Se o UID for 9c5a1db3, ele autentica o bloco 1 com a chave padrão, seis bytes 0xFF, lê os 16 bytes desse bloco, imprime ACCESS GRANTED com o UID e os dados e acende o verde. Qualquer outro UID imprime ACCESS DENIED e acende o vermelho. Depois ele envia HALT, então uma etiqueta deixada no leitor só é informada uma vez.
O que observar
Serial imprime MFRC522 version 0x92 e depois pede que você aproxime uma etiqueta do leitor. Selecione o leitor e ative Hold tag to reader: Serial imprime ACCESS GRANTED 9c5a1db3 b'BREADBOARD DOOR ' e o LED verde acende. Esse texto é o que o bloco 1 da etiqueta guarda. Desative e ative a opção de novo para fazer outra leitura.
O que está na bancada
- 2× LED
- 2× Resistor (Resistor)
- ESP32-C6
- Leitor RFID (MFRC522) · RFID reader (MFRC522)
Como está ligado
- ESP32-C6 · 3v3→Leitor RFID (MFRC522) · RFID reader (MFRC522) · vcc
- ESP32-C6 · gnd2→Leitor RFID (MFRC522) · RFID reader (MFRC522) · gnd
- ESP32-C6 · 3v3b→Leitor RFID (MFRC522) · RFID reader (MFRC522) · rst
- ESP32-C6 · g6→Leitor RFID (MFRC522) · RFID reader (MFRC522) · sck
- ESP32-C6 · g7→Leitor RFID (MFRC522) · RFID reader (MFRC522) · mosi
- ESP32-C6 · g2→Leitor RFID (MFRC522) · RFID reader (MFRC522) · miso
- ESP32-C6 · g20→Leitor RFID (MFRC522) · RFID reader (MFRC522) · cs
- ESP32-C6 · g1→Resistor (Resistor) · a
- Resistor (Resistor) · b→LED · a
- LED · k→ESP32-C6 · gnd
- ESP32-C6 · g0→Resistor (Resistor) · a
- Resistor (Resistor) · b→LED · a
- LED · k→ESP32-C6 · gnd
O código
Este script MicroPython roda na placa emulada a cada inicialização. Edite-o em “Code”.
from machine import Pin, SPI import time class MFRC522: OK, ERR = 0, 2 def __init__(self, spi, cs): self.spi, self.cs = spi, cs self.cs.value(1) self._wreg(0x01, 0x0F) # soft reset for r, v in ((0x2A, 0x8D), (0x2B, 0x3E), (0x2D, 30), (0x2C, 0), (0x15, 0x40), (0x11, 0x3D)): self._wreg(r, v) self._wreg(0x14, self._rreg(0x14) | 0x03) # antenna on def _wreg(self, reg, val): self.cs.value(0) self.spi.write(bytes([(reg << 1) & 0x7E, val])) self.cs.value(1) def _rreg(self, reg): self.cs.value(0) self.spi.write(bytes([((reg << 1) & 0x7E) | 0x80])) val = self.spi.read(1, 0)[0] self.cs.value(1) return val def _crc(self, data): self._wreg(0x05, 0x04) self._wreg(0x0A, 0x80) for b in data: self._wreg(0x09, b) self._wreg(0x01, 0x03) # CalcCRC return [self._rreg(0x22), self._rreg(0x21)] def _tocard(self, cmd, send, bits=0): self._wreg(0x04, 0x7F) # clear irqs self._wreg(0x0A, 0x80) # flush FIFO self._wreg(0x0D, bits) for b in send: self._wreg(0x09, b) self._wreg(0x01, cmd) if cmd == 0x0C: self._wreg(0x0D, 0x80 | bits) # StartSend wait = 0x10 if cmd == 0x0E else 0x30 for _ in range(20): n = self._rreg(0x04) if n & 0x01: return self.ERR, [] # timer: nothing answered if n & wait: break else: return self.ERR, [] return self.OK, [self._rreg(0x09) for _ in range(self._rreg(0x0A))] def request(self): return self._tocard(0x0C, [0x26], 7)[0] def anticoll(self): stat, r = self._tocard(0x0C, [0x93, 0x20]) return r[:4] if stat == self.OK and len(r) == 5 else None def select(self, uid): buf = [0x93, 0x70] + uid + [uid[0] ^ uid[1] ^ uid[2] ^ uid[3]] return self._tocard(0x0C, buf + self._crc(buf))[0] def auth(self, block, key, uid): return self._tocard(0x0E, [0x60, block] + key + uid)[0] def read(self, block): buf = [0x30, block] stat, r = self._tocard(0x0C, buf + self._crc(buf)) return bytes(r[:16]) if stat == self.OK and len(r) >= 16 else None def halt(self): buf = [0x50, 0x00] self._tocard(0x0C, buf + self._crc(buf)) self._wreg(0x08, self._rreg(0x08) & 0xF7) # stop crypto1 # C6 hardware SPI (GPSPI2) fixed pins: sck=6, mosi=7, miso=2. rdr = MFRC522(SPI(1, baudrate=1000000), Pin(20, Pin.OUT)) print('MFRC522 version 0x%02x' % rdr._rreg(0x37)) green = Pin(1, Pin.OUT, value=0) red = Pin(0, Pin.OUT, value=0) KEY = [0xFF] * 6 AUTHORIZED = '9c5a1db3' print('Hold a tag to the reader (toggle in its Inspector)...') while True: if rdr.request() == rdr.OK: uid = rdr.anticoll() if uid and rdr.select(uid) == rdr.OK: s = ''.join('%02x' % b for b in uid) if s == AUTHORIZED and rdr.auth(1, KEY, uid) == rdr.OK: note = rdr.read(1) print('ACCESS GRANTED', s, note) green.value(1) red.value(0) else: print('ACCESS DENIED', s) red.value(1) green.value(0) rdr.halt() # one report per tap; lift + re-hold to scan again time.sleep_ms(300)
Experimente
- No Inspector, mude o UID da etiqueta para outro valor e aproxime a etiqueta de novo. Serial imprime ACCESS DENIED com o novo UID e o LED vermelho acende.
- Mantenha o UID autorizado e mude o conteúdo do bloco 1 no Inspector. A próxima linha ACCESS GRANTED mostra os seus novos dados.
- Na condição de autorização, troque apenas s == AUTHORIZED por s in ('9c5a1db3', '01020304'). Mantenha a verificação rdr.auth(1, KEY, uid) == rdr.OK. Clique em Run on board, digite o UID 01 02 03 04 no Inspector e apresente a etiqueta. Qualquer um dos UIDs autorizados acende o LED verde depois da autenticação.



