The Arduino freezes after some time


I have arduino mega 2560. After some time (a few hours) it freezes and I can't figure out why this is happening... I think it will be a hardware error... It always freezes in another part of the cycle... it is least desirable when the solenoid vaves are open.. My code will be below.

I use in my circuit: 12 V DC power source - from it in shielded wire it goes to a 4 channel relay. I have 4 solenoid valves all are used everything is connected in shielded wire.. a diode is parallel to each valve. then it goes backt to 12 DC power source... The relay is powered from a separate 5 V source is galvanically separated from Arduino... Only the control wires are connected between the Arduino and the relay and one 5 V wire ... The arduino is powered by a classic USB charger... Then I have an LCD display in shielded wires it is powered by the Arduino and I have pull up resistor on data wires (1602)... Then I have a classic button... to stop the cycle.. in unshielded wires and with pull up resistor....then I have and RFID reader and I think that may be a problem... The rfid is in unshielded wires... is it powered from the same 12 V source as the solenoid valves. then it goes directly to Arduino (I dont use any resistors) - 4 wires... 2 data, 1 beeper, one LED.

I apologize for my English and writing the text quickly... Thank you for all the advice.


EDIT: - code:

Please post your code here in this Forum. And please use the code button </>

Also please make a simple pencil drawing showing all the connections and post a photo of the drawing. It is much too easy to misunderstand a textual description of connections. See this Simple Image Posting Guide


tabs in the code are showing up as printed characters in the URL you posted.

try posting the code as an attachment since it's > 9000 characters

Despite your efforts to anticipate and handle issues from the exterbal circuitry, it would be interesting to see if the program fails when just running some LEDs as proxies for you relays and stuff.


The symptom that you describe is often caused by writing past the end of an array into memory that you do not “own” or the use of the String class.

It always freezes in another part of the cycle... it is least desirable when the solenoid vaves are open..

I can't see the flyback diode connected across the coil of the solenoid valve on your transparent diagram.

Thank you. I will send everything tomorrow. I am very bussy. I will send the wiring diagram and the code. I couldn't put it here because of the 9000 characters. And the program fails occasionally without opening the solenoid valve. I have a yellow control diode.. And it stop blinking completely. See you tomorrow, Patrik

I apologize for the delay, attached is a simple wiring diagram and code.. I will also give a photo of the LCD display here... Thank you :slight_smile:

code_1.txt (14.8 KB)

Please make your images visible in your Post so we don't have to download them I gave you a link to the instructions in Reply #1


Although you have a separate supply for the relay module, I'm sure you wouldn't have opto isolation because there would be a ground connection from the 230VAC-12Vdc supply to the RFID reader and then to the Arduino Mega. The 12VDC-5VDC supply uses the same ground which is connected to the relay module.

Could try a few things:

  • a separate isolated DC supply for the relay module.
  • add a capacitor across each solenoid coil, maybe 1µF.
  • move the relay module and solenoids further away from the Arduino and display.

Thank you for your advice. I forgot one wire. The GND wire between RFID and Arduino. I have already connected that, because the reader doesn't work without it. My connection between the relay and the Arduino is this, link below:
Is that right? I removed the jumper and connected exactly like this. The solenoid valves are about a meter away. Those solenoid valves are located in another cabinet (unfortunately it's a wooden cabinet). The relay and Arduino are in a different wooden cabinet...; are drilled into the wall on a plastic plate... The Arduino on the valve side has a metal box, that is grounded (PE wire 12 DC power supply - actually the input to the power supply AC 230 V, 50 HZ, L - N - PE. The box is connected to PE.

I will try to put the capacitors in there, like you said. But I probably don't have that capacity. Do you think it would be a problem to give a bigger one?

and for Robin: I couldn't put the code visibly here because of the 9000 characters. I will visibly try the pictures now.


Thank you so much for your advice.


I've highlighted what I think the ground path is on your circuit. Your 12V-5Vdc supply for the relay module probably doesn't have isolated grounds. Now, the same ground that reaches the relay module is sneaking around through the RFID reader to the Arduino as shown. Also, the same ground for the solenoid valves reaches the Arduino.

If you have an ohmmeter, try checking the resistance or continuity from relay GND to Arduino GND and see what it reads.

As another test, I suspect that if you disconnect the RFID reader and its GND connection to the Arduino, the problem will go away.

Thank you very much.

Oh, I see. It will be true... I don't have a projet at home, I have to measure it with a ohmmeter tomorrow at night, but I think the resistance will be small. What else do you suggest? I have a separate 12 V source (it is original for arduino)... Maybe try to power the relay power supply from this separate power supply?

I have a separate 12 V source (it is original for arduino)... Maybe try to power the relay power supply from this separate power supply?

Sound like a good idea. I think using it to power the RFID reader could also accomplish the same thing (isolation). Good luck!


so I made the connection the way you wrote it. There was little resistance. After connecting a separate supply source, the resistance is infinitely high, however, it didn't solve my problem. I am starting to think I have some mistake in the program, but really I don't know...
My colleagues have reported these errors to me:

  1. The program freezes again - see the first photo (the yellow LED has stopped flashing - it is in a cycle of seconds) and the display froze too.

  2. They said that the whole display turned off (see second photo) and I don't know why... They told me that when they pulled out and turned on the power (230 V, 50 Hz) that again everything started working as it should, display too.
    EDIT: but in the second photo you can see that only brightness went out.

So it makes me a little tired :smiley: because I tried a lot... Thank you for all the advice.

First photo:

Second photo:

Another program stop. :frowning: :stuck_out_tongue: and really I don't know why.
My dynamic memory usage is 47 %.

I looked at your code and the array1 array is writing outside the bounds of the array. " Schvaleno " is 18 bytes (arrays are zero based) + the terminating null is 19 bytes (see the string reference). That will not fit into the allotted 17 bytes.

This may or may not be the problem but it is worth fixing. If you enable the compiler warnings in preferences, that would have generated a warning.

Run this to see the true size of the array1 array.

// char array1[17] = "    Schvaleno     ";//the string to print on the LCD
char array1[] = "    Schvaleno     ";  // array size left to the compiler.

void setup()
   Serial.println(sizeof(array1) / sizeof(array1[0]));

void loop()

Thanks for the advice, but I have already tried to solve this as a static text and the problem still persist.

Now I had in the code - see below:

// Pohyblivy napis schvaleno
/* char array1[17] = "    Schvaleno     ";//the string to print on the LCD
byte tim = 100;  //the value of delay time */
/*   for (int positionCounter1 = 0; positionCounter1 < 16; positionCounter1++)
      digitalWrite (LED_BLUE, HIGH);
      lcd.print(array1[positionCounter1]); // Print a message to the LCD.
      delay(tim);                          //wait for 250 microseconds
    } */

and instead, I only wrote this:

     lcd.print (F("SCHVALENO"));
     delay (2000);

and the problem (freezing) still persist…

Do you want us to guess what changes you made to the code?

As a test, if you could disconnect the solenoid valves and leaving everything run for an extended period, it would be interesting to see if the problem goes away. If it does, then this points to either an issue with the 12VDC power supply or reverse connected diodes.

By the way, what's the part number of those diodes? They should be of sufficiently sized to handle the flyback current from the solenoid valve coils. This means we need the part number or datasheet of the solenoid valves also.