RC522 stopped reading cards after several attempts

Hi, i'm currently working on a project which involves the use of RC522, esp8266,relay and a solenoid lock.
There are two problems that i have been facing now.

1 - The RC522 stops reading the cards after several reads and will only work again if i reset or reupload the sketch. The number of attempts is random but it ranges from 2-7 times.

2 - My projects connects to the firebase realtime database. Wierd thing is that it keeps saying that the connection refuses but sometimes it works.

Below is my sketch

void setup () {

  Serial.begin(115200);
  SPI.begin();
  mfrc522.PCD_Init();
  delay(10);
  
  WiFi.begin(WIFI_SSID,WIFI_PASS);
  Serial.println();
  Serial.print("Connecting");
  while(WiFi.status() != WL_CONNECTED){
    Serial.print(".");
    delay(700);
  }

  delay(1000);
  Serial.println();
  Serial.print("Connected!");
  Serial.println();
  Serial.println(WiFi.localIP());

  Firebase.begin(FIREBASE_HOST , FIREBASE_AUTH);
  Firebase.reconnectWiFi(true);
  Firebase.setMaxRetry(firebaseData, 3);

  pinMode(RELAY,OUTPUT);
  digitalWrite(RELAY,LOW);
  Serial.println("Put your card to the reader...");
}

void loop (){
   if (mfrc522.PICC_IsNewCardPresent()) 
    {
      if (mfrc522.PICC_ReadCardSerial()) 
      {
        //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(Firebase.getString(firebaseData, "village1/V1b-L1-24")){
          cardNum = firebaseData.stringData();
          temp = cardNum;
        } else {
          cardNum = temp;
          Serial.println("Firebase: " + firebaseData.errorReason());
        }
        if (content.substring(1) == cardNum) //change here the UID of the card/cards that you want to give access
        {
          Serial.println("Authorized access");
          Serial.println();
          if(digitalRead(RELAY) == LOW){
            digitalWrite(RELAY, HIGH);
          }
          else if(digitalRead(RELAY) == HIGH){
            digitalWrite(RELAY, LOW);
          }
          delay(ACCESS_DELAY);
        }
        else{
          Serial.println(" Access denied");
          delay(DENIED_DELAY);
        }
      }
    }
  
}

SDA - D8
SCK - D5
MOSI - D7
MISO - D6
GND - GND
RST - D3
3.3v - 3.3V

The firebase problem is quite inconsistent. Tried it like 6 hours ago and it kept getting connection refuse many times. Just tried it now and it didnt happen at all. For the RFID, tried it probably less than a hour ago and it stopped working after 3-6 attempts. Now it stopped working 1-2 times.

do you have a schematic? Part numbers or link for everything would help.. including the solenoid.

wolframore:
do you have a schematic? Part numbers or link for everything would help.. including the solenoid.

Sorry i dont. Quite new to arduino so i dont really know how to create one.

Here is the numbers

[RFID] - [NodeMCU]
SDA - D8[GPIO15]
SCK - D5[GPIO14]
MOSI - D7[GPIO13]
MISO - D6[GPIO12]
GND - GND
RST - D3[GPIO0]
3.3v - 3.3V

[Relay] - [NodeMCU]
IN - D1[GPIO5]
GND - GND
VCC - VIN

Using Normally open[ Power source i used Power Adapter 12v to supply to the solenoid lock 12v]

Im using the firebase esp8266 by mobizt

Right now I'm concerned about your solenoid... can you just scribble a picture of the connections? It's hard to comment on something I can't see or picture. Some use fritzing and others use circuit drawing software like kicad or similar... I have also seen hand scribbled diagrams.

Sounds like it could be a wiring issue but can't be sure yet.

Some folks don't like fritzing but anything is better than nothing... here's an example of correctly wired solenoid done using fritzing:

here's a simplified circuit diagram:

see how this can help wiring correctly?

@wolframore

Here you go.. first time using fritzing so looks quite bad. The power source is actually a 12v power adapter but i couldnt find a drawing for it. I couldnt find a RFID labelled picture so you can see my previous comment for the connection from rfid to nodemcu.

op's diagram:

how is your modemcu powered?

Do you have 5V relays? If so are you switching them at 3.3V? and powering them using the 3.3V?

wolframore:
how is your modemcu powered?

Do you have 5V relays? If so are you switching them at 3.3V? and powering them using the 3.3V?

wolframore:
how is your modemcu powered?

Do you have 5V relays? If so are you switching them at 3.3V? and powering them using the 3.3V?

Forgot to mention. The relay in the diagram is supposed to be a single channel.. i couldn't find a single channel part so i used the dual channel instead. The nodemcu is connected to my pc so i can see the serial monitor. The relay is powered by the nodemcu.

I see a "String" defined in at least one place! Generally fatal to an Arduino! :astonished:

"Some Most folks don't like fritzing but anything is better than nothing.."

I would rather see a hand-drawn schematic on a napkin than a pretty Fritzing picture.

OP- I have the same exact issue, and the exact same hardware, except I am driving a 3V servo.

I actually have two copies of the project, one on a breadboard with jumpers, and one on a soldered breadboard. The soldered version does exhibit the same issue, but much, much less often.

From what I can see it appears that the reader just stops reading cards.

This can't be your whole sketch- what libraries are you using, and where do you initiate the mfrc522 class?

