Circuit with RFID and Relay sometimes misbehaving

I built a circuit with a MEGA2560 that contains an RFID reader and relay. When the RFID reader sees a specific fob, it needs to switch the relay to closed.

That is, the relay is in an open state all the time except when the fob touches the reader.

The relay controls 12v going to a magnet that I'm going to use to lock a door.

Once in a while, the whole circuit hangs. The RFID reader stops responding. It's not a code issue. I suspect an electric issue.

Appreciate your help.


Video explanation : https://photos.app.goo.gl/iMNhRxiXGNPbqpPbA

/*
PINOUT:
RC522 MODULE    Uno/Nano     MEGA
SDA             D10          D9
SCK             D13          D52
MOSI            D11          D51
MISO            D12          D50
IRQ             N/A          N/A
GND             GND          GND
RST             D9           D8
3.3V            3.3V         3.3V
*/
/* Include the standard Arduino SPI library */
#include <SPI.h>
/* Include the RFID library */
#include <RFID.h>

#include "pitches.h"

/* Define the DIO used for the SDA (SS) and RST (reset) pins. */
#define SDA_DIO 9
#define RESET_DIO 8

/* card IDs */
#define ETHAN_PHONE 86924247144
#define BLUE_FOB 601852375595
#define WHITE_CARD 2112021385150


/* Create an instance of the RFID library */
RFID RC522(SDA_DIO, RESET_DIO); 
#define GREEN_LED 3
#define RED_LED 2
#define MAGNET 5
#define BUZZER 10

void setup()
{ 
  Serial.begin(9600);
  /* Enable the SPI interface */
  SPI.begin(); 
  /* Initialise the RFID reader */
  RC522.init();
  Serial.println("Hello!");
}

void loop()
{
  /* Has a card been detected? */
  delay(1000);
  if (RC522.isCard())
  {
    /* If so then get its serial number */
    RC522.readCardSerial();
    Serial.println("Card detected:");
    char buf[15]; 
    sprintf(buf, "%d%d%d%d%d", RC522.serNum[0], RC522.serNum[1], RC522.serNum[2], RC522.serNum[3], RC522.serNum[4]);
    Serial.println(buf);
    Serial.println();
    if (strcmp(buf, "601852375595") == 0) {
      Serial.println("It's the blue fob!");
      digitalWrite(GREEN_LED, HIGH);
      digitalWrite(RED_LED, LOW); 
      analogWrite(MAGNET, 0  );
      tone(BUZZER, NOTE_FS3, 1000);
      delay(5000);
    }
   
  } else {
    digitalWrite(GREEN_LED, LOW);
    digitalWrite(RED_LED, HIGH); 
    analogWrite(MAGNET, 255);
  }
} 

After posting here, the recommendations that the forum gave (at the bottom of the screen) were interesting. I saw other people run into the same thing - and then encountered this post: RFID Scan Stops Working After the First Loop - #5 by van_der_decken (by @van_der_decken)

I had the RFID wired to the 5v, not 3.3v. I fixed that, and so far it seems to work flawlessly.

Almost all the internet gets this wrong but this is how you should connect a 3V3 RFID reader to a 5V system:-

Exactly! Thank you @Grumpy_Mike!