Lightweight program restart

Hi,
I have made a couple of Arduino project and this one is not much more complicated,
however it contains of some more relays and light barrier sensors. It is for controlling some modellcars in a model railway and some other things (through relays). I have been struggling the last days with the program resetting/running setup again.

I would appreciate some new eyes on this. :o :slight_smile:
best regards Johan

So far, as I am in testing and programming process, I supply the Nano with USB to check Serial output. The serial output will in production be shut of, and I will power Vin with 9v Mean well. That is what I have done to other projects. All sensors, relays etc. are of course powered by Mean Well 5 v stable power even right now. Everything connected with gnd.

Arduino Nano

Sketch uses 7406 bytes (24%) of program storage space. Maximum is 30720 bytes.
Global variables use 321 bytes (15%) of dynamic memory, leaving 1727 bytes for local variables. Maximum is 2048 bytes.

What happens?
When running the main part "carControl" it sometimes restart in case "MEETING". That is why the if statements checking sensors are out commented - maybe they makes the running hang up.

Sometimes the program freeze and restart, and so does the Serial Monitor output. And sometimes, it goes so quick and the program seems to continue its running (but the Serial text in setup is shown).

Some notes

  • Most strings in "F()".
  • I plan to use a LCD but this code is commented out and no LCD is connected due to I am doing trouble shooting and want to remove potential error sources. That is also why some other code are commented out. I have tried with/out the hand full of LCD-commandes in code that is not commented out. It still apperas to be problems.
  • I have tried changing US ant checking the power supply so I do not think it is hardware?
  • I have implemented a temporary internal LED for testing the program with out serial prints, and sometimes is freeze.

I have also tried to get an analyze here https://sparks.gogo.co.nz/avr-ram-use.html

Code attached!

Minnesanalys 2021-03-25.txt (5.78 KB)

Lidhult_Bilbana_1_v2_DEV.ino (41.1 KB)

Many times, resets are caused by power problems. What is the power source for the setup and what all is connected to the Arduino?

WARNING: You are checking the return value from 'checkActiveChargecontrol()' but the contents of that function have been commented out so it doesn't return a value:

  // Om laddningsmätning är aktiverad
  if (checkActiveChargecontrol() == true)
  {
    DEBUG_PRINT_F(" ");
    DEBUG_PRINT_F("F U N K T I ON: loop");
    DEBUG_PRINT_F(" if: checkActiveChargecontrol = true");
    chargeControl_voltageReading_1();
  }
// TEST TEST
// TEST TEST
/**
    Funktion: checkActiveChargecontrol


    Returnerar "true" om laddningsmätning ska vara aktiv
*/
boolean checkActiveChargecontrol()
{
  /*
...
  */
}

johnwasser:
WARNING: You are checking the return value from 'checkActiveChargecontrol()' but the contents of that function have been commented out so it doesn't return a value:

Good point, thank you.

The Arduino Nano is connected to the USB in the test. It shall be 9 volt in the main setup.
I have not yet tested this to see if there might be any difference when running.

The relay and sensors are supplied from 5v separately. The feedback from sensors and control pin to relay cards are connected to digital in/out.

EDIT 22:12
May the PC where I supply power to arduino + reed serial monitor affect the running of code?

Can you tell more about the relays and wires and cables and lengths ? Where are the ground wires going ? Can you make a drawing or show a photo ?

TagCentralen:
EDIT 22:12
May the PC where I supply power to arduino + reed serial monitor affect the running of code?

In order to trigger the bootloader for uploading, whenever a serial connection is established with the Arduino, the Arduino resets. Is the PC opening a new serial connection after the sketch is running?

I will try to make a drawing - I already have one in kiCad.

How can I check if the computer tries to open new serial?

TagCentralen:
How can I check if the computer tries to open new serial?

There is no way the Arduino can tell the difference, unless the PCB jumper that is provided to enable that default option, is cut. If you cut the jumper, a new serial connection will not reset the board, but also you can not upload normally.

So here comes some pictures and wiring. Everything is not connected yet eg. the LCD.

This is the relay used: https://www.electrokit.com/produkt/relamodul-5v/

I will not try further on with the ordinary power supply instead of PC and another Nano and further out commenting of code.

I have corrected the note from John.

I'm a little confused, but I will write down what got my attention:

  • You use a usb converter. Maybe a connector is low quality and the 5V from the USB is not going properly to the Arduino board. Can you measure the 5V pin with the relays turned on ? You could power the Arduino board with an extra 5V to the 5V pin or with 7.5V ... 9V to the VIN pin. I also suggest a capacitor of 470µF to the 5V pin and GND.
  • When a relay switches the mains voltage, there should be two separated parts in the wiring. You switch model cars in a model railroad. Is that inductive in some way ? I think you still have to keep the wires of the Arduino away from the wires of the railroad.

