Electromagnet with relay reset arduino

I have an Arduino Mega, a 12v source, an electromagnet and a relay.
I pass one cable from the electromagnet through the relay and connect it to positive and the other cable I connect to negative of the source. I do that with my engines and they work fine.

The problem is that when I turn off the electromagnet relay it resets the Arduino, but the motors don't do that.

Electromagnet: https://www.amazon.com/gp/product/B00BHT49NM/
Relay: https://www.amazon.com/SainSmart-101-70-103-16-Channel-Relay-Module/dp/B0057OC66U/

It is a simple code, the problem is not in the code, I think it is the hardware, because as I said it does not happen with the actuators.

// Definir el pin del relé
const int pinRelay = 10;

void setup() {
  // Inicializar el pin del relé como salida
  pinMode(pinRelay, OUTPUT);

  
  // Iniciar la comunicación serial a 9600 bps
  Serial.begin(9600);
  Serial.println("inicio");
}

void loop() {
  // Verificar si hay datos disponibles en el puerto serial
  if (Serial.available() > 0) {
    // Leer el primer byte recibido
    char comando = Serial.read();

    // Realizar la acción correspondiente según el comando recibido
    if (comando == '1') {
      // Encender el relé
      digitalWrite(pinRelay, HIGH);
      Serial.println("Relé activado");
    } else if (comando == '2') {
      // Apagar el relé
      digitalWrite(pinRelay, LOW);
      Serial.println("Relé apagado");
    }
  }
}

Did you add the freerunnig diode to the magnet?

1 Like

Put SS34 or another similar diodes in reverse polarity to relay coil and electromagnet. keep them as close as possible to the terminal of relay and electromagnet. Coils generate back emf when de-energised and that creates trouble.

2 Likes

Despite that, you posted your code but not your schematic. Why is that? Is it because posting the code is easy, but posting the schematic will require some effort, because you did not draw one yet?

1 Like

My electronics are very basic so it's hard for me to understand. I have the electromagnet connected directly without anything else. Is there a simple way to solve that problem?

Do I just need two gods and connect them on the positive and negative line?

https://forum.arduino.cc/t/flyback-diodes-and-why-you-need-them-comments-here-please/675412/76

and:

1 Like

Does any diode work? What if I only put one?
I have this one:
Screenshot 2023-11-28 at 10.35.07 AM

Try it, should do.

1 Like

I ask because I don't want to burn anything. It's not clear to me if I should connect the diode there:

Which one is correct?

The second one.

It works, but after several attempts or several quick attempts it restarts again.

I placed it like this:
3.

When I made this connection on 2, the attempts were fewer:

is the 12V powersupply connected to your arduinos power supply?

I have the 12v 10A power supply negative connected to the arduino GND. But I power arduino from my computer.

3 works fine, sometimes, and after multiple attempts it restarts.

The 16 channel relay board you are using is a 12V version NOT 5V.
Are you supplying 12V to the relay board?

After reading this I made the connections to 12V because maybe that was the problem, but after connecting it I started to smell something burning, I think it was the Arduino.

The relays worked fine with 5V, it is the 2nd arduino that I burned after connecting the 12V to the relays, I don't know why it happens. But I don't think that's my problem, is it?

Probably not. You need to show us exactally how you actually have things connected and maybe someone can figure it out

Should be like this

I burned my arduino and my USB-C adapter, now I'm afraid to plug it in to 12v. With 5V it works fine.

If I make that connection, will the Arduino not restart or will it restart? Because as I said, my relay cuts the 12V positive that goes to the magnet.

Check your relay board, maybe it is missing the diodes or has a bad soldering of the diodes.

Post a clear picture of the relay board you have.
There are several versions of that board. Some have 5volt relays and have no buck converter, others have 12volt relays and a 5volt buck converter that powers the ULN chips and the Arduino.
All 16-channel boards I have seen have a design fault, where you can't select opto isolation.

Not the case if you have the relay board with an LM2576 buck converter.
Then the relay board could even back-power the computer's USB port.

Do you know which part burns on the Mega.
Leo..