I develop a system that filters water. I added two solenoid valves connected with a relay to the circuit. and uploaded this code to check if they worked well or not:

#define valves 10
#define led 2
void setup() 

void loop() 

  digitalWrite(valves, HIGH);
  digitalWrite(led, HIGH);
  digitalWrite(valves, LOW);
  digitalWrite(led, LOW);


then, I turned the power supply on and observed that the microcontroller (ATmega328p) executes the first two lines in the loop() which are digitalWrite(valves, HIGH); and digitalWrite(led, HIGH); and doesn't change the state of the relay and the led into LOW state after 5 seconds, in addition to executing random instructions that does not exists in the code like toggling any I/O pin.
here is the circuit: enter image description here

I tried to solve this problem. I measured the input current and the output current of the adjustable voltage regulator (LM317). The input current was 100mA and the output current was 90mA.

The LM317 was very hot, so much so that you can feel the heat when you approach your finger to the LM317. Although the regulator was very hot, it still regulates 5V and it does not change.

I changed the LM317 circuit to regulate the 33V to 14V and added a 7805 fixed regulator. I thought that I reduce the dissipated power in the LM317 therefore, the heat will be less and the microcontroller will work properly. I turned the power supply on and the 7805 regulator was super-heated and the LM317 was warm. but they still make the 5V.

When I disconnected one of the valves from the relay and turned the power supply on, the microcontroller executed the code well for a while and stopped executing it. when I disconnected the both valves, the microcontroller executed the code well and didn't stop.

this is the water inlet valve which I use in my project:

enter image description here

I replaced the 470uf capacitor with 2200uf capacitor. Moreover, I added

  • 10uf electrolytic capacitor
  • 1uf electrolytic cap
  • 1uf ceramic cap
  • 100nf cap
  • 10nf cap
  • 1nf cap
    near the microcontroller I also added a flyback diode for each relay
    But the problem was not solved. The device still the same.
  • 5
    \$\begingroup\$ No bypassing at all, especially driving relays and solenoids? Also show us your PCB layout. \$\endgroup\$
    – Wesley Lee
    Jul 11, 2022 at 1:56
  • 2
    \$\begingroup\$ Also consider reading this to make your schematics more readable: electronics.stackexchange.com/questions/28251/… \$\endgroup\$
    – Wesley Lee
    Jul 11, 2022 at 1:57
  • 4
    \$\begingroup\$ You're missing a bypass cap on your micro. You have no flyback diodes on your relays. Q2 and Q4 serve no useful purpose. \$\endgroup\$
    – brhans
    Jul 11, 2022 at 4:05
  • 1
    \$\begingroup\$ I would suggest removing the loads, one by one (i.e. valves, relays and so on), until you find out what is causing the issue. If you have a scope you can probe for the power lines and so on to see where the noise is causing issues (could be power supply drop, reset line, etc). \$\endgroup\$
    – Wesley Lee
    Jul 12, 2022 at 2:32
  • 1
    \$\begingroup\$ Use flyback diodes across your coils - relays/ solenoids whatever. Use bypass caps on output of your power supply, and BETWEEN any high switching elements. Ideally connect the high current elements directly to PS point, and uC circuitry also directly via a separate pcb trace/ wire. And PLEASE start with paying attention to your power supply -- this is the life-blood of yor circuit, so why neglect it ? a lm317 dropping (33-5) 28 volts won't deliver more than 50mA before imploding. Use a buck converter !! OR even a USB power adaptor !! DO check the specs of your relays ? You have not specified. \$\endgroup\$ Feb 17, 2023 at 17:58

2 Answers 2


After doing some more research and testing, I found that the reason my microcontroller wasn't executing the rest of the code after turning the first valve off was due to the back emf generated by the solenoid valves. This was causing interference with the microcontroller's circuits and preventing it from functioning properly.

To solve this problem, I attached RC snubber circuits to each of the solenoid valves. These snubber circuits helped to eliminate the back emf and prevent interference with the microcontroller. As a result, I was able to successfully turn off all the valves and execute the rest of the code without any issues.

I hope this solution helps anyone else who may be facing a similar problem with their microcontroller and solenoids.

  • \$\begingroup\$ Thanks for coming back to add this insight. \$\endgroup\$
    – greybeard
    Feb 17, 2023 at 16:49

This is not intended as a comprehensive answer but a starting point along with the other fine recommendation you have received so far.
Your regulator is not properly bypassed, the correct values will be given on the data sheet. Not all manufacturers recommend the same values so check the data sheet for the part you are using. I think with a scope you will find it is oscillating causing some of your heat.
You are trying to dissipate 2.7 Watts of power at 100mA as heat (25C + 168C) very hot. The TO-220 junction-to-air thermal to ambient equal 62.5 degree per watt. Without a heatsink do not expect it to last very long. I expect it would get hot enough to melt solder from its leads. The AC lines must be kept away from the logic lines.
You are operating the regulator at almost its maximum input voltage, it is very possible transients from the relay contacts are getting in via the DC power supply.


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.