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 :

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
