AVR main loop interference with relay

avrback-emfmicrocontrollerrelay

I'm working on a test program on an ATmega164A that toggles a 12V relay on and off (with no load) through a ULN2003A. For my test program, I'm toggling the relay at 100ms. I've got an IN4001 diode across the coil. Additionally, I've got an LCD that displays a milliseconds counter, which increments in an interrupt. In normal operation, it goes on forever without any issues. I noticed that one of my relay sockets was wonky, so sometimes I need to fiddle with the relay in the socket to get it working again. When I did this, I noticed that sometimes the main loop would hang and I'd get some garbage on the LCD. If I fiddled with the relay some more (and specifically the coil contacts in the socket), the main loop would resume, but the counter value would be correct, indicating that the main loop would be paused but the interrupts would continue.

I mostly fixed the garbage on the LCD by putting a 0.1uF cap right at the power pins, but the main loop will still hang if I do what I mentioned above. I know this is kind of a weird situation and it should never really happen, but I'm just trying to understand it. I just figure that if the main loop gets interrupted somehow, then surely the interrupts wouldn't still be working correctly.

Best Answer

Your hand was probably part of the problem in this case. By capacitively coupling the noise from the coil -- which would not be snubbed (snubbered? LOL) okay clamped by the diode since the break was at the coil to ground you've probably introduced noise through other conductors. The voltage at the coil end could be hundreds of volts when the connection breaks.

This is not necessarily a problem since it's an unusual situation but it points out that you may need to protect or shield your inputs, outputs or internal wiring better.

BTW, you don't need an extra diode across the relay coil if it's close to the ULN2003- there's a diode available in there if you tie it to the relay supply voltage. Of course if you're switching mixed voltages on different outputs with the ULN2003 you may wish to use external diode(s) to control to which supply the energy is returned.