I use Arduino UNO and two SPST Relay (Wemos Mini D1 Relay) to make the reverse polarity circuit. I use this circuit to open and close the Solenoid Water Valve (SWV), which work with 9 to 12 volt. The connections are as follows:

NO (Relay1) --> NC (Relay2) --> +12 Volt
NC (Relay1) --> NO (Relay2) --> 0
D1 (Relay1) --> D1 (Relay2) --> Digital pin 3 of Arduino
COM (Relay1) --> SWV1
COM (Relay2) --> SWV2
GND Relay1) --> GND (Relay2) --> GND of Arduino
5V (Relay1) --> 5V (Relay2) --> 5 Volt of Arduino

the circuit work perfectly with Arduino boar. But the problem is that when I use ATMEGA328p-PU on breadboard as explained in LINK, Relays are not working properly. Actually, relays connecting and disconnecting continuously when ATMEGA328p sends 1 to D1 pins of Relays.

The point is when I de-attach the SWV, relays work properly. but if the SWV is connected it still is not working.

I've checked the connection many times and they are correct.

I used the following code in arduino:

int Relay = 3;
void setup() {
  // put your setup code here, to run once:
  pinMode(Relay, OUTPUT);
  digitalWrite(Relay, LOW);


void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(Relay, HIGH);
  digitalWrite(Relay, LOW);


Any help is highly appreciated.

Thanks in advance.

  • Could you show a schematic (or drawing) and a photo please. When someone puts a atmega328p on a breadboard for the first time and adds a relais or motor, then it almost never works. There are currents going the wrong way, there is a lot of noise, peak pulses bounching through the breadboard, unstable crystal, and so on. – Jot Mar 17 at 15:39
  • I did exactly the same as the link mentioned in the post. – user42037 Mar 17 at 15:55
  • 1
    sigh... In typical Arduino fashion, the "official" documentation is woefully inaccurate and misleading. Oh dear. I wish they would stop trying to produce these tutorials - they make our lives so hard... – Majenko Mar 17 at 16:01
  • what is a reverse polarity circuit? – jsotola Mar 18 at 5:41

The problem is most likely EMI (electromagnetic interference) from the switching of the inductive load by the relays.

The fact that you have your setup on a breadboard means that each and every pin of the ATMega328 is connected to a little antenna. That massively increases its susceptibility to falling prey to EMI. Add to that the fact that there is absolutely no decoupling of the power supply (thanks to Arduino producing shoddy worthless tutorials) makes the whole chip very fragile.

When running with a real Arduino board you have decoupling on the power which helps to reduce induced power rail bounce which can cause the chip to fail.

There are two things that can help mitigate this:

  • Add decent decoupling to the ATMega328

This means adding capacitors between each VCC pin and ground. At the bare minimum a 100nF capacitor on each VCC pin is needed. Additionally a 1nF capacitor can be beneficial. A 10µF capacitor should also be added on the breadboard's power rails to act as a board-level reservoir.

  • Snubber networks on the switching

A snubber network, in its most simple form, is a resistor and capacitor in series. It is placed across the switching terminals of a device that is switching an inductive load to absorb the sudden spike of back-EMF from the collapsing magnetic field whenever the load is switched off.


simulate this circuit – Schematic created using CircuitLab

Note that the capacitors here should be rated for at least 400V since the voltages involved are quite high.


An inductor can create large pulses of power when turned off and on such as in a relay or a solenoid. A flyback diode is usually added across inductors to suppress these pulses. Indeed if you look at the schematic of the Wemos Relay Board used in this project you see where D1 is used in just such a way. Unfortunately the voltage is being applied in both directions across the solenoid so a flyback diode can not be used directly across the solenoid. However, it may be possible to add a flyback diode before the relays where the voltage is not reversing.

It is almost always better to mitigate electrical noise such as Electromagnetic Field (EMF) interference at its source. Hence the flyback diode. However, many combination logic and processor designers add decoupling capacitors as close as possible to the power terminal of each logic chip. This may be the reason a real Arduino board works while the bread board does not.

  • If the case that you mentioned is a problem the circuit with Arduino board should have not worked while, as I've mentioned, it works properly and the problem is with ATMEGA328 on breadboard. – user42037 Mar 17 at 15:54
  • The way I understand it, the solenoid is galvanically isolated from the Arduino by the relays. Thus no back-EMF can get to the Arduino. More likely it is EMI from the switching arcs crashing the ATMega on the breadboard due to a lack of proper decoupling and power management which the Arduino "tutiorial" (hah!) completely lacks. As always, Arduino completely misleads their poor users with the garbage they produce in the name of "tutorials". – Majenko Mar 17 at 16:38
  • @user42037 I will modify my answer to reason out why a real Arduino board works while your bread board does not. But, please note, it is almost always better to mitigate electrical noise at its source. To do otherwise almost always takes more effort. – st2000 Mar 17 at 21:15

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.