Koepel:
You use a usb converter. Maybe a connector is low quality and the 5V from the USB is not going properly to the Arduino board. Can you measure the 5V pin with the relays turned on ? You could power the Arduino board with an extra 5V to the 5V pin or with 7.5V ... 9V to the VIN pin. I also suggest a capacitor of 470µF to the 5V pin and GND.

I have tried to change to a coomplete cable. I have tried to supply with 9 volt to arduino and 5 v separtly to relays. Thats is also the setup now. Always running sensors and relays separetly with connected Gnd even when powering Arduino from pc.

  • When a relay switches the mains voltage, there should be two separated parts in the wiring. You switch model cars in a model railroad. Is that inductive in some way ? I think you still have to keep the wires of the Arduino away from the wires of the railroad.

The car system is based on Faller Car System. I have another system very similar running flawless. Cars are battery driven with coils in ground which creates a magnetic field so the car stops. Connection to motor is cut.

I have tried to shut of serial and run with arduono powered from pc respectively also 9 volt.

The other car system I have built works with no disturbance from the railway and so far I have not powered the railway up during test of this car system 2

Same problem.
So maybe next step is to remove more of the code and start testing from beginning.

Thanks in advance for further investigations. :slight_smile:

More information would help.

You could activate all relays a few times per second, all together or randomly. Then disconnect the wires to the relays, and run the same test. If that makes a difference, then only activate the relay coils, but keep the power to the relay switches off.

Do you have a collection of heat-shrink tube ?

Yes I have a collection of tubes.

Ok so you mean:
Write a simple program which activate/deactivate relay coils

Run it for a while with power on to the relay switches.

Then shut of power to switches still running the relays?

This is to find out if there are any induction that's interfere with the arduio?

Yes.
Induction might cause voltage spikes or current spikes, then there are ground currents, voltage drops, inductive coupling between wires, and so on. It's a wild world out there.

There is more than one thing going on:

  • The relay modules have leds (I think), the Arduino has to supply the current for the leds.
  • The coils of the relays need power, but peaks might travel trough ground wires or other wires and disturb the Arduino.
  • The contacts of the relays turn on and off your car models. That will give spikes. Those spikes have multiple ways to get to the Arduino.

If you can test that one by one, then you know more. If you have bad luck, then all three mess up the Arduino.

So, now I have made some try outs:

  1. Running the program without 12v to relay switches. Seems to work fine.

  2. Write a simple code:

            DEBUG_PRINT_F("  Växel: PÅ.");
            digitalWrite(relay_turnout_pin, HIGH); // Slå om växel.
            delay(1500);
            DEBUG_PRINT_F("  Vägst: PÅ.");
            digitalWrite(relay_roadstop_pin, HIGH); // Slå av vägstopp.  
              delay(1500);
            DEBUG_PRINT_F("  H1: PÅ.");
            digitalWrite(relay_mainstop_1_pin, HIGH);
              delay(3000);
        DEBUG_PRINT_F("  H1+vx: AV.");
            digitalWrite(relay_mainstop_1_pin, LOW);
             digitalWrite(relay_turnout_pin, LOW); // Slå om växel.
              delay(2500);
               DEBUG_PRINT_F("  Växel: AV.");
            digitalWrite(relay_turnout_pin, LOW); // Slå om växel.
            delay(700);
            DEBUG_PRINT_F("  Vägst: AV.");
            digitalWrite(relay_roadstop_pin, LOW); // Slå av vägstopp.  
              delay(500);
        DEBUG_PRINT_F("  H2: PÅ.");
            digitalWrite(relay_mainstop_2_pin, HIGH); // Slå av vägstopp.  
              delay(1500);
                 DEBUG_PRINT_F("  H2: AV.");
            digitalWrite(relay_mainstop_2_pin, LOW); // Slå av vägstopp.  
              delay(1500);

Test with 12v. It resets or gets crazy like:

48 ->
22:17:11.348 -> Ldh_BB1_V1.ino
22:17:11.348 -> F E L SÖ K N I N G Aktiv
22:17:11.348 ->
22:17:11.348 ->
22:17:11.348 -> LOOP IGEN
22:17:11.348 ->
22:17:11.348 ->
22:17:11.348 -> Växel: PÅ.
22:17:12.793 -> Vägst: PÅ.
22:17:14.291 -> H1: PÅ.
22:17:17.294 -> H1+vx: AV.
22:17:19.827 -> Växel: AV.
22:17:20.511 -> Vägst: AV.
22:17:20.511 ->
22:17:20.511 -> V⸮L st: PÅ.
22:17:20.556 -> H1: PÅ.
22:17:20.556 -> HSÖ K N I N G Aktiv
22:17:20.601 ->
22:17:20.601 ->
22:17:20.601 -> LOOP IGEN
22:17:20.646 ->
22:17:20.646 ->
22:17:20.646 -> Växel: PÅ.
22:17:20.646 -> Vägst: P

