I am using an Atmega 1284 microcontroller and four 5v electromechanical relays to turn on/off appliances based on some sensors, time, etc. The ATmega has its own 5v power supply in parallel with an 8v supply that I am using for the relays. Both of these power supplies are getting their DC from the same transformer after its been rectified.

I am having an issue where the appliance that I am connecting to the relay shorts the entire circuit when turned off. It turns off the LCD screen and resets all the variables in the program on the Atmega. After about 2 seconds the circuit reboots to the my home screen but all the variables have been reset. I have measured the current coming from one leg of the transformer and before the appliance is turned on the current is at a steady 30ma. After the appliance is turned on it jumps to 70ma. After I turn the appliance off the current drops to about 15-20ma and shorts the Atmega.

I have tried to solve this issue by using an optocoupler/optoisolater in between the signal pin of the Atmega and the transistor just before the relay to try to isolate the branches as much as possible. That hasn't solved the issue. I have read all of the place that isolating the circuits is the best way to go to try to save the Atmega from damage. But how can I isolate the circuits when they technically share the same ground from the transformer? Or is that even the issue here?

Here is the circuit:

[I left out some things like the 16mhz crystal, sensors, lcd screen and buttons in the 5v branch of the circuit. Only did one of the 4 relays. I also put in the voltmeter where I was taking my measurement and getting a  current drop. Hope this is good enough to get some help it would be much appreciated.]

  • 6
    \$\begingroup\$ I respect you for the obviously tremendous effort that you put into drawing that schematic, but honestly: this site has a schematic editor that would have been much faster, and much better. The interesting part of your design, the grid side of the relay, is not clear. It looks like you have a short there by design. \$\endgroup\$ Nov 5, 2017 at 22:43
  • 1
    \$\begingroup\$ Im using a 5v Songle relay (srd-05vdc-sl-c) \$\endgroup\$
    – Tay
    Nov 5, 2017 at 22:50
  • 1
    \$\begingroup\$ Also clarify how you connect to the opto coupler. It looks like you're using a NPN as a high side switch to the PNP relay driver which is in turn used as a low side switch. That won't work very well I think... \$\endgroup\$
    – Dejvid_no1
    Nov 5, 2017 at 23:09
  • 1
    \$\begingroup\$ @Dejvid_no1 one thing after the other… Pretty sure that is not causing a short on the high-voltage side. \$\endgroup\$ Nov 5, 2017 at 23:10
  • 1
    \$\begingroup\$ @Tay point is that in schematics, components are not just white boxes; a white box doesn't tell us anything about to which internal connection you've wired up your grid etc. So, your self-drawn schematic is pretty much useless :( \$\endgroup\$ Nov 5, 2017 at 23:11

4 Answers 4


Your problem is lack of awareness to EMI solutions

( hundreds on this site alone)

The problem is not that Relay shorts MCU , rather that a reset condition from ground shift or voltage noise on lines i.e. EMC issue.

Noise glitches may be conducted or radiated by E field (Voltage) or H field (current) when there are high impedance lines close to untwisted inductive back EMF surge voltages.

Since you have tried conductive isolation, that leaves radiated noise isolation.

The solution to this depends on your schematic and layout.

Check for :

  • inductive load diode clamp for DC , RC snubber for inductive AC loads
  • use ground from power source not shared power ground cable for pulsed loads
  • twisted pair for inductive loads and coil driver
  • shielded cable helps as well to suppress the emission over twisted pair or STP - cable proximity and non-parallel orientation

  • add ferrite sleeves to suppress CM noise.

Opinion (based on experience)

  • Opto-isolation was unnecessary.
  • solenoid wiring needs to be twisted pair
  • solenoid needs a snubber to reduce bandwidth of voltage spike and thus crosstalk voltage to signal/return lines.
  • DC signal cables ( if any) need to be twisted pair.

enter image description here

  • plastic film X-rated snubber cap avail from avail from Digikey etc. enter image description here
  • \$\begingroup\$ This solution seems to work thank you very much. The changes I made were: removed the optocoupler, added a snubber circuit across the contacts (.12uf, 400vdc capacitor in series with a 100ohm resistor), and also changed the wire connected to the relay from neutral to live. Still using a single transformer with two DC power sources in parallel. Thanks again! \$\endgroup\$
    – Tay
    Nov 11, 2017 at 1:24
  • \$\begingroup\$ cheers !!!!.... \$\endgroup\$ Nov 11, 2017 at 2:01

The only thing that makes sense about resetting the circuit when turning the relay off is that you're not handling the stored current from the solenoid part of the relay. You do this with a flyback diode, which it appears that you have one, but it isn't clear how you have wired it. Make sure thAt when the relay is energized, the diode is not. Also, follow the direction of the current through the coil, and when you deenergize the relay, the resulting current should find a recirculating path through the diode.

If you have a scope, put a probe on the switching node of the relay coil. When you shut it off, if you don't have it hooked up right, you'll see it spike up to 10s or 100s of volts.


The indication from the information that you have provided is that you are not actually getting a "short" happening. Instead it would seem that instead you are getting latchup happening inside your microcontroller. Latchup is a phenomenon that happens on complex IC chips when illegal signal conditions are presented at the pins of the part and internal stray circuit paths act like an silicon controlled rectifier and turn on and conduct current through parts of the silicon that are not valid for current flow. Latchup can vary from annoying and requiring a simple Power Cycle to reset the problem to a more serious type where the latch current critically destroys all or part of the IC chip.

Latchup will typically occur when some IC pin is taken to a voltage way over the VCC rail or below the GND rail. Voltage spikes coupled into the circuit are the usual latchup trigger and can come from static discharge or switching circuits. In your case it is most probable that the AC mains voltage switching is causing spikes in your circuit.

Switching AC mains with an electromechanical relay can cause noise and voltage spikes up to 100s of volts. The noise can come from contact bounce, contact arcing and switching inductive loads. There are a number of things you may need to consider for investigating your problem.

  1. Take a look in your circuit with an oscilloscope to determine if you have high voltage spikes.
  2. Use the scope with proper probing techniques to see if there are serious ground rail surges going on.
  3. Learn about AC switch snubber circuits and how these can be used to reduce spiking and noise on relay contacts.
  4. Evaluate carefully how you have your circuit constructed. Make sure to isolate and keep all the AC power wires keep completely away from any of the low voltage portions of the circuit.
  5. Evaluate having a fully separate transformer for the 8V supply and use that only on the relay side of the optocoupler. This would allow full isolation of the grounds between the MCU subsystem and the switching subsystem.
  6. Check into the actual use of real solid state relays that incorporate zero crossing switching. These can significantly reduce AC switching noise and spikes.
  • \$\begingroup\$ If it was actually latching up, the MCU would get very hot, possibly to destruction, and would not reset without a power cycle, if then. This is a 'soft' failure. \$\endgroup\$ Nov 6, 2017 at 0:03
  • \$\begingroup\$ @SpehroPefhany - I have to differ with you. Latchup can be soft or catastrophic. \$\endgroup\$ Nov 6, 2017 at 0:04
  • \$\begingroup\$ @SpehroPefhany - The strong evidence of soft latchup in this case is the increase of current drawn from the power supply. A typical MCU software that simply got scrambled would not increase the current draw a lot except for very specific circumstances including - spikes waking the MCU from a low power sleep type state or crashed software changing GPIOs to s state that causes direct bus wire contention from other circuits on the board. The latter would be inputs to the MCU that had the GPIOs turned to outputs in the opposing state. \$\endgroup\$ Nov 6, 2017 at 0:36
  • \$\begingroup\$ You don't think the increased current might be related to the relay coil? \$\endgroup\$ Nov 6, 2017 at 1:45

Assuming it only does this when the appliance is connected, the problem is EMI from the contacts scrambling the operation of the MCU.

Your optoisolator does nothing of value since you are using the same supply.

Try using a different supply just for the relay coil and try to clean up your layout around the micro, and the wiring to the load. In particular, avoid having any more connections than those you have shown (such as to a debugger or programming pod).

You can try an MOV across the contacts but typically the long term solution is a better relay and/or isolation and better layout (a 4-layer board with ground plane for example).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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