My rfid won't read a card

so i have an mfrc522 rfid scanner, i connected it to an arduino mega, i checked the connections and they are fine. when ever i use code that is not from the examples it doesn't work, the red led is on, the power is connected to 3.3 volts on the arduino and there are no errors. it just won't detect the card. it works fin on examples though.

Hi,
Post the sketch that works, the sketch that doesn't work and the wiring diagram of your project.
When posting the sketches, place them between tags </>

RV mineirin

Your post was MOVED to a more suitable location.

Could you also review How to get the best out of this forum as it will help you find your way around.

here is the working code

/*
* Initial Author: ryand1011 (https://github.com/ryand1011)
*
* Reads data written by a program such as "rfid_write_personal_data.ino"
*
* See: https://github.com/miguelbalboa/rfid/tree/master/examples/rfid_write_personal_data
*
* Uses MIFARE RFID card using RFID-RC522 reader
* Uses MFRC522 - Library
* -----------------------------------------------------------------------------------------
*             MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
*             Reader/PCD   Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
* Signal      Pin          Pin           Pin       Pin        Pin              Pin
* -----------------------------------------------------------------------------------------
* RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
* SPI SS      SDA(SS)      10            53        D10        10               10
* SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
* SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
* SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
*/

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

#define RST_PIN         9           // Configurable, see typical pin layout above
#define SS_PIN          10          // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance

//*****************************************************************************************//
void setup() {
 Serial.begin(9600);                                           // Initialize serial communications with the PC
 SPI.begin();                                                  // Init SPI bus
 mfrc522.PCD_Init();                                              // Init MFRC522 card
 Serial.println(F("Read personal data on a MIFARE PICC:"));    //shows in serial that it is ready to read
}

//*****************************************************************************************//
void loop() {

 // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
 MFRC522::MIFARE_Key key;
 for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;

 //some variables we need
 byte block;
 byte len;
 MFRC522::StatusCode status;

 //-------------------------------------------

 // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
 if ( ! mfrc522.PICC_IsNewCardPresent()) {
   return;
 }

 // Select one of the cards
 if ( ! mfrc522.PICC_ReadCardSerial()) {
   return;
 }

 Serial.println(F("**Card Detected:**"));

 //-------------------------------------------

 mfrc522.PICC_DumpDetailsToSerial(&(mfrc522.uid)); //dump some details about the card

 //mfrc522.PICC_DumpToSerial(&(mfrc522.uid));      //uncomment this to see all blocks in hex

 //-------------------------------------------

 Serial.print(F("Name: "));

 byte buffer1[18];

 block = 4;
 len = 18;

 //------------------------------------------- GET FIRST NAME
 status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 4, &key, &(mfrc522.uid)); //line 834 of MFRC522.cpp file
 if (status != MFRC522::STATUS_OK) {
   Serial.print(F("Authentication failed: "));
   Serial.println(mfrc522.GetStatusCodeName(status));
   return;
 }

 status = mfrc522.MIFARE_Read(block, buffer1, &len);
 if (status != MFRC522::STATUS_OK) {
   Serial.print(F("Reading failed: "));
   Serial.println(mfrc522.GetStatusCodeName(status));
   return;
 }

 //PRINT FIRST NAME
 for (uint8_t i = 0; i < 16; i++)
 {
   if (buffer1[i] != 32)
   {
     Serial.write(buffer1[i]);
   }
 }
 Serial.print(" ");

 //---------------------------------------- GET LAST NAME

 byte buffer2[18];
 block = 1;

 status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 1, &key, &(mfrc522.uid)); //line 834
 if (status != MFRC522::STATUS_OK) {
   Serial.print(F("Authentication failed: "));
   Serial.println(mfrc522.GetStatusCodeName(status));
   return;
 }

 status = mfrc522.MIFARE_Read(block, buffer2, &len);
 if (status != MFRC522::STATUS_OK) {
   Serial.print(F("Reading failed: "));
   Serial.println(mfrc522.GetStatusCodeName(status));
   return;
 }

 //PRINT LAST NAME
 for (uint8_t i = 0; i < 16; i++) {
   Serial.write(buffer2[i] );
 }


 //----------------------------------------

 Serial.println(F("\n**End Reading**\n"));

 delay(1000); //change value if you want to read cards faster

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

and here is the non working code



#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
 
#define SS_PIN 10
#define RST_PIN 9
#define LED_G 5 //define green LED pin
#define LED_R 4 //define red LED
#define BUZZER 2 //buzzer pin
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
Servo myServo; //define servo name
 
void setup() 
{
  Serial.begin(9600);   // Initiate a serial communication
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  myServo.attach(3); //servo pin
  myServo.write(0); //servo start position
  pinMode(LED_G, OUTPUT);
  pinMode(LED_R, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  noTone(BUZZER);
  Serial.println("Put your card to the reader...");
  Serial.println();

}
void loop() 
{
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  //Show UID on serial monitor
  Serial.print("UID tag :");
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();
  if (content.substring(1) == "8D 20 06 85") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("Authorized access");
    Serial.println();
    delay(500);
    digitalWrite(LED_G, HIGH);
    tone(BUZZER, 500);
    delay(300);
    noTone(BUZZER);
    myServo.write(180);
    delay(5000);
    myServo.write(0);
    digitalWrite(LED_G, LOW);
  }
 
 else   {
    Serial.println(" Access denied");
    digitalWrite(LED_R, HIGH);
    tone(BUZZER, 300);
    delay(1000);
    digitalWrite(LED_R, LOW);
    noTone(BUZZER);
  }
}

how about adding serial-output to see what is going on?

global defined variables of type String eat up all memory over time and will cause hard to find crashes

You should use something like PString or SafeString

Another approach start new from the working code adding only a few lines of code re-compile and test
best regards Stefan

should i mention i don't really understand C++ i just command c command v into the IDE. i am a noob that just bought an elegoo kit

Most newcomers add a lot of code before they start to test.
You should do it exactly opposite. Add just a few lines of code and test again

best regards Stefan

thanks for the help!

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