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.
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");
}
}
}
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.
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?
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?
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?
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.