Sleep mode questions

@rickkas7
Hello Rick. Thank you for posting this, it was quite helpful. But i need a little bit of help. None of these modes seems to suite my particular application:
I will be operating my photon in manual mode only.
I would like to put my photon into deep sleep mode for a specified amount of time and then it automatically wake up (without the need of a dedicated external input trigger signal to wake up).
Whilst in deep sleep mode i would like for it to still run the code associated with an interrupt i have programmed to trigger whenever a reed switch closes (which i am using to measure wind speed). Or even if it were to wake up to execute the code in the interrupt and than return to sleep mode that will be fine as well.
Once the time has elapsed i would like the photon to wake up from deep sleep and continue as normal, i.e not start from void setup again.

Aside from,

System.sleep(SLEEP_MODE_DEEP, 30)

I did come across a few different sleep commands in the particle docs and the,

System.sleep(uint16_t wakeUpPin, uint16_t edgeTriggerMode, long seconds)

seems to be the next best for my purpose. However i am unclear if it will acknowledge the use of my interrupt and since i am not using an external trigger to wake the photon, if it will work. Please advise. Thanks in advance.

@Alli, the way deep sleep works is dictated by the design of the STM32F205 MCU in the Photon. By design, deep sleep (aka STANDBY sleep on the STM32) will ONLY wake with a rising edge on the WKP (wakeup) pin of the Photon and no other pin will work. It will also wake on an alarm condition from the RTC so you can set a time to wake. In addition, because this sleep mode turns off so much of the processor, waking does a RESET of the processor upon waking.

The only sleep mode that will work for your requirements is STOP sleep (using System.sleep(uint16_t wakeUpPin, uint16_t edgeTriggerMode, long seconds)) where you can wake on any interrupt including a specified time but current consumption is not the lowest. If you wake on the interrupts of a wind speed indicator, the resulting interrupts may not yield good power savings due to their frequency. However, it is possible to create loop() code that goes to STOP sleep mode where the ISR is serviced and the loop code goes right back to STOP sleep until a suitable condition is met (data accumulated over desired interval) after which is can process that condition and go back to sleep. Since DeviceOS 1.0.0 now allows your code to test the reason for waking (timeout vs interrrupt), when a timeout occurs you could turn on WiFi, connect to the cloud and publish your data, turn WiFi off and go back to sleep.

So, for power consumption it is not the best but it is the only option you have for your requirements and using only the Photon (ie. no secondary processor to do the windspeed data gathering).

To clarify: There is no way to run your interrupt code whilst your device is in either actual sleep mode (neither Standby Mode nor Stop Mode).
Either the device sleeps or it is awake and will attend to your code, but not both at the same time.

@peekay123 @ScruffR

Thank you guys for the advice. i will play around with this the STOP mode and see if it works well for me.

1 Like

@peekay123 @ScruffR

Is there a way to put my photon into deep sleep for less than 1 second using the

System.sleep(uint16_t wakeUpPin, uint16_t edgeTriggerMode, long seconds)

command or similar?

That command does not deep sleep but stop sleep and no there is no easily accessable command for sub-second sleep.
What do you intend with such a short sleep phase?
Are you intending to have your device connected before and after the sleep phase?

@ScruffR

I intend to put the photon to sleep every 100ms or so (still trying to find a suitable period) to run the Blynk.run commands and every 10 100ms (1 second) i will run the main functions of my program. I am not sure if

BlockquoteSystem.sleep(uint16_t wakeUpPin, uint16_t edgeTriggerMode, long seconds)

will work since the time condition is defined in seconds? Is there a way to put the photon into a deep sleep or to STOP for a period less than a second?

Since a WiFi reconnect would usually take more than a second sleeping the device for that short of a period doesn’t seem to be viable approach.

You explain what you want to do, but not why you want to sleep the device in the first place.

1 Like

@ScruffR

