Tracking Millis With Esp8266 Deep Sleep Mode

how to keep track of millis esp8266 deep sleep

The ESP8266’s Deep Sleep mode is a useful feature for conserving power, but it also resets the module. This means that the millis() function, which counts the number of milliseconds since the program commenced, is reset as well. This can be a problem for applications that need to keep track of the time the application has been running, such as a stopwatch, timer, or alarm. To overcome this issue, one suggestion is to use the SLEEP_MODE_IDLE, which keeps Timer0 running, or the Watchdog Timer to count the time spent in sleep mode and add it to millis() upon waking up. Another option is to use the timer2 asynchronous mode with a 32kHz crystal connected to the TOSC pins, allowing for accurate timekeeping within a minute per year after calibration. For low-power projects, the ESP32 board offers more deep sleep modes and wake-up sources.

shunsleep

Using the Watchdog Timer

The Watchdog Timer can be used to keep track of the time spent in sleep mode and add it to the millis() when the device wakes up. However, the Watchdog Timer is known to be highly inaccurate, so it is not a reliable method for timekeeping.

One way to work around this limitation is to use the SLEEP_MODE_IDLE, which keeps Timer0 running. This means that the device will wake up every millisecond, which may not be desirable in certain applications.

Another approach is to switch to Timer 2, which allows for asynchronous mode and connection to a 32kHz crystal to the TOSC pins. This enables the use of Power-save sleep mode, providing more flexibility.

Additionally, consider using an external RTC (Real-Time Clock) if accurate time tracking is crucial. This method was suggested in a GitHub thread and can provide a more precise account of time passage during sleep.

In summary, while the Watchdog Timer can be utilized for tracking millis during sleep mode, its inaccuracy may pose challenges. Alternative methods, such as SLEEP_MODE_IDLE, Timer 2 with external crystals, or an external RTC, can offer more accurate and reliable timekeeping solutions.

Fitbit Versa 3: Sleep Tracking and More

You may want to see also

shunsleep

SLEEP_MODE_IDLE

However, using SLEEP_MODE_IDLE means that for every millisecond, an interrupt wakes up the device. This can be inefficient, especially if your loop takes a long time to execute. For example, if your loop takes 60ms to execute, sleeping for 1ms at a time will not offer much advantage in terms of power savings.

In this case, it may be more beneficial to use the Watchdog Timer to count the time spent in sleep mode and add this to millis() upon waking up. While the Watchdog Timer is inaccurate for timekeeping, it can still be useful for counting time spent in sleep mode.

Additionally, if you are using alarms in your application, you will likely need to use an RTC module to ensure accurate timekeeping.

It is important to note that the ESP8266's Deep Sleep mode is designed to conserve power by cutting activities that consume more energy, such as Wi-Fi, while keeping essential functions active to wake up the processor when needed. This can significantly extend battery life, especially in low-power projects.

To enable Deep Sleep mode, you can use ESP.deepSleep(uS) and pass the sleep time in microseconds as an argument. To wake up the ESP8266, you can use either Automatic Wake-Up (Timer Wake-Up) or External Wake-Up. The former automatically wakes the device after the predefined sleep time has elapsed, while the latter requires pressing the RST button or applying a momentary LOW signal to the RST Pin.

shunsleep

RTC modules

The ESP8266's Deep Sleep mode is useful for conserving power, but it resets the module, including the millis() counter. While you can store things in the "RTC" memory, it is not a true RTC and is highly dependent on the temperature of the chip.

If you are looking for accurate timekeeping, you will need an external RTC module. The DS3231 is a low-cost, accurate I2C real-time clock (RTC) module that works well at 2.6V. It is more power-efficient than the DS1307, which requires at least 3.3V. The DS3231 maintains the RTC to within ±2 minutes per year accuracy from -40°C to +85°C.

To save power, you can use a transistor connected to GPIO to turn off the Vcc power supply on the DS3231 module. Don't forget to turn off the I2C pull-ups, as they can provide parasite power to the RTC. You can also simply disconnect GND. If restarted with WiFi off, the ESP8266 only needs to wake for about 200 ms, consuming 20 mA to check the DS3231 if it's time to wake up fully.

Another option is to use the timer2 asynchronous mode and connect a 32kHz crystal to the TOSC pins. Then you can use the Power-save sleep mode. This requires some work to set up the code, but after calibration, it can be accurate to within a minute per year.

The ESP32 also offers more deep sleep modes and wake-up sources, and it has been shown to have similar electricity consumption to the ESP8266.

shunsleep

Deep sleep mode

ESP8266's Deep Sleep mode is a useful feature for conserving power. However, it resets the module, including the millis() counter, which counts the number of milliseconds since the program commenced. This can be problematic for applications that require accurate timekeeping, such as a stopwatch, timer, or alarm.

To keep track of millis during Deep Sleep mode, you can use the SLEEP_MODE_IDLE, which keeps Timer0 running. However, this may not be advantageous as it wakes up the device for every millisecond. Another option is to use the Watchdog Timer to count the time during sleep mode and add this to millis() upon waking up. Alternatively, you can use the timer2 asynchronous mode with a 32kHz crystal connected to the TOSC pins, but this requires additional setup and calibration.

If you are using a battery-powered device, Deep Sleep mode can significantly extend the battery life of your project. However, when the ESP8266 wakes up from Deep Sleep, it draws a considerable amount of current, which may offset the power savings. The optimal time interval for Deep Sleep depends on various factors, including power optimization techniques and the specific use case.

To measure the current consumption in Deep Sleep mode, you can use a multimeter to determine how much power the board is draining. This can help you assess the effectiveness of Deep Sleep in conserving power for your specific application.

Overall, ESP8266's Deep Sleep mode is a valuable feature for power-saving, but it requires careful consideration of the trade-offs between current consumption during sleep and wake-up, as well as the need for accurate timekeeping in certain applications.

shunsleep

Power optimisation

However, it's important to note that the ESP8266 won't execute any code during Deep Sleep. This can be a challenge if you need to keep track of the time or date when the module wakes up from sleep. One solution is to use the RTC (Real-Time Clock) memory, which survives the reset, but it's not a true RTC and can be affected by temperature drift.

To optimise power consumption further, consider using efficient algorithms and minimising unnecessary computations. Additionally, efficient wake-up triggers, such as timed intervals or external events, can help prolong battery life. For example, you can use the Power-save sleep mode, which is ideal when you don't need radio communications or when you're waiting for external events like sensor inputs.

Another strategy is to strike a balance between responsiveness and power consumption by optimising the ESP8266's power modes and fine-tuning sleep durations. This can be particularly useful for battery-powered IoT devices, such as smart door sensors or wearable fitness trackers, where energy efficiency is critical for extended usage.

Furthermore, if space is not a constraint, consider using the SLEEP_MODE_IDLE to keep millis() counting while in sleep mode. This mode keeps Timer0 running, but it also means that an interrupt wakes up the device for every millisecond, which may not be advantageous in terms of power savings.

By following these power optimisation techniques, you can enhance the energy efficiency of your ESP8266 projects, making them more sustainable and environmentally friendly.

Frequently asked questions

ESP8266's DeepSleep mode is useful for conserving power as it keeps everything off except the Real Time Clock (RTC), which is how the ESP8266 keeps track of time.

In sleep mode, Timer0 is disabled, which means that millis() stops counting. To keep track of time, you can use the Watchdog Timer to count how much time has passed while in sleep mode and add this to millis() when waking up.

DeepSleep mode is a power-saving feature that resets the module and requires a significant amount of current to wake up from. Light Sleep mode, on the other hand, allows for lower power consumption while still being able to wake up with other GPIOs.

To measure the current consumption of ESP8266 in Deep Sleep mode, you can use a multimeter to measure the current when the board is in this mode. This will help you understand how much power is being drained during Deep Sleep.

Written by
Reviewed by

Explore related products

Share this post
Print
Did this article help you?

Leave a comment