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.

  • \$\begingroup\$ Does the main loop have any 'while loops' (possibly inside functions it calls) which are reading input pins? For example is it waiting for an acknowledge back from the LCD? It may be that while fiddling with the coil contacts you caused some electrical noise which effected the code in the main loop. Often a timer is serviced in an interrupt, so it might be correct because it isn't being blocked in the main loop. Does any of that sound plausible? \$\endgroup\$ – gbulmer Sep 24 '14 at 0:41
  • \$\begingroup\$ Yes, it does indeed! I'm using an LCD library and just found the lcd_waitbusy() function, so that's probably the culprit. What can I do about the LCD interference? Caps on the i/o lines? \$\endgroup\$ – Andrew Sep 24 '14 at 0:47
  • \$\begingroup\$ You might want to put a 10nF-ish capacitor across the coil, to reduce the likelihood of noise from the coil interfering with MCU to LCD communication. There are a bunch of ways to 'quite' electrical noise, and I think there are some answers already on this site. I'll have a quick look, but it is almost 2am, so I'll be signing off very soon. \$\endgroup\$ – gbulmer Sep 24 '14 at 0:49
  • \$\begingroup\$ Search for "reducing electrical noise" and several questions look relevant, remember motors are somewhat coil-like!-) So they may have some relevant advice too. \$\endgroup\$ – gbulmer Sep 24 '14 at 0:54
  • \$\begingroup\$ How is the ATmega supplied? \$\endgroup\$ – venny Sep 24 '14 at 1:18

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.


A capacitor across the relay coil, and a series resistor, in addition to the diode may form a more effective snubber. See attached schematic, though you might need to experiment with values.

If you want to go all-out, use an isolated power supply and optoisolator.


simulate this circuit – Schematic created using CircuitLab

  • 1
    \$\begingroup\$ The ULN2003 is not a source (high side) driver. \$\endgroup\$ – Spehro Pefhany Sep 24 '14 at 3:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.