Paul__B:
I see a "String" defined in at least one place! Generally fatal to an Arduino! :astonished:

Can you please elaborate more? I did see a post saying big s string is bad and suggested to use small s string for as an array of chars. I did try small s string but it didnt recognize it.

SteveMann:
"Some Most folks don't like fritzing but anything is better than nothing.."

I would rather see a hand-drawn schematic on a napkin than a pretty Fritzing picture.

OP- I have the same exact issue, and the exact same hardware, except I am driving a 3V servo.

I actually have two copies of the project, one on a breadboard with jumpers, and one on a soldered breadboard. The soldered version does exhibit the same issue, but much, much less often.

From what I can see it appears that the reader just stops reading cards.

This can't be your whole sketch- what libraries are you using, and where do you initiate the mfrc522 class?

Ahhh sorry about that.. here you go.

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

#define WIFI_SSID "**"
#define WIFI_PASS "**"

#define FIREBASE_HOST "**.firebaseio.com"
#define FIREBASE_AUTH "**"

#define SS_PIN 15
#define RST_PIN 0
#define RELAY 5 //relay pin
#define DENIED_DELAY 1000
#define ACCESS_DELAY 1500

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
FirebaseData firebaseData;
String cardNum = "";
String temp = "";

danialsaufi:
I did see a post saying big s string is bad and suggested to use small s string for as an array of chars. I did try small s string but it didn't recognize it.

That is indeed the point. Using the capital "S" String functions causes memory corruption, they are extremely difficult to use safely in systems - as Arduinos are - with limited memory.

It is not just a matter of changing a big "S" for a little one - that is nonsense. You have to write the code to use array strings with fixed allocation.

I note you just cited:

String cardNum = "";
String temp = "";

That exemplifies the problem. You have just defined two strings which are null - they have no allocation. That would not matter if you never altered them, but I gather that is anything but the case!

You propose to put not one, but a number of successive things into those strings, possibly of different sizes, and the "String" function will attempt to find places to put them even though no such space is allocated. When it runs out of places, your program stops.

Paul__B:
That is indeed the point. Using the capital "S" String functions causes memory corruption, they are extremely difficult to use safely in systems - as Arduinos are - with limited memory.

It is not just a matter of changing a big "S" for a little one - that is nonsense. You have to write the code to use array strings with fixed allocation.

I note you just cited:

String cardNum = "";

String temp = "";



That exemplifies the problem. You have just defined two strings which are null - they have **no** allocation. That would not matter if you never altered them, but I gather that is anything but the case!

You propose to put not one, but **a number** of successive things into those strings, possibly of different sizes, and the "String" function will attempt to find places to put them even though no such space is allocated. When it runs out of places, your program stops.

i see.. ill try to code it and see if there's any change to it.Thanks

@Paul__B , @SteveMann

So the first problem is fixed! :slight_smile: changing the strings to char worked.. Not sure how long and how many times before it hangs but i already scanned more than 15 times and it seems that it doesnt hang anymore.

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

#define WIFI_SSID "**"
#define WIFI_PASS "**"

#define FIREBASE_HOST "**.firebaseio.com"
#define FIREBASE_AUTH "*"

#define SS_PIN 15
#define RST_PIN 0
#define RELAY 5 //relay pin
#define DENIED_DELAY 1000
#define ACCESS_DELAY 1500

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
FirebaseData firebaseData;
char cardNum[] = "";

void setup () {

  Serial.begin(115200);
  SPI.begin();
  mfrc522.PCD_Init();
  delay(10);
  
  WiFi.begin(WIFI_SSID,WIFI_PASS);
  Serial.println();
  Serial.print("Connecting");
  while(WiFi.status() != WL_CONNECTED){
    Serial.print(".");
    delay(700);
  }

  delay(1000);
  Serial.println();
  Serial.print("Connected!");
  Serial.println();
  Serial.println(WiFi.localIP());

  Firebase.begin(FIREBASE_HOST , FIREBASE_AUTH);
  Firebase.reconnectWiFi(true);
  Firebase.setMaxRetry(firebaseData, 3);

  pinMode(RELAY,OUTPUT);
  digitalWrite(RELAY,LOW);
  Serial.println("Put your card to the reader...");
}

void loop (){
   if (mfrc522.PICC_IsNewCardPresent()) 
    {
      if (mfrc522.PICC_ReadCardSerial()) 
      {
        //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(Firebase.getString(firebaseData, "village1/V1b-L1-24")){
          firebaseData.stringData().toCharArray(cardNum,12);
        } else {
          Serial.println("Firebase: " + firebaseData.errorReason());
        }
        if (content.substring(1) == cardNum) //change here the UID of the card/cards that you want to give access
        {
          Serial.println("Authorized access");
          Serial.println();
          if(digitalRead(RELAY) == LOW){
            digitalWrite(RELAY, HIGH);
          }
          else if(digitalRead(RELAY) == HIGH){
            digitalWrite(RELAY, LOW);
          }
          delay(ACCESS_DELAY);
        }
        else{
          Serial.println(" Access denied");
          delay(DENIED_DELAY);
        }
      }
    }
  
}

However, the firebase is still having problems.. It either timeout or connection refused. sometimes it works, sometimes it timeout but most of the time the connection refuses. Any idea?

Sorry, absolutely no familiarity with "FirebaseESP8266" whatsoever. Don't even know what it is supposed to do! :roll_eyes: