MFRC522 stops working when relay is connected

Hello everyone,

I’m new to arduino and this is my second project.
I wanted to use arduino to unlock a 12V Electric strike door lock by RFID cards and a push button. I’m using the RC522 module and a one channel jqc-3ff-s-z relay. Basically to unlock the door I’d have to scan the card or press the button which should trigger the relay and connect the lock to 12V.

For some reason the RC522 module stops detecting cards right after the first unlock or basically when the 12V load is connected to the other end (door lock). The push button works just fine and keeps unlocking the door.

I tested the relay with the COM and NO of the relay disconnected, and the RC522 module works just fine and keeps scanning. Once I connect the COM and NO, it stops working and then I’d have to re-plug the entire arduino for it work again.

In the beginning, I used an AC-DC12V 1A adaptor with the arduino and the VIN and GND pins of the arduino for the lock and It didn’t work: the door unlocks but the RFID reader stops working.

Then, I cut the adaptor and connected one end directly to the door lock, and back to the normal output socket and the other end to normal output socket and the COM of the relay then the other end of the lock to the NO of the relay. That didn’t work either.


(something like this, sorry for the crappy quality)

I also tried using separate power supplies for each of the arduino and the door lock (12V 1A for the lock, 12V 2A for the arduino) and one end of the power supply is controlled by the relay but that didn’t seem to work either.


(something like this)

I tried using the NC and COM (instead of NO and COM of the relay) and changed the code, but that didn’t seem to work either.

I don’t think it’s a problem with the code for the project because, the serial monitor of RC522 Library dumpinfo also stops once the NC and COM are connected to the 12V adaptor.

Code:

//RFID
#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
 
#define SS_PIN 10
#define RST_PIN 9
#define LED_G 4 //define green LED pin
#define LED_R 5 //define red LED
#define BUZZER 7 //buzzer pin
#define BUTTON 2 //BUTTON pin
#define RELAY 8 //RELAY pin
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() 
{
  Serial.begin(9600);   // Initiate a serial communication
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  pinMode(LED_G, OUTPUT);
  pinMode(LED_R, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  pinMode(RELAY, OUTPUT);// Define RELAY_PIN as OUTPUT for relay
  pinMode(BUTTON, INPUT_PULLUP);
  noTone(BUZZER);
  Serial.println("Put your card to the reader...");
  Serial.println();
  // initialize the LCD, 
  lcd.begin();
  // Turn on the blacklight and print a message.
  lcd.backlight();
  digitalWrite(RELAY,LOW);// Turn the relay OFF
}


void OpenDoor() {
  Serial.println("DOOR OPENED");
  digitalWrite(RELAY, HIGH);// Turn the relay ON
  digitalWrite(LED_G, HIGH);
  tone(BUZZER, 500);
  delay(300);
  noTone(BUZZER);
  delay(5000);
  lcd.clear();
  digitalWrite(RELAY,LOW);// Turn the relay OFF
  digitalWrite(LED_G, LOW);
  
}

void loop() 
{
  int pushButton = digitalRead(BUTTON);
  if(pushButton == LOW){
    Serial.println("BUTTON PUSHED");
    lcd.clear();
    lcd.print("Indoor Exit");
    OpenDoor();
  }
  // 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) == "B1 5B 3E 20" || content.substring(1) == "B9 C3 BC 6E")
  {
    Serial.println("Authorized access");
    lcd.clear();
    lcd.print("Access Granted");
    lcd.setCursor (0,1); // go to start of 2nd line
    if (content.substring(1) == "B1 5B 3E 20") { lcd.print("CARD 1"); }
    if (content.substring(1) == "B9 C3 BC 6E") { lcd.print("CARD 2"); }
    Serial.println();
    OpenDoor();
  } else   {
    Serial.println("Access denied");
    lcd.clear();
    lcd.print("Access Denied");
    digitalWrite(LED_R, HIGH);
    tone(BUZZER, 300);
    delay(2000);
    digitalWrite(LED_R, LOW);
    noTone(BUZZER);
  }
}

The code has LEDs, LCD, and a buzzer which are not used yet: I ordered them, but they haven’t arrived.

The wiring



(I’m using INPUT_PULLUP for the button)
Also, removing the button altogether didn’t change anything.

Bottom line, I can’t manage to use the relay with the RC522 module; the relay works fine on its own in a different sketch, and the module works fine on its own when the relay is not connected to the 12V.

I can’t seem to figure out how to fix this and I’ve looked everywhere.

Any advice?

I would like You to use a kick back diode across the magnet lock. The kick back occurs when the lock is deenergized and might trip the controller to get lost in the code. Other electronics might be affected as well. This disturbance gives unpredictable results.

Railroader:
I would like You to use a kick back diode across the magnet lock. The kick back occurs when the lock is deenergized and might trip the controller to get lost in the code. Other electronics might be affected as well. This disturbance gives unpredictable results.

I'm sorry I wrote electromagnetic for some reason. I'm actually using an electric strike door lock. Sorry again for the confusion.
This is exactly how it looks

Please post a link to it.

Could You post a proper wiring diagram? Those Fritzings are not for engineering and the first picture tells nothing.

Railroader:
Could You post a proper wiring diagram? Those Fritzings are not for engineering and the first picture tells nothing.

Sorry, I'm not quite understanding what you need. As I said, I'm new to these stuff. Would pictures of it in real life be helpful? or maybe could you show me an example of what you need?
Also, I tried the VIN and GND directly to lock and it works perfectly, so it has to do with the relay if that helps.

No, real pictures can't replace wiring diagrams.

"Tried the Vin and GND....." That's a wiring digram but of the wrong kind, Poetry, or Shakespeare type.
Often the things You don't tell about are important. The lack of this or that.
Use pen and paper, take a picture and post.
As long as I don't understand You get no bulls eye replies.

Railroader:
No, real pictures can't replace wiring diagrams.

"Tried the Vin and GND....." That's a wiring digram but of the wrong kind, Poetry, or Shakespeare type.
Often the things You don't tell about are important. The lack of this or that.
Use pen and paper, take a picture and post.
As long as I don't understand You get no bulls eye replies.

Will that help?

Make sure all pin names are readable. That's important. Just a position "somewhere" on the board is not clear.
Let's try.
You can't use and Arduino to handle that lock, as faar as I know. The Arduino handles a maximum of 20 milliAmpere.
Don't connect the relay to Vin. Connect it to the power supply. Those tine copper strips on the Arduino board are not strong enough.
Don't connect lock GND to the controller board. Connect it to the power supply.
Use the relay in-pin for operating the relay. Most likely the Arduino handles it.

Railroader:
Make sure all pin names are readable. That's important. Just a position "somewhere" on the board is not clear.
Let's try.
You can't use and Arduino to handle that lock, as faar as I know. The Arduino handles a maximum of 20 milliAmpere.
Don't connect the relay to Vin. Connect it to the power supply. Those tine copper strips on the Arduino board are not strong enough.
Don't connect lock GND to the controller board. Connect it to the power supply.
Use the relay in-pin for operating the relay. Most likely the Arduino handles it.

something like this?


Well, I did that before and It didn't work but, I tried it again though, and this time, I noticed something strange: when the upside plug (red circle in the picture) of the adaptor is connected to the live AC wire in the wall outlet. The RFID system works fine for like 3-4 unlocks and then stops working. I don't know if that was luck or what happened there, but I did it like 3 times and yeah it only worked if that part was connected to the Live wire.

Anyway, I tried that before and no it doesn't work even when I use different power supplies.

I still think You need a kick back diode across the lock. Do You have a link to the lock?

Re the situation about the polarity of the mains plug: From where does that USB power come? A Pc?
It looks like a ground, mains negative, problem. Is mains negative in connection with USB GND? Unplug ALL mains connections and check using a DVM set for continuity test.

OK, many matters of significance here.

Works when the electric strike is not connected but fails when it is? And is fussy about which way up you plug in the adaptor?

OK, the problem is that the impulse generated when the relay switches off current to the lock is just so dramatic that it is being radiated like a radio signal from the lock circuit back to the Arduino and crashing either the Arduino or the MFRC522. The solution for this part of the problem is twofold - you need to suppress that impulse with a diode across the strike solenoid - any common power diode will suffice, 1N4004 or such - and of course it must connect in the direction so that it does not conduct when you power the strike solenoid.

However there is the problem of this impulse actually getting from one part to another. You must keep the connections to the solenoid and its power supply, totally separate from the logic - and power - connections to the Arduino and its attachments such as the MFRC522.

Also absolutely critical is the matter of "lead dress" - keeping all wiring from device to device paired or grouped together in single bundles. Any open loops invite electromagnetic coupling from one part to another, and associating "active" wires with a ground also limits capacitive coupling of transients.

Finally you need to provide proper power supply to the Arduino and relay module. A very real danger is that the obsolete tutorials on the Arduino site and others misleadingly imply that the largely ornamental "barrel jack" and "Vin" connections to the on-board regulator allow a usable source of 5 V power. This is absolutely not the case. It is essentially only for demonstration use of the bare board back in the very beginning of the Arduino project when "9V" transformer-rectifier-capacitor power packs were common and this was a practical way to power a lone Arduino board for initial demonstration purposes. And even then it was limited because an unloaded 9 V transformer-rectifier-capacitor supply would generally provide over 12 V which the regulator could barely handle.

If we are discussing this, it is generally the case that you will wish to connect something else, in your case a relay module and MFRC522. In which case, the answer is regulated 5 V.

This is because the on-board regulator is essentially capable of powering only the microcontroller itself and no more than a couple of indicator LEDs. The on-board regulator might be able to power a few other things if it had a heatsink, but on the (older) Arduinos, it does not.

Powering via the "barrel jack" or "Vin" connections is asking for trouble. The "5V" pin is not by any means an output pin, if anything a "reference" pin but most certainly the preferred pin to which to supply a regulated 5 V.

A practical power supply for the Nano (or UNO, Pro Mini, Leonardo etc.) is a "phone charger" with a USB output connector for 5 V, generally up to a couple of Amps though you can not feed more than 500 mA through the USB connection. In this case however, you are using a 12 V power supply for the strike, so you could utilise a switchmode "buck" converter to provide regulated 5 V, given that you carefully power the converter by paired connections directly from the 12 V, locate it close to the Arduino and pair the 5 V output connections neatly to the Arduino board "5V" and ground and separately as a pair from the converter output terminals to the relay board. The relay control wire must then run along with those power supply wires to the converter and back with the power supply wires to the Arduino in order to implement the close pairing I describe.

Paul__B:
OK, many matters of significance here.

Works when the electric strike is not connected but fails when it is? And is fussy about which way up you plug in the adaptor?

OK, the problem is that the impulse generated when the relay switches off current to the lock is just so dramatic that it is being radiated like a radio signal from the lock circuit back to the Arduino and crashing either the Arduino or the MFRC522. The solution for this part of the problem is twofold - you need to suppress that impulse with a diode across the strike solenoid - any common power diode will suffice, 1N4004 or such - and of course it must connect in the direction so that it does not conduct when you power the strike solenoid.

However there is the problem of this impulse actually getting from one part to another. You must keep the connections to the solenoid and its power supply, totally separate from the logic - and power - connections to the Arduino and its attachments such as the MFRC522.

Also absolutely critical is the matter of “lead dress” - keeping all wiring from device to device paired or grouped together in single bundles. Any open loops invite electromagnetic coupling from one part to another, and associating “active” wires with a ground also limits capacitive coupling of transients.

Finally you need to provide proper power supply to the Arduino and relay module. A very real danger is that the obsolete tutorials on the Arduino site and others misleadingly imply that the largely ornamental “barrel jack” and “Vin” connections to the on-board regulator allow a usable source of 5 V power. This is absolutely not the case. It is essentially only for demonstration use of the bare board back in the very beginning of the Arduino project when “9V” transformer-rectifier-capacitor power packs were common and this was a practical way to power a lone Arduino board for initial demonstration purposes. And even then it was limited because an unloaded 9 V transformer-rectifier-capacitor supply would generally provide over 12 V which the regulator could barely handle.

If we are discussing this, it is generally the case that you will wish to connect something else, in your case a relay module and MFRC522. In which case, the answer is regulated 5 V.

This is because the on-board regulator is essentially capable of powering only the microcontroller itself and no more than a couple of indicator LEDs. The on-board regulator might be able to power a few other things if it had a heatsink, but on the (older) Arduinos, it does not.

Powering via the “barrel jack” or “Vin” connections is asking for trouble. The “5V” pin is not by any means an output pin, if anything a “reference” pin but most certainly the preferred pin to which to supply a regulated 5 V.

A practical power supply for the Nano (or UNO, Pro Mini, Leonardo etc.) is a “phone charger” with a USB output connector for 5 V, generally up to a couple of Amps though you can not feed more than 500 mA through the USB connection. In this case however, you are using a 12 V power supply for the strike, so you could utilise a switchmode “buck” converter to provide regulated 5 V, given that you carefully power the converter by paired connections directly from the 12 V, locate it close to the Arduino and pair the 5 V output connections neatly to the Arduino board “5V” and ground and separately as a pair from the converter output terminals to the relay board. The relay control wire must then run along with those power supply wires to the converter and back with the power supply wires to the Arduino in order to implement the close pairing I describe.

Appreciate your reply.

So, what I got is that :

  • I should change the main power to arduino: use the USB cable which provides 5V and connect it to a phone charger. It is worth mentioning that hopefully when I’m done with this project I intend to keep the arduino running 24/7, so is the phone charger ideal in this case?
  • I should use a diode. Thing is I’ve never used one. I know they are important and such, but I haven’t really used one so I don’t know what is connected where, so I’d really appreciate if you point that out or refer a link. That’d be very helpful. Also, will 1N4007 be okay to use?
  • I shouldn’t power the strike using the VIN of the arduino, and that I’ve already got. I’m using separate power supplies for the strike and the arduino now.

I wanna also point out that I got it working by connecting the AC directly to the relay. In other words, the relay is controlling the AC 220V connected to the (AC220V to DC12V) adaptor instead of the DC 12V connected directly to the strike.

(like this)
I don’t know the risks of doing that, but that’s the only way I got it working flawlessly for some reason.
By the way, please tell me if that holds any risks. I’m using something similar for another project where the relay controls AC flow to a lamp, and the arduino is powered by 12V 2A, and It has been running 24/7 for two weeks now, and I intend to keep it that way.

Hi,
Can you please post a picture of your project so we can see your component layout?

Keep ALL the 12V wiring associated with the lock mechanism away from other wiring.

I’m using something similar for another project where the relay controls AC flow to a lamp, and the arduino is powered by 12V 2A, and It has been running 24/7 for two weeks now, and I intend to keep it that way.

The project with the lamp does not produce the current spikes that an inductive load like the door lock has.
The door lock comprises of a solenoid, this is a very inductive load.

Tom… :slight_smile:

Electrically switching mains work. However it's not entirely safe. Suppose the mians cable comes loose someway. You might burn down not only the oroject but the entire building.
Learning how to connect a diode is well worth that half minute it takes.

Well, if now you are switching the AC which is the supply to a switchmode converter which actuates the strike, then you cannot use a diode. But the switchmode converter is not an inductive load so you no longer have the same problem of suddenly switching off an inductor and indeed, the power to the strike shuts down very smoothly as the capacitors in the switchmode converter discharge.

Unfortunately, you have a different problem!

Switchmode converters do not like frequent switching (on) of their supply input and this would be in the long term, unreliable.

The diode is connected as in this diagram:

If you are using a relay, then your relay would take the place of the FET here, this is just to indicate how the diode is connected across the solenoid (strike) which you may take as the "M". However this is the circuit you would use with a logic-level FET of adequate current rating (we have yet to figure out just how much current it requires) to control the strike directly from a 12 V power supply.

TomGeorge:
Can you please post a picture of your project so we can see your component layout?

Well, It's kind of a mess now, but..





Hi,
I'm sorry but how many different projects are there in your pictures and which is the one we are giving advice on?

I don't see in any of them, the doorlock and the relay and the UNO connected.

Please just the project your are asking about.

Thanks.. Tom... :slight_smile:

TomGeorge:
I don’t see in any of them, the doorlock and the relay and the UNO connected.

Please just the project your are asking about.

Thanks… Tom… :slight_smile:

It is one project, but It’s a mess. I had to remove the adaptor and the doorlock in the first couple of pictures to show how the relay is connected and the outlet. The door lock and the adaptor are in the last picture.

Hi,
So this is the final circuit.
The reason it works is because you are now switching a load that is not inductive.
The inductive load is isolated from the rest of your circuit by the 220Vac to 12Vdc plugpack.
The plugpack is probably absorbing the inductive spikes from the lock solenoid.
Have you put a back EMF diode across the lock terminals?