Skip to content

Commit b8313f6

Browse files
committed
ESP32: Add DeepSleep examples
*Added a Timer wakeup example *Added a Touch wakeup example *Added an external source wakeup example
1 parent cecfdf3 commit b8313f6

File tree

3 files changed

+268
-0
lines changed

3 files changed

+268
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Deep Sleep with External Wake Up
3+
=====================================
4+
This code displays how to use deep sleep with
5+
an external trigger as a wake up source and how
6+
to store data in RTC memory to use it over reboots
7+
8+
This code is under Public Domain License.
9+
10+
Hardware Connections
11+
======================
12+
Push Button to GPIO 33 pulled down with a 10K Ohm
13+
resistor
14+
15+
NOTE:
16+
======
17+
Only RTC IO can be used as a source for external wake
18+
source. They are pins: 0,2,4,12-15,25-27,32-39.
19+
20+
Author:
21+
Pranav Cherukupalli <[email protected]>
22+
*/
23+
24+
#define BUTTON_PIN_BITMASK 0x200000000 // 2^33 in hex
25+
26+
RTC_DATA_ATTR int bootCount = 0;
27+
28+
/*
29+
Method to print the reason by which ESP32
30+
has been awaken from sleep
31+
*/
32+
void print_wakeup_reason(){
33+
esp_deep_sleep_wakeup_cause_t wakeup_reason;
34+
35+
wakeup_reason = esp_deep_sleep_get_wakeup_cause();
36+
37+
switch(wakeup_reason)
38+
{
39+
case 1 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
40+
case 2 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
41+
case 3 : Serial.println("Wakeup caused by timer"); break;
42+
case 4 : Serial.println("Wakeup caused by touchpad"); break;
43+
case 5 : Serial.println("Wakeup caused by ULP program"); break;
44+
default : Serial.println("Wakeup was not caused by deep sleep"); break;
45+
}
46+
}
47+
48+
void setup(){
49+
Serial.begin(115200);
50+
delay(1000); //Take some time to open up the Serial Monitor
51+
52+
//Increment boot number and print it every reboot
53+
++bootCount;
54+
Serial.println("Boot number: " + String(bootCount));
55+
56+
//Print the wakeup reason for ESP32
57+
print_wakeup_reason();
58+
59+
/*
60+
First we configure the wake up source
61+
We set our ESP32 to wake up for an external trigger.
62+
There are two types for ESP32, ext0 and ext1 .
63+
ext0 uses RTC_IO to wakeup thus requires RTC peripherals
64+
to be on while ext1 uses RTC Controller so doesnt need
65+
peripherals to be powered on.
66+
Note that using internal pullups/pulldowns also requires
67+
RTC peripherals to be turned on.
68+
*/
69+
esp_deep_sleep_enable_ext0_wakeup(GPIO_NUM_33,1); //1 = High, 0 = Low
70+
71+
//If you were to use ext1, you would use it like
72+
//esp_deep_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK,ESP_EXT1_WAKEUP_ANY_HIGH);
73+
74+
//Go to sleep now
75+
Serial.println("Going to sleep now");
76+
esp_deep_sleep_start();
77+
Serial.println("This will never be printed");
78+
}
79+
80+
void loop(){
81+
//This is not going to be called
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
Simple Deep Sleep with Timer Wake Up
3+
=====================================
4+
ESP32 offers a deep sleep mode for effective power
5+
saving as power is an important factor for IoT
6+
applications. In this mode CPUs, most of the RAM,
7+
and all the digital peripherals which are clocked
8+
from APB_CLK are powered off. The only parts of
9+
the chip which can still be powered on are:
10+
RTC controller, RTC peripherals ,and RTC memories
11+
12+
This code displays the most basic deep sleep with
13+
a timer to wake it up and how to store data in
14+
RTC memory to use it over reboots
15+
16+
This code is under Public Domain License.
17+
18+
Author:
19+
Pranav Cherukupalli <[email protected]>
20+
*/
21+
22+
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
23+
#define TIME_TO_SLEEP 5 /* Time ESP32 will go to sleep (in seconds) */
24+
25+
RTC_DATA_ATTR int bootCount = 0;
26+
27+
/*
28+
Method to print the reason by which ESP32
29+
has been awaken from sleep
30+
*/
31+
void print_wakeup_reason(){
32+
esp_deep_sleep_wakeup_cause_t wakeup_reason;
33+
34+
wakeup_reason = esp_deep_sleep_get_wakeup_cause();
35+
36+
switch(wakeup_reason)
37+
{
38+
case 1 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
39+
case 2 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
40+
case 3 : Serial.println("Wakeup caused by timer"); break;
41+
case 4 : Serial.println("Wakeup caused by touchpad"); break;
42+
case 5 : Serial.println("Wakeup caused by ULP program"); break;
43+
default : Serial.println("Wakeup was not caused by deep sleep"); break;
44+
}
45+
}
46+
47+
void setup(){
48+
Serial.begin(115200);
49+
delay(1000); //Take some time to open up the Serial Monitor
50+
51+
//Increment boot number and print it every reboot
52+
++bootCount;
53+
Serial.println("Boot number: " + String(bootCount));
54+
55+
//Print the wakeup reason for ESP32
56+
print_wakeup_reason();
57+
58+
/*
59+
First we configure the wake up source
60+
We set our ESP32 to wake up every 5 seconds
61+
*/
62+
esp_deep_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
63+
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
64+
" Seconds");
65+
66+
/*
67+
Next we decide what all peripherals to shut down/keep on
68+
By default, ESP32 will automatically power down the peripherals
69+
not needed by the wakeup source, but if you want to be a poweruser
70+
this is for you. Read in detail at the API docs
71+
http://esp-idf.readthedocs.io/en/latest/api-reference/system/deep_sleep.html
72+
Left the line commented as an example of how to configure peripherals.
73+
The line below turns off all RTC peripherals in deep sleep.
74+
*/
75+
//esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
76+
//Serial.println("Configured all RTC Peripherals to be powered down in sleep");
77+
78+
/*
79+
Now that we have setup a wake cause and if needed setup the
80+
peripherals state in deep sleep, we can now start going to
81+
deep sleep.
82+
In the case that no wake up sources were provided but deep
83+
sleep was started, it will sleep forever unless hardware
84+
reset occurs.
85+
*/
86+
Serial.println("Going to sleep now");
87+
esp_deep_sleep_start();
88+
Serial.println("This will never be printed");
89+
}
90+
91+
void loop(){
92+
//This is not going to be called
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
Deep Sleep with Touch Wake Up
3+
=====================================
4+
This code displays how to use deep sleep with
5+
a touch as a wake up source and how to store data in
6+
RTC memory to use it over reboots
7+
8+
This code is under Public Domain License.
9+
10+
Author:
11+
Pranav Cherukupalli <[email protected]>
12+
*/
13+
14+
#define Threshold 40 /* Greater the value, more the sensitivity */
15+
16+
RTC_DATA_ATTR int bootCount = 0;
17+
touch_pad_t touchPin;
18+
/*
19+
Method to print the reason by which ESP32
20+
has been awaken from sleep
21+
*/
22+
void print_wakeup_reason(){
23+
esp_deep_sleep_wakeup_cause_t wakeup_reason;
24+
25+
wakeup_reason = esp_deep_sleep_get_wakeup_cause();
26+
27+
switch(wakeup_reason)
28+
{
29+
case 1 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
30+
case 2 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
31+
case 3 : Serial.println("Wakeup caused by timer"); break;
32+
case 4 : Serial.println("Wakeup caused by touchpad"); break;
33+
case 5 : Serial.println("Wakeup caused by ULP program"); break;
34+
default : Serial.println("Wakeup was not caused by deep sleep"); break;
35+
}
36+
}
37+
38+
/*
39+
Method to print the touchpad by which ESP32
40+
has been awaken from sleep
41+
*/
42+
void print_wakeup_touchpad(){
43+
touch_pad_t pin;
44+
45+
touchPin = esp_deep_sleep_get_touchpad_wakeup_status();
46+
47+
switch(touchPin)
48+
{
49+
case 0 : Serial.println("Touch detected on GPIO 4"); break;
50+
case 1 : Serial.println("Touch detected on GPIO 0"); break;
51+
case 2 : Serial.println("Touch detected on GPIO 2"); break;
52+
case 3 : Serial.println("Touch detected on GPIO 15"); break;
53+
case 4 : Serial.println("Touch detected on GPIO 13"); break;
54+
case 5 : Serial.println("Touch detected on GPIO 12"); break;
55+
case 6 : Serial.println("Touch detected on GPIO 14"); break;
56+
case 7 : Serial.println("Touch detected on GPIO 27"); break;
57+
case 8 : Serial.println("Touch detected on GPIO 33"); break;
58+
case 9 : Serial.println("Touch detected on GPIO 32"); break;
59+
default : Serial.println("Wakeup not by touchpad"); break;
60+
}
61+
}
62+
63+
void callback(){
64+
//placeholder callback function
65+
}
66+
67+
void setup(){
68+
Serial.begin(115200);
69+
delay(1000); //Take some time to open up the Serial Monitor
70+
71+
//Increment boot number and print it every reboot
72+
++bootCount;
73+
Serial.println("Boot number: " + String(bootCount));
74+
75+
//Print the wakeup reason for ESP32 and touchpad too
76+
print_wakeup_reason();
77+
print_wakeup_touchpad();
78+
79+
//Setup interrupt on Touch Pad 3 (GPIO15)
80+
touchAttachInterrupt(T3, callback, Threshold);
81+
82+
//Configure Touchpad as wakeup source
83+
esp_deep_sleep_enable_touchpad_wakeup();
84+
85+
//Go to sleep now
86+
Serial.println("Going to sleep now");
87+
esp_deep_sleep_start();
88+
Serial.println("This will never be printed");
89+
}
90+
91+
void loop(){
92+
//This will never be reached
93+
}

0 commit comments

Comments
 (0)