RFID reader stops working when NO and COM are wired

Hello,

I have a strange problem and i hope you can help me.
I am building an RFID card reader that opens a solenoid lock with an arduino nano ESP32.

The code is working perfectly when i don't plug the solenoid on the 5V relay NO and COM.
However as soon as i plug the wire on the NO and COM my RFID reader stops working after 1 to 3 reads (depends)

Do you have an idea what is causing the issue ? i was thinking of electromagnetic interference of the solenoid..

Below you will find my wiring and my code
I tried to simplify the code to just reading the correct NUID and i have the same behaviour. So i think this is an hardware issue.
my power source is DC 12V/1A for the solenoid and USB-C cable plug on a phone charger for the arduino Nano

I also read those different topics and tried the different response without success :
RFID reader stops working as soon as relay is engaged

important note for the wiring, i am not using the same 5v relay as in picture but this one :
Capture d'écran 2024-05-18 193357
also i use an ARDUINO nano esp32 so relay is plug on the VBUS and not 5V

#include <SPI.h>
#include <MFRC522.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>

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

MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance
char ssid[] = "******";
char password[] = "******";
const char* mqtt_server = "*******";
const char* mqtt_topic = "******";
const char* mqtt_user = "******";
const char* mqtt_password = "****";
const char* mqtt_client = "******";
const char* token = "Electronics-Hub";

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);

  Serial.begin(9600);             // Initialize serial communications with the PC
  SPI.begin();                    // Init SPI bus
  mfrc522.PCD_Init();             // Init MFRC522
  delay(4);                       // Optional delay. Some board do need more time after init to be ready, see Readme
  mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
  Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));

  connectToWiFiAndMQTT();
}

void loop() {
  client.loop();

  // Authenticate with the card using the default key
  MFRC522::MIFARE_Key key;
  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  }

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

  byte block = 2;
  MFRC522::StatusCode status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
  if (status != MFRC522::STATUS_OK) {
    Serial.println("Authentication failed.");
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }

  byte buffer[18];
  byte bufferSize = sizeof(buffer);
  status = mfrc522.MIFARE_Read(block, buffer, &bufferSize);
  if (status != MFRC522::STATUS_OK) {
    Serial.println("Reading failed.");
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }

  String data = "";
  for (byte i = 0; i < bufferSize - 3; i++) {
    data += (char)buffer[i];
  }

  Serial.print("Data from sector 0, block 2: ");
  Serial.println(data);

  if ((data == token &&
       mfrc522.uid.size == 4 &&
       mfrc522.uid.uidByte[0] == 0x83 &&
       mfrc522.uid.uidByte[1] == 0x77 &&
       mfrc522.uid.uidByte[2] == 0xB1 &&
       mfrc522.uid.uidByte[3] == 0x07) || data == "Electronics-Hub") {
    digitalWrite(RELAY_PIN, HIGH);
    Serial.println("OPEN!!");
    delay(3000);
    digitalWrite(RELAY_PIN, LOW);
  } else {
    Serial.print("wrong card");
  }

  mfrc522.PICC_HaltA();           // Halt PICC (card)
  mfrc522.PCD_StopCrypto1();      // Stop encryption on PCD (reader)
  delay(500);
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.println("Message received on topic: " + String(topic));

  DynamicJsonDocument jsonDoc(1024);  // Adjust the size based on your JSON payload
  deserializeJson(jsonDoc, payload, length);

  const char* id = jsonDoc["id"];
  const char* command = jsonDoc["command"];
  const char* newData = jsonDoc["data"];

  if (strcmp(id, "RfidCasier") == 0 && strcmp(command, "SOLVED") == 0) {
    digitalWrite(RELAY_PIN, HIGH);
    Serial.println("OPEN!!");
    delay(3000);
    digitalWrite(RELAY_PIN, LOW);
  }

  if (strcmp(id, "RfidCasier") == 0 && strcmp(command, "STATUS") == 0) {
    client.publish(mqtt_topic, "{\"id\":\"RFIDCASIER2\",\"state\":\"ONLINE\"}");
  }

  if (strcmp(id, "RfidWriter") == 0 && strcmp(command, "UPDATE") == 0) {
    token = newData;
  }
}

void connectToWiFiAndMQTT() {
  // Connect to WiFi
  int attempts = 0;
  while (attempts < 10 && WiFi.status() != WL_CONNECTED) {
    WiFi.begin(ssid, password);
    Serial.println("Connecting to WiFi...");
    delay(1000);
    attempts++;
  }

  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("Connected to WiFi");

    // Connect to MQTT broker
    attempts = 0;
    client.setServer(mqtt_server, 1883);
    client.setCallback(callback);
    while (attempts < 10 && !client.connected()) {
      if (client.connect(mqtt_client, mqtt_user, mqtt_password)) {
        client.subscribe(mqtt_topic);
        Serial.println("Connected to MQTT broker");
      } else {
        Serial.println("Failed to connect to MQTT broker. Trying again in 5 seconds...");
        delay(5000);
        attempts++;
      }
    }
  } else {
    Serial.println("Failed to connect to WiFi after 10 attempts. Giving up.");
  }
}

thank you for your help

  • First off the solenoid should have a reversed biased kickback diode across its coil.

Do you think this is the reason ?
Should I follow the diagram you gave here :
Flyback diode required for relay board and 12v solenoid?

you are very active by the way :smiley: thanks for helping all of us

Just a little nit of a nag. Your schematic does not reflect the components that you describe. You use an ESP32 but the schematic shows a Nano. The relay module in the schematics is totally different from the relay module in the image.

It might not be that relevant for this project but correct schematics are important.

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