The Arduino does hang up every time almost.

Without 12 v on. Everything did run for 5 min (then I terminated the test...)

SO it seems that this is not an programming issue?

The strange thing is the fact that I have a similiar car system on another station of the railroad, running with similar components but maybe layouted slighlty different.

Checking voltage spikes (check between gnd and:)
Arduino Vin (powered by USB) 4,28-4,32 v
Relay signal 0-5 volts (off-on)
Relay power 4,7-5 volts.

I have not used any shrinking tubes on the relay power connections.
For switch side there are screw terminals.
I am thinking maybe optocouplers would help to fully isolate relay power/12-switch power from control signal of Arduino.

Thank you for the help from all of you so far.

The 5V pin is the voltage that the microcontroller runs on. I would like to know that voltage. The best is between 4.5 and 5.0V, but everything will probably still work at 4.0V.

The same circuit running fine is not a surprise. That is typical for how voltage spikes or grounding problems work. You can make 10 identical circuit of which 9 work perfect and 1 will not work.

The cable management made it worse. If you would have all the wires just going everywhere, then they don't influence each other that much.
It is best to keep the wires of the Arduino circuit away from the 12V wires. The normal way to do that is to have all the 12V wires at the top of the board and all the Arduino wiring at the bottom.

The relays should separate the two circuits. I don't understand your schematics, so I don't know if the grounds are connected.
With optocouplers, an extra level to isolate circuits is created. However, then you still have keep the wires of the two circuits apart and still have to know how to make a good ground.

The "grounding" is a craftsmanship on its own. This is basic information: Common ground and why you need one - General Electronics - Arduino Forum.

I'm trying to think how a spike from the 12V can have that result for the serial data. A voltage drop of the Arduino board would make more sense. Or a very strong spike. Or a very cheap Arduino board with almost no decoupling. Or a missing GND wire somewhere.

Thanks, for this reminder.

This morning it hit me,
in this very setup I have (why?!) connected 12v gnd and 5v gnd...

I have now removed that and sometimes is working better.
Tried with/without serial power from USB and also separate 9v power to arduino.

9v + 5v gnd are connected.
12v not connected.

Of course the power supplies are connected to the same mains power socket in wall. I can not touch "mains system". I am not switching 120-230v.
Just working with 5-12v.

I have now attached simplified sketches showing placing of components and how cables are running.
I did read what you wrote about placing 12v above the arduino.

Compare, and what do you think.
Maybe I shall give it a try to change the 2:nd setup to look similar to 1:st setup, where the relays are placed above Arduino.

In any of the setup, all cables are running to the layout's landscape ("roads"). So there should be no big difference there.

When I with my hand try to keep cables apart and/or make them cross like a +
it run 2-3 loops, then stops:

08:39:48.108 -> Ldh_BB1_V1.ino
08:39:48.108 -> F E L SÖ K N I N G Aktiv
08:39:48.168 ->
08:39:48.168 ->
08:39:48.168 -> LOOP IGEN
08:39:48.227 ->
08:39:48.227 ->
08:39:48.227 -> Växel: PÅ.
08:39:49.640 -> Vägst: PÅ.
08:39:51.148 -> H1: PÅ.
08:39:54.116 -> H1+vx: AV.
08:39:56.616 -> Växel: AV.
08:39:57.338 -> Vägst: AV.
08:39:57.338 -> H2: PÅ.
08:39:58.841 -> H2: AV.
08:40:00.343 ->
08:40:00.343 ->
08:40:00.343 -> LOOP IGEN
08:40:00.343 ->
08:40:00.343 ->
08:40:00.343 -> Växel: PÅ.
08:40:01.842 -> Vägst: PÅ.
08:40:03.353 -> H1: PÅ.
08:40:06.333 -> H1+vx: AV.
08:40:08.856 -> Växel: AV.
08:40:09.540 -> Vägst: AV.
08:40:09.540 -> H2: PÅ.
08:40:11.059 -> H2: AV.
08:40:12.564 ->
08:40:12.564 ->
08:40:12.564 -> LOOP IGEN
08:40:12.564 ->
08:40:12.564 ->
08:40:12.564 -> Växel: PÅ.
08:40:14.026 -> Vägst: PÅ.
08:40:15.537 -> H1: PÅ.
08:40:18.553 -> H1+vx: AV.
08:40:21.047 -> Växel: AV.
08:40:21.738 -> Vä
08:40:39.200 ->
08:40:39.200 ->
08:40:39.246 -> Ldh_BB1_V1.ino
08:40:39.246 -> F E L SÖ K N I N G Aktiv
08:40:39.292 ->
08:40:39.292 ->
08:40:39.292 -> LOOP IGEN
08:40:39.292 ->
08:40:39.292 ->
08:40:39.292 -> Växel: PÅ.
08:40:40.747 -> Vägst: PÅ.
08:40:42.254 -> H1: PÅ.
08:40:45.262 -> H1+vx: AV.
08:40:47.726 -> Växel: AV.
08:40:48.461 -> Vägst: AV.
08:40:48.461 ->
08:40:48.461 -> V
08:40:48.461 ->
08:40:48.461 -> LOOP IGEN
08:40:48.506 ->
08:40:48.506 ->
08:40:48.506 -> Växel: PÅ.
08:40:49.966 -> Vägst: PÅ.
08:40:51.451 -> H1: PÅ.
08:40:54.479 -> H1+vx: AV.
08:40:56.955 -> Växel: AV.
08:40:57.645 -> Vägst: AV.
08:40:57.692 -> H2: PÅ.
08:40:59.151 -> H2: AV.
08:41:00.647 ->
08:41:00.647 ->
08:41:00.647 -> LOOP IGEN
08:41:00.701 ->
08:41:00.701 ->
08:41:00.701 -> Växel: PÅ.
08:41:02.151 -> Vägst: PÅ.
08:41:03.649 -> H1: PÅ.
08:41:06.646 -> H1+vx: AV.
08:41:09.157 -> Växel: AV.
08:41:09.879 -> Vägst: AV.
08:41:09.d

The wires to/from the Arduino bundled with the 12V cables causes problems. If you can keep a gap between them, even if it is only 1cm that would be better.

Perhaps using shielded cable for the signals to/from the Arduino might help.

It is 12V DC ? then you should also dampen the spikes of the 12V with diodes. They are called flyback diodes. They can be put at the relay contacts but also at the load. To be sure you can put flyback diodes at both ends. They are used with an inductive load, but if your load is not inductive, then the wires and cables still are. Ordinary 1N4007 diodes are fine. Also LC filters might help. A snubber circuit over the contacts of the relays will also help.

Have you seen the pictures in the link for flyback diodes ? Maybe you think that your circuit does not have 300V spikes. But such things really happen. If you would know how much high frequency noise there is in a circuit, you would be amazed.
An other problem is the GND. Suppose that the GND pin of an Arduino is the real GND. If you add a piece of wire to it and bundle it with a noisy 12V signal or send some current through that GND wire, then 50 cm further from the real GND you don't have a good GND anymore.
Look at your circuit and image that there is a lot of noise and spikes going on, and then it is probably worse than you think :o

Koepel:
Look at your circuit and image that there is a lot of noise and spikes going on, and then it is probably worse than you think :o

Yep!

Just to start up testing. I have read the link, Thank you :slight_smile: Also tried to read on www,
but I do not feel sure how to connect flyback diode on 12v-side (the load)

The relay card I use: https://www.electrokit.com/produkt/relamodul-5v/
says to have transistor and diode, on the coil side. Check the link to pdf in webshop for mor info.

I am driving different 12v. coils /magnetic coils to halt cars or switch turnouts.
I have looked to find how to connect on the flyback diode.
Should it be like this sketch?

I do not have IN4007 home, but maybe some of those might work?

1N4148
1N4001
1N4933
1N5404
1N5819
?

I will now start to rerun some cables in the central. However the cables to the layout is not so easy to rerun, so I hope cleaning in the power distribution central and maybe some diodes might help?

I have a friend how suggested to
connect a 470uF between gnd and 12v and maybe that could help to?

EDIT 22:52:

Yes all components are 12vDC
"An other problem is the GND. Suppose that the GND pin of an Arduino is the real GND" - I do not understand what you mean with 'real ground'?

Short status update. 8)

I did som quick rearranging in the central. Of course that shall be cleaned up.

I now ran the simple program
for about 8 minutes.

The Arduino did "only" reset 2 times, so I say that is a step forward to solution.
The layout is more like the first car system I made - which works flawless.
The difference is that the Arduino is actually placed below the relays.

And NOW, writing this, the Arduino did hang up. So 3 times which of the last one did freeze the running if program. The first two hang ups, did reset the program. The Arduino won't restart so I have to press rst-button now.

This happpens when shutting of two 5 volt relays close to each other as far as I can figure out.

Now after resetting it did not even run through the loop once. Did another reset it ran 2 loops then every relay switched like a firework (brrrrp), then went back to normal, then made som new restart and so on. Ahhh... :confused:

The first car system I put shrinking tubes on the 5v vin soldering pin on relays. The guy who helped med building this central no. 2 has not done that. Maybe it is important for interference?