Sketch resumption after power loss

I am using the Arduino WiFi R2 as a garden sensor to transmit temperature and moisture readings to my home WiFi network and display on my PC. The power for the Arduino is supplied by a small solar arrangement (panel + controller). The sketch and solar arrangement work perfectly. My problem concerns sketch resumption after power loss.
If I have the arduino connected directly to my PC via a USB cable, everything works fine. If I unplug the cable and then reconnect it, the sketch resumes no problem. However, when in the field and power is lost (due to the solar controller cutting power to prevent battery over-discharge), when the battery recharges and the controller resumes power to the Arduino (power on LED comes on), the sketch does NOT resume. I cannot understand why this happens with the solar set up but not with PC power! I can't even investigate this because obviously I can't use the serial port to see what is going on. Am I missing something here? Any ideas gratefully received!

1 Like

The details that would allow someone to understand the problem, and offer informed help.

Please explain what you mean by "resume". After power loss, the Arduino boots up and starts over.

If it doesn't reboot, then there is a power supply problem. Or, you have something in the code like this line, which waits for the serial monitor to connect, and hangs when not connected to a PC.

while (!Serial);

4 Likes

You can investigate on this problem in multiple ways

using a USB-cable where the +5V is disconnected. ground must stay connected

using an extra TTL-to-USB-converter

using one or mutliple LED that are switched on at different places in your code

answering the question:
when does your solar powered system switch power on again?
as soon as the battery is 0,01V above a minimum voltage that is pretty low
or
if battery is loaded 20%?

You could use a deep-sleep-power-optimised ESP32 stand-alone.
The average ESP32 will draw 5 to 15 mA in deepsleep.
But some manufacturers optimised their board for minimum deep-sleep current of
0,015 mA = 15 microampere

Then you would put this stand-alone ESP32 into deep-sleep
wake up once every 10 minutes send a ESP-NOW message which will only take 2 seconds and fall back into deepsleep again

Establishing a WiFi-connection takes 5 to 30 seconds

best regards Stefan

If I have the arduino connected to the PC with the sketch uploaded and running, and then I disconnect the USB cable, obviously everything stops. If I then reconnect the USB cable, the sketch starts running again without any action on my part. Although, I'm not sure where exactly which line the sketch resumes from. Thanks

Thanks Stefan, some great ideas here! I guess the USB cable without the 5V will allow me to run the serial monitor without supplying power to the arduino? I'm not sure what voltage the controller resumes battery discharge. This is something I'll investigate. I'll investigate your other solutions too. JRemington's thought regarding the possibilty of a code line which requires the USB connection also has me thinking. The version of the code which I use in the field is identical to the version I use when using the PC except that all code lines referencing the serail monitor are rem'ed out. I wonder if there is someting else which might cause a problem? Thanks again

From the very beginning, if the USB cable is supplying all the power.

@ian7511
I looked up your profile. You joined the forum 3 years ago.
You posted a few questions.

As soon as you post your complete sketch where complete sketch means from the very first to the very last line of code
your problems can be solved much faster.

This depends on what voltage does your solar-charger reconnect your Arduino Uno WiFi Rev 2.

Usually on powerup there are high current-spikes where this spike exceeds the averaged current that you can measure with a digital multimeter 5 or ten times.

And then a lot of additional factors come into play.

  • What wire diameter are you using for supplying the Uni WiFi?
  • How long are these wires?
    both factors result in the wire-resistance which causes a voltage-drop.
    The voltagedrop on the current. Low current low voltage-drop. High current high voltage-drop
    This is the reason why adding a 4700 µF capacitor as near as possible to the Uni WIFi 2 - where near as possible means a single inch -
    This big capacitor can help stabilising power.

Though I don't know if this is what causes your code to hang.

I want to encourage you again to change to a ESP32-nodeMCU board.
Even any kind of ESP32-nodeMCU board goes below your "delay"-current of 90 mA.
With Wifi off they use 50 to 50 mA. And in deeplsleep mode only 15 mA

And the ones from EZ-SBC as mentioned above go down to
90 mA / 0.015 mA = 6000 times less current consumption in your 30 minutes sleeping interval. Which means battery almost lasts 6000 times longer.

What is it that you measure with your device in the garden?
What exact types of components / sensors are you using?

Programming is almost the ESP32 is almost the same except the WiFi-part.

You should use a precise voltage adjustable power-supply to investigate how the board behaves if you powerup on 3.0V, 3.1V, 3.2V, ......

You should measure at what voltage does your charger connect the device to the battery

best regards Stefan

1 Like

Hi Stefan,
I'm trying to investigate using the suggestions you made. How can I use the serial monitor to follow what is happening in the sketch? I have connected the solar system to the Vin pin so I can leave the arduino USB socket free (the solar controller normally uses a USB cable connection to supply power to the arduino). I then connected a usb cable (+5V disconnected) from the Arduino to my PC, but how do I use it? I couldn't upload the sketch (device not found) or select a COM port (grayed out).
One interesting observation: I was monitoring the voltage across the USB output from the solar controller (still connected directly to the arduino at this point, so supplying power) and was surprised to find the voltage at 3.7V. I assumed that output from the controller would be either 5V (battery on) or zero (battery off). Even more surprising was that if I disconnected the USB cable between controller and arduino and then reconnected it, the voltage across the USB rose to 5V and the sketch resumed.
Thanks again for your help. It is much appreciated!

Your description is still not precise enough to be sure what you have really done.
Can you please hand-draw sketchy multiple schematics what is connected to what.

And then draw each situation that you measured in a extra-drawing where each drawing shows what is connected and what is disconnected to make things absolutely clear
And draw in from exact between which points you measured what voltage.

It makes CAN make a difference even measuring ground between battery, solar-charger-output, and GND on the arduino.

For measuring plus-pole this is even more valid

You seem to have not much experience and knowledge about electronics so it is even more important to clear each and every detail very very carefully.

what does "controller" mean? This is a too generalised word to be specific
Do you mean:

  • micro-controller?
  • solarcharge-controller?
  • undervoltage-disconnect controller?

is a somehow not very common wording of what is happening.

If you connect power to your arduino the arduino

  • is booting
  • executes function setup() one time
  • then goes on executing the code inside function loop() infinitely

as a short description: "the arduino booted and code runs"

best regards Stefan

OK, will do. How do I attach a drawing to my reply?

take a smartphone picture. Store smarthone-picture on your computer
copy picture into the clip-board
paste clipboard-content into the textfield - and voilà there it is

OK, here goes:

line 1 shows the normal 'in-field' set up where the power is supplied by the solar set-up and the Arduino sends data via wi-fi and a 'client.connect()' in the sketch. Works great until the battery is discharged. Sketch does not resume even when battery is fully re-charged, but a momentary disconnection then reconnection of USB 2 (see diagram) causes the sketch to resume normally.

Line 2 shows the set-up used to troubleshoot/develop the sketch using the PC

Line 3 shows how I tested the voltage across USB and found an intermediate voltage (3.7V) whereas I expected to find either 5V or 0V

Line 4 shows the set-up I used to enable me to connect the Arduino to the PC using a modified USB cable (as per your suggestion - +5V wire cut, but gnd and data intact). Power was supplied from the solar system using a regulated 5V output pin to connect to the Arduino Vin pin. The arduino ran fine, but the PC did not "see" the Arduino.

Hope this is clearer!

Hi Stefan, I realized I made 1 dumb error on my drawing. There is no USB 1. The battery is just connected directly to the screw terminals on the DFR0559 solar controller board.
Thanks

If you connect the V-in-pin to 5V this does not work.

The V_in input is before the 5V voltage-regulator.
On the V_in-pin the voltage must be at least 7V. The voltage-regulator needs a higher inputvoltage to be able to regulate down.

The 5V-input can handle only 5V

You should carefully measure the voltage between GND that is connected with your arduino-Wifi-R2-Board and the wire you want to use to power your arduino.

If this wire REALLY has 5V you can power the arduino over the 5V pin.
But in this case you have to make sure that it is 5V and it is the ONLY power-supply.

I should have realized that. The system did operate OK, but shut down well before the battery reached the over discharge protection voltage (2.4V) of the DFR0559 solar controller. I've switched to the 5V pin and all is functioning. The DFR0559 supplies a regulated 5V output. I'll allow the battery to discharge and see at what voltage the arduino shuts down.

I'm using the the set up as shown in line 4 of my drawing, with power being supplied to the arduino 5V pin directly from the DFR0559 solar controller. This leaves the USB socket on the arduino free so I can connect to the PC using the modified USB cable with the +5V wire cut. Could you explain how I can use this cable to run the serial monitor. I think it would be a huge help in investigating this problem if I can use the serial monitor to follow what happens in the sketch at the point of battery cut off and restart. Thanks

It should just work if still the three other wires

  • data+
  • data-
  • GND
    are connected.

Another option would be to use an external TTL to serial adapter
I have never used an arduino Uno Wifi R2 and I never will because the ESP32 can do all the same as an arduino Uno WiFi.

Another option would be to use UDP-messages but I hav zero experience how to code UDP-messags for Uno WiFi-R2

This didn't work. I made sure only the +5V (red) wire was cut. I did the following, using the setup in line 4 of my drawing:

  1. Firstly I disconnected the solar power supply and connected the Arduino directly to the PC using a normal USB cable.

  2. I then uploaded my sketch and made sure it was functioning and the serial monitor was working OK.

  3. I then disconnected the USB cable from the Arduino and reconnected the solar power with the Arduino being fed directly to the 5V pin. The Arduino and sketch resumed working normally.

  4. I then plugged the modified USB cable into the Arduino USB socket and connected it to the PC. With the sketch still open in the IDE I tried to open the serial monitor, but I just got a "device not found" message. I also opened the tools menu on the IDE to check the COM port. It was still set correctly, but grayed out.

Clearly the PC was not "seeing" the Arduino. Any thought? Thanks

Hi Stefan, thanks for sticking with me on this. I really appreciate your help. I have some more information:

  1. With my Arduino powered by the solay system and battery charged, the sketch runs and sends data normally to my wifi network.

  2. If I cover the solar panel and allow the battery to discharge, the battery is shut off by the solar controller at around 2.7V and the arduino shuts down.

  3. If I then allow the solar panel to re-charge the battery, the solar controller resumes the 5V supply to the arduino, but the sketch does not resume.

  4. If I press the reset button on the arduino, the sketch restarts and resumes sending data,

  5. Why does the sketch not resume when the battery reactivates the arduino? Is there any way I can make this happen without pressing the reset button?

I have looked at the ESP32 and you are right. This is probably the way to go, but at the moment I'm heavily invested in the arduino wifi so I like tsolve this problem before I move on.
Thanks again,
Ian

Hi, @ian7511

When the 5V is switched back on, is it a soft start or an immediate step up from 0V to 5V.
If a slow start, the reset circuitry may not respond and not be able to send a reset level to the controller.

Tom.. :grinning: :+1: :coffee: :australia: