Esp32 Power management

For general Flowcode discussion that does not belong in the other sections.
Oscar_T
Posts: 89
http://meble-kuchenne.info.pl
Joined: Wed Dec 02, 2020 10:06 am
Location: Italy
Has thanked: 37 times
Been thanked: 10 times

Esp32 Power management

Post by Oscar_T »

Hi

I'm trying to figure out how to be able to activate the various energy modes of esp32.

Could you kindly give some examples and indicate which libraries to include to activate the various modes and how to get out of the low-power modes?

Kind regards

BenR
Matrix Staff
Posts: 1739
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 440 times
Been thanked: 603 times

Re: Esp32 Power management

Post by BenR »

Hello,

This page should be useful, just call the required C calls using a C icon.

https://docs.espressif.com/projects/esp ... modes.html

MJU20
Posts: 244
Joined: Tue Dec 08, 2020 5:11 pm
Has thanked: 75 times
Been thanked: 50 times

Re: Esp32 Power management

Post by MJU20 »

Hey Oscar_T,

Did you manage to put the ESP32 into power down mode?
I want to reduce power by putting the ESP into some kind of sleep mode and return to wake every few minutes.

I don't know what's best: maintain the WIFI connection:
If WiFi connection needs to be maintained, enable WiFi modem sleep, and enable automatic light sleep feature (see Power Management APIs). This will allow the system to wake up from sleep automatically when required by WiFi driver, thereby maintaining connection to the AP.
or go into it's deepest sleep and wake up and connect to wifi every time again..

Any experience?

Oscar_T
Posts: 89
Joined: Wed Dec 02, 2020 10:06 am
Location: Italy
Has thanked: 37 times
Been thanked: 10 times

Re: Esp32 Power management

Post by Oscar_T »

Hi Mju20
Honestly, I spent a few days doing some rehearsal but I didn't get great results. Then I left it and turned to something else

mnfisher
Valued Contributor
Posts: 955
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 508 times

Re: Esp32 Power management

Post by mnfisher »

A very simple example of light sleep with a timer...
sleep.fcfx
(9.74 KiB) Downloaded 133 times
Here the esp32 sleeps for 5 seconds before resuming output to the UART - experiment with other esp_sleep_enable_... functions to suit.
Note - this is only intended as a sample - it would probably be better to move the import of esp_sleep.h to supplementary code..
Power consumption (using a USB power tester - so not very accurate) falls from 0.005A to 0.00)

Note that deep sleep is different - here the MCU is actually reset on waking - so will go back to the start of the 'main' function on wake.
There is a way to save some state in the RTC AP memory - and, for example, use a flag to indicate that the program has just restarted. I'll have a go at getting this to work..

So - if you use

Code: Select all

esp_deep_sleep(5 * 1000 * 1000);
To enable a 5s deep sleep - you notice that the output to UART is 'Sleep in 1s' 6s pause 'Sleep in 1s' etc

Martin

MJU20
Posts: 244
Joined: Tue Dec 08, 2020 5:11 pm
Has thanked: 75 times
Been thanked: 50 times

Re: Esp32 Power management

Post by MJU20 »

Magnificent Martin! Thanks!

I've altered it so I can get feedback with the onboard LED.
I can see the current from the ESP32S Wroom drop form 30mA to 0mA.

My goal is to get the ESP in as low-power-mode as possible to get an ESP working on a 18650 battery as long as possible.
But I'm not sure which mode is best to maintain connection with WIFI.

The modes where WIFI connection is maintained would be best, but maybe this consumes more power.
I don't know if a router appreciates it if a device (ESP32) connects every minute and loses it's connection after sending data?

Note to myself.. Need to test if the RAM is kept during sleep.

Thanks Martin!

MJU20
Posts: 244
Joined: Tue Dec 08, 2020 5:11 pm
Has thanked: 75 times
Been thanked: 50 times

Re: Esp32 Power management

Post by MJU20 »

Update to whom it may concern :-) (And otherwise for me to remember these values :) )

I've put the ESP32S into the normal sleep and it draws 14mA (measured in the USB line that powers it).
This includes the power-on-LED.
The RAM is maintained in this light sleepmode and it returns back to the point where it ended.

Code: Select all

esp_light_sleep_start();
12mA in deep sleep mode and 53mA when I wake it up and make the onboard LED flash.
Again with the power LED on.

Code: Select all

esp_deep_sleep(15 * 1000 * 1000);
In this mode it returns back to the start of the main of the program.

Need to find out how much power the module uses with the WIFI enabled, maybe the Bluetooth radio..

To do: order some more boards and desolder the powerled.
More testing...

mnfisher
Valued Contributor
Posts: 955
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 508 times

Re: Esp32 Power management

Post by mnfisher »

It's interesting - the wifi is going to use some power - but a connection will not be maintained in either light or deep sleep.
WiFi/BT and sleep modes
In deep sleep and light sleep modes, wireless peripherals are powered down. Before entering deep sleep or light sleep modes, applications must disable WiFi and BT using appropriate calls (esp_bluedroid_disable(), esp_bt_controller_disable(), esp_wifi_stop()). WiFi and BT connections will not be maintained in deep sleep or light sleep, even if these functions are not called.
So I guess for maximum power saving the connection needs to be made on each power on..

An alternate power save route is to reduce the clock speed... (see https://docs.espressif.com/projects/esp ... ement.html)

Anyway - I tried maintaining some RAM.. Here just two values - a flag which is set to 123 when the program first runs and a restart counter that is initially set to 0 and is incremented by 1 each time the MCU resets. Note that the output to UART display this count and also all the 'debug' info that the esp32 puts out on restart (this can probably be turned off in config?)

Here I just enter deep sleep for 1s and restart 10 times before the program terminates...

The RTC memory is at address 0x50000000 (and there is 8k (words?) available) - RTC_SLOW_MEM is defined in supplementary code and can be used like an 'array' in a C block.
deepsleep.fcfx
(11.58 KiB) Downloaded 132 times
Martin

MJU20
Posts: 244
Joined: Tue Dec 08, 2020 5:11 pm
Has thanked: 75 times
Been thanked: 50 times

Re: Esp32 Power management

Post by MJU20 »

Hey Martin,

By testing it I got to know why this workaround.

If the ESP reboots after sleep, the whole RAM is cleared, and you found a way to store a value even if the module reboots.
NICE!!!

I guess that was your purpose?

I'm not looking to have the module working for years on 1 battery charge, but it would be nice to have it running every 10 minutes with one battery charge (I think I will use a 18650), and that for a week or so.
This will work I guess if I can reduce power and starting by removing the power-on-led.
If other tricks can be applied without making the program too complicated, I will try this too.

For me, this trick by storing the value in a RAM that is maintained is very very nice.
This means that I can sample a value and store it, go to sleep for 10 minutes, resample the value and only if the values are not the same: start WIFI, connect.... go to sleep... This means that I don't have to use the WIFI unnecessary. (and safe power).

Thanks again and keep the tips coming :-)

I need to test the power consumption with WIFI enabled.

mnfisher
Valued Contributor
Posts: 955
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 508 times

Re: Esp32 Power management

Post by mnfisher »

There is a lot of power 'hidden' in the esp32.... For example it is possible to write a program for the low power processor that reads a sensor and only wakes the 'main' MCU if the data has changed.

I found https://blog.voneicken.com/2018/lp-wifi-esp32-1/ - which looks at some techniques for maximizing battery life by speeding up wi-fi connection. I think the techniques should be applicable in Flowcode.

As an aside - I was given a weather station kit for Christmas (just the sensors) - and spent some time trying to get it working with an esp32 - the anemometer worked well - but the wind direction I struggled with - the sensor has 16 possible resistance values and by using a second known resistor can be used as part of voltage divider..
However - all my attempts to correctly read the voltage correctly failed. By varying the value of the known resistor - I was able to check ~12/16 directions correctly. I'd put it aside - other things needing time - and planned to return (it's currently way too windy here to risk it outside) - but if anyone has any advice/experience of this?

I'd also struggled to work a way to get a 'web' page displaying current conditions - if the esp32 is 'sleeping' then an error is displayed.

I'd also almost resorted to using an ATTiiny and nrf24l01 which can wake the MCU when a data request is received. However, the bits you've posted here (and other topics) have encouraged me to try again with the esp32 and a web 'interface'.

Martin

Post Reply