ESP8266 issues with swipe on RC522

I have a RC522 swipe reader connected to an ESP8266, which seems to be temperamental. I have the usual checks for card presence, and can monitor the detection of the swipes on the serial monitor, but can swipe once for on and then not get recognised for 10+ swipes. I can reboot the device and it seems to work instantly for a while, then can go back to not recognising.

I could post the code but it's 500+ lines. I am also performing reads on an ACS712 current detector every 100ms, setting 2 LEDs and controlling a 5V relay via a transistor - would this cause delays on the ESP8266 device polling the reader?

Am wondering if this is load related, specifically to how many devices are hooked to the ESP8266 and the current is not enough to the reader. How would I calculate this and measure it? I also know there is a keyword for this type of issue but can't find it by asking google.

Regards, Ian

Disconnect the LED and relay outputs from Esp.
Does it work now?
If not, try with some example (demo) code for your reader. If that works, you need to find the problem in your 500+ lines of code..

Either modularize your code or let some helpers help you make it smaller, because if we can't see and compile your code, we are operating blind.

@iandennison Another thing you can do is eliminate any code that is not involved with the probem thus making the code smaller.

Bad code.
Maybe it's a line 42- problem.

Or maybe 442?

Post it along with your annotated schematic, its not that big. I agree with @Railroader but change line 43 with line 42, that will make go the other way.

Addressing Your Issue:

You can spend weeks spinning your wheels, or you might get lucky and solve your problem quickly. To avoid unnecessary delays, it’s crucial to provide an annotated schematic of your circuit as you have it wired, showing all connections, including power, ground, and power supplies. I recommend it be in English, you can translate before posting if needed.

Why Detailed Information Matters:

Annotated Schematics: These are essential because they show exactly how your circuit is set up. Without them, it's difficult for anyone to understand what you’ve done, which makes troubleshooting nearly impossible. Frizzing diagrams or unclear pictures are not enough.

Technical Information: Many modules look similar and may even have the same name, but they can function differently. This is why we always ask for links to detailed technical information—not just sales pages like those on Amazon, which often lack the specifics we need.

Post your Software Without that we do not have a clue as to how it is expected to operate. Be sure to use code tags.

Show All Connections: It’s important to include every connection, especially power, ground and power sources in your schematic. Missing these details makes it hard to determine if a setup issue might be causing your problem.

My Process:

When I see a question, I spend a moment assessing it. If it’s missing critical information, I might ask for it. However, if it's repeatedly lacking important details, I may assume the questioner is not serious and move on to another query.

What You Need to Consider:

We don’t know your skill level or what resources you have available. If you’re missing key technical details or seem unprepared, it may indicate that you need to spend more time learning the basics before starting your project.

Providing the right information upfront will help you get the best possible assistance and avoid the frustration of running into dead ends. Let us help you by sharing what you have clearly and completely!

Try reading this: How to get the best out of this forum

Thanks for the feedback. I have created a cutdown version of the code and tested for the issues. I have progressively added in the functionality to see when the issue occurs. Fritzing diagram attached.

I get the "swipe ignore" issue in V1.2 of the code (checking the ACS712 sensor once a second) and V1.3 (enabling / disabling the relay every swipe). It is inconsistent though, I can power on the box and it will work approx 50% of the time.

I have a scaled down version that uses a 3.3v relay that does not appear to have an issue, am wondering if the load on the 5V power supply is too much. Appreciate any advice.

/* machine_box_cc_sandbox
Date: 1 Aug 2025
Author: Ian D
Version 1.2 - power on relay

History:
V1.0 - switch LEDs every swipe - all swipes detected OK
V1.1 - add in current ACS712 reading every second
V1.2 - power relay on when Green (ledStatus=1)
V1.3 - add in wifi join
V1.4 - report to web URL every 5 seconds
V1.5 - reset reader every 30 seconds
*/

/* Setup the various #includes
*/
#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <ACS712.h>

/* Global variables */
const int ledGreen = D2;
const int ledRed = D4;
const int relay = D1;
String cardUID="";
int ledStatus=0; // 0 = Red on, 1 = Green on
unsigned long ulSensorMarker = 0;   // Marker for every 1000 ms (1 second) that sensor needs to capture daya
float fltCurrentValue=0.0;    /* Value for present current load */
// Setup ACS712 - analog port 0
ACS712 sensor(ACS712_05B, A0);

#define RST_PIN         D3          // Configurable, see typical pin layout above
#define SS_PIN          D8         // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance
MFRC522::MIFARE_Key key;

void setup() {
  // Set LEDs off
  //initialise LEDs
  Serial.begin(115200);
  SPI.begin();      // Init SPI bus
  mfrc522.PCD_Init();         // Init MFRC522 card

  pinMode(ledRed, OUTPUT);
  pinMode(ledGreen, OUTPUT);

  digitalWrite(ledRed, LOW);
  digitalWrite(ledGreen, LOW);
  ulSensorMarker=millis();
  sensor.calibrate();

    //initialise Relay - sequence swapped
  digitalWrite(relay, LOW);
  pinMode(relay, OUTPUT);
  Serial.println("Setup complete for machine");
}

void loop() {
  // put your main code here, to run repeatedly:
  RfidScan();

  // Detect if Card UID change
  if (!(cardUID == ""))
  {
    Serial.print("Switching LED, status = ");
    Serial.print(ledStatus);
    Serial.println("");
    // SWitch LEDS
    if (ledStatus == 1)
    {
        // Set Red on, power off relay
        ledStatus=0;
        digitalWrite(ledRed, HIGH);
        digitalWrite(ledGreen, LOW);
        //open relay
        Serial.println("\nClosing Relay....");
        // digitalWrite(relay, LOW);

    } else
    {
      // Set Greenj on, power on relay
        ledStatus=1;
        digitalWrite(ledRed, LOW);
        digitalWrite(ledGreen, HIGH);
        //open relay
        Serial.println("\nOpening Relay....");
        // digitalWrite(relay, HIGH);
    }
    // Reset carduid
    cardUID="";
  }

  // Measure current every second
  if (millis()-ulSensorMarker > 1000)
  {
      // Run current sensor (temp, increase fltCurrentValue)
      // fltCurrentValue=fltCurrentValue+0.1;
      fltCurrentValue=sensor.getCurrentAC();
      ulSensorMarker=millis();
     // Serial.printf("=========================\n");
      Serial.printf("ACS712 input  - Time ");
      Serial.printf("%i", ulSensorMarker);
      Serial.printf(" - Current value ");
      Serial.printf("%3.2f", fltCurrentValue);
      Serial.printf("\n");
  }
}

void RfidScan(){
    // 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()) {
    // Serial.println("Card detected");
    return;
  }

  Serial.print(F("\nPICC type: "));
  MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  Serial.println(mfrc522.PICC_GetTypeName(piccType));

  // Check is the PICC of Classic MIFARE type
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&  
    piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
    piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    Serial.println(F("Your q                                                                                         / is not of type MIFARE Classic."));
    return;
  }
  
  Serial.println(F("**Card Detected:**"));
  //printHex(mfrc522.uid.uidByte, mfrc522.uid.size);

  //save UID value
  cardUID = String(mfrc522.uid.uidByte[0],HEX) + " " + String(mfrc522.uid.uidByte[1],HEX) + " " + String(mfrc522.uid.uidByte[2],HEX) + " " + String(mfrc522.uid.uidByte[3],HEX);
  //upper case string
  cardUID.toUpperCase();
  //print Card UID
  Serial.println("\nCard UID: " + cardUID);
    // Wait so card can be removed- 1 second
  delay (1000);
  
  // Halt PICC
  mfrc522.PICC_HaltA();

  // Stop encryption on PCD
  mfrc522.PCD_StopCrypto1();
}


Make sure that your actual bread board in that fritzing abomination, is the same as the one you are actually using. Some bread boards have a gap in the middle of the power rails run. These need bridging with a link to get power at both ends of the board.

That cries out loose connection to me.

Your wiring says you power it from 5V pin though.
So did you try how it behaves when relay is disconnected from arduino??
Also, you need voltage divider for the current sensor output (0-5V)

OK Thanks @ZX80 , the wifi clash was something I was not aware of. Handy to know, I am connecting this to a Wifi LAN to send the results to a central server.

@Grumpy_Mike it's actually soldered up IRL, (and I must have been away from school the day they taught "IC feng shui".)

Thanks all, will re-cable and amend code. No smoking gun for now but points for improvement.

:rofl:

Yes it is a bit of an advanced thing for a school to teach. It turns out that PCB layout engineer (that is the proper name for it) is a highly skilled and specialised skill. I have worked with some of the best in the field and been very impressed with their skills.

You say you use a 3.3V relay, is that the voltage rating of the coil? If so then it will draw more current than you can get from the 5V pin. As the working voltage goes down the amount of current to make it click (turn on) goes up. We normally quote the power needed to turn on a relay. That is in watts, which is the current multiplied by the voltage.