Manufactura industrial
Internet industrial de las cosas | Materiales industriales | Mantenimiento y reparación de equipos | Programación industrial |
home  MfgRobots >> Manufactura industrial >  >> Manufacturing Technology >> Tecnología Industrial

Módulo RFID RC522: Lectura/Escritura Tarjetas y Llaveros con Arduino

El MFRC522 (RC522) es el módulo RFID de 13,56 MHz más popular para Arduino. Lee/escribe tarjetas y llaveros MIFARE Classic 1K / 4K (ISO 14443A). Comunicación: SPI. Alimentación: 3,3 V (¡no 5 V en VCC!). Corriente: 13–26 mA. Alcance lectura: 0–5 cm. Pines: VCC, RST, GND, MISO, MOSI, SCK, SDA (SS). Incluye antena PCB integrada. Ideal para control acceso, registro asistencia, identificación usuarios, taquillas, pagos simples, proyectos IoT. Librería estándar: MFRC522 por GithubCommunity (miguelbalboa).

Pinout RC522 y Conexión Arduino Uno

RC522Arduino UnoFunción
VCC3,3VAlimentación 3,3 V (¡NO 5V!)
RSTD9Reset (configurable)
GNDGNDTierra
MISOD12SPI MISO
MOSID11SPI MOSI
SCKD13SPI SCK
SDA (SS)D10SPI SS (Slave Select)

Arduino Mega: MISO=50, MOSI=51, SCK=52, SS=53. ESP8266: MISO=D6, MOSI=D7, SCK=D5, SS=D8. ESP32: MISO=19, MOSI=23, SCK=18, SS=5 (configurables).

Librería MFRC522

Instala desde Gestor de Librerías: MFRC522 (by GithubCommunity). Incluye ejemplos completos.

Código Básico: Leer UID de Tarjeta


#include 
#include 

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);

void setup() {
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  Serial.println("Acerca tarjeta/llavero al lector...");
}

void loop() {
  // Buscar nueva tarjeta
  if (!mfrc522.PICC_IsNewCardPresent()) return;
  // Leer UID
  if (!mfrc522.PICC_ReadCardSerial()) return;

  Serial.print("UID: ");
  for (byte i = 0; i < mfrc522.uid.size; i++) {
    Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(mfrc522.uid.uidByte[i], HEX);
  }
  Serial.println();

  mfrc522.PICC_HaltA(); // Detener comunicación
  mfrc522.PCD_StopCrypto1();
  delay(1000);
}

Estructura de Memoria MIFARE Classic 1K

Autenticación y Lectura/Escritura de Bloque


#define BLOCK 4 // Bloque de datos (ej. sector 1, bloque 0)
byte keyA[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // Clave por defecto

void readBlock(byte block) {
  MFRC522::MIFARE_Key key;
  for (byte i = 0; i < 6; i++) key.keyByte[i] = keyA[i];

  if (mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)) != MFRC522::STATUS_OK) {
    Serial.println("Fallo autenticación");
    return;
  }
  byte buffer[18];
  byte size = 18;
  if (mfrc522.MIFARE_Read(block, buffer, &size) == MFRC522::STATUS_OK) {
    Serial.print("Bloque "); Serial.print(block); Serial.print(": ");
    for (byte i = 0; i < 16; i++) {
      Serial.print((char)buffer[i]);
    }
    Serial.println();
  } else Serial.println("Fallo lectura");
  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
}

void writeBlock(byte block, byte data[16]) {
  MFRC522::MIFARE_Key key;
  for (byte i = 0; i < 6; i++) key.keyByte[i] = keyA[i];

  if (mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)) != MFRC522::STATUS_OK) {
    Serial.println("Fallo autenticación escritura");
    return;
  }
  if (mfrc522.MIFARE_Write(block, data, 16) == MFRC522::STATUS_OK) {
    Serial.println("Escritura OK");
  } else Serial.println("Fallo escritura");
  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
}

Ejemplo: Escribir "Hola RFID" en Bloque 4


void setup() {
  // ... init como antes ...
  byte data[16] = {0};
  strcpy((char*)data, "Hola RFID      "); // 16 bytes exactos
  writeBlock(4, data);
  readBlock(4);
}

Consideraciones de Seguridad

Proyecto: Control Acceso Simple con Relé


#include 
#include 
#define SS_PIN 10
#define RST_PIN 9
#define RELAY_PIN 7 // Active LOW
MFRC522 mfrc522(SS_PIN, RST_PIN);

// UID autorizado (ejemplo: 4 bytes)
byte authorizedUID[4] = {0xA1, 0xB2, 0xC3, 0xD4};

void setup() {
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, HIGH); // OFF
  Serial.println("Sistema listo. Acerca tarjeta.");
}

void loop() {
  if (!mfrc522.PICC_IsNewCardPresent()) return;
  if (!mfrc522.PICC_ReadCardSerial()) return;

  bool authorized = true;
  if (mfrc522.uid.size != 4) authorized = false;
  else {
    for (byte i = 0; i < 4; i++) {
      if (mfrc522.uid.uidByte[i] != authorizedUID[i]) { authorized = false; break; }
    }
  }

  if (authorized) {
    Serial.println("ACCESO CONCEDIDO");
    digitalWrite(RELAY_PIN, LOW); // Abre puerta (relé ON)
    delay(3000);
    digitalWrite(RELAY_PIN, HIGH); // Cierra
  } else {
    Serial.println("ACCESO DENEGADO");
    // Buzzer, LED rojo, log...
  }
  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
  delay(1000);
}

Conclusión

El RC522 es puerta de entrada al RFID con Arduino: barato, SPI, librería madura, soporta lectura/escritura MIFARE Classic. Claves: alimentar 3,3 V, cableado SPI correcto, autenticación por sector, claves por defecto 0xFF. Para seguridad real, migra a DESFire/NTAG424. ¡Construye tu control de acceso!

Tecnología Industrial

  1. Construye una cerradura RFID con Arduino – Domina la tecnología RFID paso a paso
  2. Guía Experta para Crear Moldes de Fundición en Arena para Fundiciones y Proyectos DIY
  3. Litofanías: Crea imágenes en 3D y elige el filamento adecuado
  4. Construir un proceso de gestión del cambio efectivo: Un modelo probado
  5. Electrónica DIY simplificada: Tu guía completa para construir proyectos con confianza
  6. Guía DIY: Construye tu propio controlador de motor DC
  7. Garantizando el Cumplimiento REACH en Electrónica: Guía Práctica
  8. Construye tus Propias Luces LED: Guía Completa Paso a Paso para DIY
  9. Luz de Cultivo LED DIY: Guía Definitiva Paso a Paso para Invernadero Casero Exitoso
  10. NC State bate récord: chip RFID se reduce a 125µm × 245µm, reduciendo costos e impulsando seguridad de cadena de suministro
  11. Tecnología RFID Explicada: Cómo Funciona la Identificación por Radiofrecuencia y Usos Clave