Go Down

Topic: Is this AVR sleep code correct? (Read 2910 times) previous topic - next topic

Danois90

I do not think that you can brick your arduino with sleep (please correct me if I'm wrong), but you can brick it by changing fuses - but I'm not sure that is possible on a nano.

Whenever I program a stand alone AVR/MCU, I dedicate a single digital input pin as sort of a "sleep aborter" by wiring a R10K from GND to PIN (not really nescessary). When I want to cancel any sleep, I rewire it to R1K from VIN to PIN. The code would be something like:

Code: [Select]
#define SLEEP_ABORT_PIN 5

void loop()
{
  if (digitalRead(SLEEP_ABORT_PIN) == HIGH) return; //Abort regular operation
  //Perform regular operation
}


This should ensure that you always have an escape route when things go bad :)
Instead of mocking what's wrong, teach what's right! ;)
When you get help, remember to thank the helper and give some karma!
Please, do NOT send me any private messages!!

DrAzzy

#16
Aug 29, 2018, 09:13 pm Last Edit: Aug 29, 2018, 09:18 pm by DrAzzy
Different battery packs have radically different behavior with regards to turning themselves off under low/no load.

Some will only turn off if the load is damned near zero, so waking up and blinking an led would be enough. Others require loads on the order of 100mA to stay awake (I have one that won't charge my Go-tcha (a fitbit-sized companion device for pokemon go - it turns off because charging the tiny battery doesn't draw enough current to keep it from turning off). And of course, this parameter is never spec'ed by the manufacturer.

You can't brick an arduino using sleep mode. You can nearly-soft-brick nano (except the "new bootloader" official ones manufactured this year) and pro mini with the WDT if it's used to generate a reset event (when this happens the WDT *stays on* after the reset, with the minimum timeout, and if the bootloader isn't smart enough to check for this and turn it off, it will reset-loop while the bootloader is running) - this can be recovered from by manually resetting to upload (IIRC even reset doesn't turn it off, so you need to power cycle it and hold reset until the right point in the upload process) or by rebootloading with an ISP programmer. It can be prevented entirely by bootloading them as an Uno and treating them as such within the IDE.

I dedicate a single digital input pin as sort of a "sleep aborter" by wiring a R10K from GND to PIN (not really nescessary)
Yes it is, otherwise the pin will float and reads will yield a random value. Better to set it input_pullup, and trigger the abort by grounding it.

But as noted above, it's not possible to brick a board with sleep mode. The reset pin will always bring it back.
ATTinyCore for x4/x5/x61/x7/x8/x41/1634/828/x313 megaTinyCore for the megaavr ATtinies - Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts, mosfets, awesome prototyping board in my store http://tindie.com/stores/DrAzzy

DKWatson

Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

Marciokoko

Thanks Dr Watson

Even with sleep idle the battery pack shuts down after 1.5 minutes

DKWatson

You don't think I've heard all the Sherlock jokes. One reason I keep my education quiet.

Seems like I've got myself a pretty useful battery pack, the only one in fact that hasn't given op the ghost. It goes into it's own power down mode while still maintaining a trickle current. Wakes up every couple of minutes for 12 seconds to see if anything is still attached and keeps on plugging. Seems to be branded for the local market (Indonesia) using the same name as the local beer, All I can say is I got it at Ace Hardware.
Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

Marciokoko

Oh no, sorry, autocorrect.

Well Ill keep trying with larger loads.  Its obvious that just the nano current draw even in normal mode isnt enough to keep it awake.  So Ill have to find something for it to do.

Thanks again.

DKWatson

If it needs a stronger current draw to keep it alive, it might as well come from the nano where you can control it. With exhaustive testing you'll discover the tipping point. Which of course will change as soon as you change power sources and then you can start the whole process all over again. Isn't this fun?
Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

Marciokoko

Well I tried leaving the led on the whole time but it turns off after 1.5 minutes just as well.

The thing is that if I'm gonna have to go much higher I'll be using up my battery pack which is what I don't want.

DKWatson

It may be time to think about a new battery pack.
Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

DKWatson

Did you try the code I posted in #13?
Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

Marciokoko

Yup.  Same result.  It falls asleep after 1.5 minutes.

DKWatson

Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

Marciokoko

yeah the thing is that I specifically wanted that solar charging battery pack so that I could leave the project out in the field and it would operate autonomously until a prolonged period of cloudiness or something like that.

DKWatson

Time for a different strategy. If the battery pack dies after 90 seconds of sleep, put Arduino to sleep for only 80 seconds. Then wake up and do something for 1, 2, 5 or 10 seconds, however long it takes to convince the pack to stay awake, then back to sleep for another 80. It may be that the pack is on it's own timer and checks every now and then to see if something is drawing power. After n negative responses, it shuts down. If Arduino happens to be sleeping when the pack checks.... So, keep Arduino awake long enough so it's seen by the battery pack, find out when then check is being done and the fine tune Arduino to ensure a wake-up call just before the last check.
Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

Marciokoko

Well here is the thing...the nano might be too small a load for the pack anyway because as I found out in some post above, the pack sleeps even if I don't put the nano to sleep

Go Up