Electron deep sleep not working after first power up

This is my minimal code:

SYSTEM_MODE(SEMI_AUTOMATIC);

void setup() {
    System.sleep(SLEEP_MODE_DEEP, 10);
}
void loop() {
}

I would expect that the electron goes to deep sleep, wakes every 10 seconds and sleeps again immediately. When I power the electron by plugging the battery in, this seems to work. Every 10 seconds I wakes up, the led flashes for a moment. But if i measure the current consumption, I can see that sleep doesn’t work properly, the current varies between ~70mA and ~15mA:
20181009_145547

Normal current should be 0.2mA in sleep and some higher current consumption in the short wake up time. I found several options to get this normal behaviour:

  1. Just wait. After some random time (from 20s to 5min) the current consumption is normal. The time varies with every try.
  2. Unplug the battery for a very short time and immediately connect it again.
  3. Hold MODE while reset, when led blinks, reset again without MODE.

The only option to retrigger this behaviour is to plug the battery in after having it disconnected long enough. Is this a bug? I would like to know the reason for this behaviour to find a reliable solution.

Thank you!

Yes, that won’t work right. The problem is in SEMI_AUTOMATIC mode on cold boot the modem is somewhat powered up, not fully powered and operational, but not asleep either.

Putting the modem into SLEEP_MODE_DEEP requires that the modem be on in order to send the AT commands to it, which is why you’re not going into deep sleep.

The workarounds are listed here:

1 Like

Thank you for the fast response, that solves my problem!

My modified and now working code based on the linked examples looks like that:

SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);
STARTUP(System.enableFeature(FEATURE_RESET_INFO));

void setup() {
    if(System.resetReason() != RESET_REASON_POWER_MANAGEMENT) {
        Cellular.on();
        Cellular.off();
    }
    System.sleep(SLEEP_MODE_DEEP, 10);
}

void loop() {
}

When waking up after deep sleep, the resetReason is RESET_REASON_POWER_MANAGEMENT. On initial power up after connecting the battery, the resetReason is RESET_REASON_POWER_DOWN.

Be a little careful with that sequence in SYSTEM_THREAD(ENABLED) mode. Since Cellular.on() is non-blocking there’s no way to tell when it’s done. It’s best to insert a delay of a couple seconds after that. Since it will only be done on cold boot it won’t happen often.

Or you can use cellular_on(NULL). That is blocking.

Cellular.off() isn’t as much of a problem as the System.sleep() will block until the Cellular.off() call is complete I think.

1 Like

Thank you for the note.

I changed it to

cellular_on(NULL);
cellular_off(NULL);

and it works fine.