RFID RC522 Authentication failed while writing tag

Hello Community,

I am reaching out seeking assistance with a challenge I am encountering while attempting to write data onto an RFID tag using NodeMCU8266. Despite multiple attempts, I consistently encounter an "Authentication failed!" error message.

Below, I've provided the code I'm currently using:

#include <ESP8266WiFi.h>
#include <SPI.h>
#include <MFRC522.h>

#define WIFI_SSID "DIGITALUX"
#define WIFI_PASSWORD "OfficeNet22"

#define RST_PIN D3
#define SS_PIN D8

MFRC522 mfrc522(SS_PIN, RST_PIN);


/*///////////////////////////////////////////////////////////////////////////////////////////////////
                                      Write Function!
///////////////////////////////////////////////////////////////////////////////////////////////////*/

void writeData(byte* buffer, byte block, const char* prompt) {
  Serial.println(prompt);
  byte len = Serial.readBytesUntil('#', (char *)buffer, 16);
  for (byte i = len; i < 16; i++) buffer[i] = ' ';

  MFRC522::MIFARE_Key key;
  for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
  
  if (mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)) == MFRC522::STATUS_OK) {
    if (mfrc522.MIFARE_Write(block, buffer, 16) == MFRC522::STATUS_OK) {
      Serial.println("Data written successfully");
    } else {
      Serial.println("Failed to write data");
    }
  } else {
    Serial.println("Authentication failed");
  }
}

/*///////////////////////////////////////////////////////////////////////////////////////////////////
                                      setup Function!
///////////////////////////////////////////////////////////////////////////////////////////////////*/

void setup() {
  Serial.begin(115200);
  SPI.begin();
  mfrc522.PCD_Init();

  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.println("Connecting");
  while (!WiFi.isConnected()) {
    Serial.print(".");
    delay(50);
  }
  Serial.println();
  delay(500);
  Serial.print("Connected with IP : ");
  Serial.println(WiFi.localIP());
}

/*///////////////////////////////////////////////////////////////////////////////////////////////////
                                      Loop Function!
///////////////////////////////////////////////////////////////////////////////////////////////////*/

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

  Serial.print(F("Card 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();

  byte buffer[18];
  byte block;
  MFRC522::StatusCode status;
  Serial.setTimeout(20000L);

  // Write user data to RFID card
  writeData(buffer, 1, "Enter Your Name: ");

  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
}

Here is the output I got every time.

1234

I've tried different cards, but every time I get the same error. I'm not sure if there is a problem in my code because I've carefully checked all my hardware connections multiple times.

Thank you very much in advance for your time and assistance.

Best regards,
Ali

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.