Sorry. Let me clarify. I will be using the device in manual mode so the issue of reconnecting to the wifi or to the particle cloud is not a concern. Communication of data will be between my photon and a Blynk application via Bluetooth. My design only requires an update of data every second, in the most frequent case, but to keep Blynk running i need to execute the Blynk.run command more often than every second. I am using trial an error to determine what the lowest frequency is that this command needs to be executed before i have connection issues with Blynk. This frequency will almost definitely be in the millisecond range. Arbitrarily i am stating a 100ms for now. In between this time and the time of execution of my main code, i do not need the photon to run any other code. Hence, i would like to stop code execution (aside from the running of time for my timers) or place the photon into deep sleep for that duration. This will dramatically reduce the overall power consumption of my project and increase the working hours i can get from my battery.

During sleep there are not timers running on the Photon.
The interrupt for timed wake is produced by the RTC which only has 1sec resolution IIRC.

But when you are talking about BT/BLE you may like to jump ship to the Argon which has onboard BT/BLE capabilities and should also feature real-time-counter(s) which should still run during sleep phases (I haven't really looked into these yet tho').

@ScruffR
Is there a way to put my photon into any sort of sleep mode or power saving mode (aside from manual mode) for a time less than a second? The only timer i need running is to wake up the photon from sleep mode or power saving mode every X milliseconds. I have structured my code to only require that timer

void AllSensors(){                                    //runs every 100 ms after the photon is woken up                         
 
    Blynk.run();                                            //Maintain connection to Blynk 
    timer1.run();                                           //Keep timer1 running
    Counter=Counter+1;
    
    if(Counter==10){                                    //Runs every 1 second
    
        GetBME280SensorData();
        Windsensor();
        GetVEML7700SensorData();
        DisplayOnSerialMonitor();
        Counter=0;
    
    }
    
    if(PowerSave==1){                                                     //When my wind speed sensor is disconnected then run 
                                                                            power save mode 
        
            //PUT PHOTON IN SLEEP MODE AND TO WAKE UP IN 100 MILLISECONDS FROM NOW
    
     }                                                                                                                                                                          
    }

Would this be possible?

But when you are talking about BT/BLE you may like to jump ship to the Argon which has onboard BT/BLE capabilities and should also feature real-time-counter(s) which should still run during sleep phases (I haven’t really looked into these yet tho’).

I would but it not available locally and will take some time to ship. I am stuck with the photon for now.

As I said, internal timers are not (!) running when in any sleep mode.

The only way I can see the Photon sleep for a reliable sub-second period would be with an external timer that will wake the device via a pin interrupt.

Then i think i miss understood what Peekay was saying here. I was of the understanding that the photon could be powered down for a period, which is timed by the photon itself, before it powers up again. All i need is for the photon to wake itself up after some milliseconds has passed and i already have an idea of how to implement the rest.

That is no contradiction to what I said.
The RTC (real time clock) can wake the device but since ...

... "exact" sub-second sleep periodes are out of question.

2 Likes

Ah i see, thanks @ScruffR. Is there any power down mode that is capable of sub-second periods?
My challenge is running Blynk. Members of the Blynk community were reluctant to give me a minumum frequency that the Blynk.run command needs to executed for except to say that it needs to be executed as frequently as possible before connections errors are experienced. As a result i am forced to wake up the photon more frequently in a second in order to keep the photon connected to Blynk. If there is no way to achieve sub-second period than it does not seem like i will be able to put my program into a power down mode if i am using Blynk

What do you guess, when reading this?

Okay let me rephrase then, is there any way of manually “switching off” elements of the photon, aside from the wifi module which i already have done, to keep the photon running in a state where timers can run in sub-second periods whilst still reducing power consumption of the photon?

Not without severe hacking and “unpredictable” side effects.
There is a thread somewhere in this forum that experiemented with underclocking the device, but the reduction in power consumption isn’t that great to outway the potential implications IMO.

@ScruffR
Okay look like i will have to figure something else out. Thank you once again for your quick responses and advice :+1:t2:

1 Like

This “Low Power Timer Breakout” will allow as short as 100ms: https://www.adafruit.com/product/3435