ESP32 Touch Wake Up from Deep Sleep
This guide shows how to wake up the ESP32 from deep sleep using the touch sensitive pins. The ESP32 will be programmed using Arduino IDE.
- Suitable forB.Tech/Diploma IoT/ESP32 students
- Build time1-2 weeks
- LevelBeginner
- Available atAjvika Labs, Kakinada
About this project
This guide shows how to wake up the ESP32 from deep sleep using the touch-sensitive pins. The ESP32 will be programmed using Arduino IDE.
The ESP32 can be awake from deep sleep using several wake-up sources:
timer
external wake-up
, and touch wake-up. This article shows how to use touch wake-up.
To learn more about deep sleep and other wake-up sources, you can follow the next tutorials:
[Complete Guide]
ESP32 Deep Sleep with Arduino IDE and Wake-Up Sources
ESP32 External Wake-Up from Deep Sleep
ESP32 Timer Wake Up from Deep Sleep
Writing a Deep Sleep Sketch
To write a sketch to put your ESP32 into deep sleep mode, and then wake it up, you need to:
First, configure the wake-up sources. This means configuring what will wake up the ESP32. You can use one or combine more than one wake-up source. This tutorial covers how to use the touch pins as a wake-up source. For that, use the
touchSleepWakeUpEnable()
function and pass as arguments the touch pin and the touch threshold.
You can decide what peripherals to shut down or keep on during deep sleep. However, by default, the ESP32 automatically powers down the peripherals that are not needed with the wake-up source you define.
Finally, you use the
esp_deep_sleep_start()
function to put your ESP32 into deep sleep mode.
Touch Wake Up
The ESP32 board has 10 capacitive touch GPIOs (that might change depending on the ESP32 chip). These GPIOs can sense variations in anything that holds an electrical charge, like the human skin. So they can detect variations induced when touching the GPIOs with a finger. These ESP32 touch pins can be used to wake up the ESP32 from deep sleep.
Learn more about the ESP32 touch pins:
ESP32 Capacitive Touch Sensor Pins with Arduino IDE
Touch Pins
The ESP32 touch pins are highlighted in pink color in the following diagram.
You can see that touch sensor 0 corresponds to GPIO 4, touch sensor 2 to GPIO 2, and so on.
Learn everything you need to know about the ESP32 GPIOs
:
ESP32 Pinout Reference: Which GPIO pins should you use?
If you’re using an ESP32 with the S3 chip,
check this GPIOs guide instead
Enable Touch Wake-Up
Enabling the ESP32 to wake up using a touchpin is simple. In the Arduino IDE, you need to use the following function—pass as argument the touch pin and the touch threshold:
touchSleepWakeUpEnable(TOUCH_PIN, THRESHOLD);
Code – Touch Wake-Up
To program the ESP32 we’ll use Arduino IDE. So, you need to make sure you have the ESP32 core installed. Follow the next tutorial if you haven’t already:
Installing ESP32 Board in Arduino IDE 2 (Windows, Mac OS X, Linux)
Let’s see how touch wake-up works using an example from the ESP32 Arduino core. Open your Arduino IDE, and go to
File
>
Examples
>
ESP32
Deep Sleep
, and open the
TouchWakeUp
example sketch.
/*
Deep Sleep with Touch Wake Up
=====================================
This code displays how to use deep sleep with
a touch as a wake up source and how to store data in
RTC memory to use it over reboots
ESP32 can have multiple touch pads enabled as wakeup source
ESP32-S2 and ESP32-S3 supports only 1 touch pad as wakeup source enabled
This code is under Public Domain License.
Author:
Pranav Cherukupalli <
[email protected]
>
*/
#if CONFIG_IDF_TARGET_ESP32
#define THRESHOLD 40 /* Greater the value, more the sensitivity */
#else //ESP32-S2 and ESP32-S3 + default for other chips (to be adjusted) */
#define THRESHOLD 5000 /* Lower the value, more the sensitivity */
#endif
RTC_DATA_ATTR int bootCount = 0;
touch_pad_t touchPin;
/*
Method to print the reason by which ESP32
has been awaken from sleep
*/
void print_wakeup_reason() {
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch (wakeup_reason) {
case ESP_SLEEP_WAKEUP_EXT0: Serial.println(\"Wakeup caused by external signal using RTC_IO\"); break;
case ESP_SLEEP_WAKEUP_EXT1: Serial.println(\"Wakeup caused by external signal using RTC_CNTL\"); break;
case ESP_SLEEP_WAKEUP_TIMER: Serial.println(\"Wakeup caused by timer\"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD: Serial.println(\"Wakeup caused by touchpad\"); break;
case ESP_SLEEP_WAKEUP_ULP: Serial.println(\"Wakeup caused by ULP program\"); break;
default: Serial.printf(\"Wakeup was not caused by deep sleep: %d\
\", wakeup_reason); break;
}
}
/*
Method to print the touchpad by which ESP32
has been awaken from sleep
*/
void print_wakeup_touchpad() {
touchPin = (touch_pad_t) esp_sleep_get_touchpad_wakeup_status();
#if CONFIG_IDF_TARGET_ESP32
switch (touchPin) {
case 0: Serial.println(\"Touch detected on GPIO 4\"); break;
case 1: Serial.println(\"Touch detected on GPIO 0\"); break;
case 2: Serial.println(\"Touch detected on GPIO 2\"); break;
case 3: Serial.println(\"Touch detected on GPIO 15\"); break;
case 4: Serial.println(\"Touch detected on GPIO 13\"); break;
case 5: Serial.println(\"Touch detected on GPIO 12\"); break;
case 6: Serial.println(\"Touch detected on GPIO 14\"); break;
case 7: Serial.println(\"Touch detected on GPIO 27\"); break;
case 8: Serial.println(\"Touch detected on GPIO 33\"); break;
case 9: Serial.println(\"Touch detected on GPIO 32\"); break;
default: Serial.println(\"Wakeup not by touchpad\"); break;
}
#else
if (touchPin < TOUCH_PAD_MAX) {
Serial.printf(\"Touch detected on GPIO %d\
\", touchPin);
} else {
Serial.println(\"Wakeup not by touchpad\");
}
#endif
}
void setup() {
Serial.begin(115200);
delay(1000); //Take some time to open up the Serial Monitor
//Increment boot number and print it every reboot
++bootCount;
Serial.println(\"Boot number: \" + String(bootCount));
//Print the wakeup reason for ESP32 and touchpad too
print_wakeup_reason();
print_wakeup_touchpad();
#if CONFIG_IDF_TARGET_ESP32
//Setup sleep wakeup on Touch Pad 3 + 7 (GPIO15 + GPIO 27)
touchSleepWakeUpEnable(T3, THRESHOLD);
touchSleepWakeUpEnable(T7, THRESHOLD);
#else //ESP32-S2 + ESP32-S3
//Setup sleep wakeup on Touch Pad 3 (GPIO3)
touchSleepWakeUpEnable(T3, THRESHOLD);
#endif
//Go to sleep now
Serial.println(\"Going to sleep now\");
esp_deep_sleep_start();
Serial.println(\"This will never be printed\");
}
void loop() {
//This will never be reached
}
Setting the Threshold
The first thing you need to do is setting a threshold value for the touch pins.
#define THRESHOLD 40 // Greater the value, more the sensitivity
The values read by a touch pin decrease when you touch it. The threshold value means that the ESP32 will wake up when the value read on the touch pin is below 40. You can adjust that value depending on the desired sensitivity.
Important:
however, if you’re using an ESP32-S2 or ESP32-S3 model, things work a little
differently. That’s why there’s a different section in the code defining a different threshold for those boards. In this case, the lower the value, the more the sensitivity.
#if CONFIG_IDF_TARGET_ESP32
#define THRESHOLD 40 // Greater the value, more the sensitivity
#else // ESP32-S2 and ESP32-S3 + default for other chips (to be adjusted)
#define THRESHOLD 5000 // Lower the value, more the sensitivity
#endif
Setting Touch Pins as a Wake-up Source
To set a touch pin as a wake-up source, you can use the
touchSleepWakeUpEnable()
function that accepts as arguments the touch pin and the threshold value that will wake up the board.
In this example, it sets GPIO 15 (T3) and GPIO 27 (T7) as wake-up sources with the same threshold value.
#if CONFIG_IDF_TARGET_ESP32
// Setup sleep wakeup on Touch Pad 3 + 7 (GPIO15 + GPIO 27)
touchSleepWakeUpEnable(T3, THRESHOLD);
touchSleepWakeUpEnable(T7, THRESHOLD);
If you’re using an ESP32-S2 or S3 model, the example just defines wake-up on GPIO 3 (T3). Note that the touch pin numbering might be different depending on the board model you’re using.
#else // ESP32-S2 + ESP32-S3
// Setup sleep wakeup on Touch Pad 3 (GPIO3)
touchSl
The ESP32 can be awake from deep sleep using several wake-up sources:
timer
external wake-up
, and touch wake-up. This article shows how to use touch wake-up.
To learn more about deep sleep and other wake-up sources, you can follow the next tutorials:
[Complete Guide]
ESP32 Deep Sleep with Arduino IDE and Wake-Up Sources
ESP32 External Wake-Up from Deep Sleep
ESP32 Timer Wake Up from Deep Sleep
Writing a Deep Sleep Sketch
To write a sketch to put your ESP32 into deep sleep mode, and then wake it up, you need to:
First, configure the wake-up sources. This means configuring what will wake up the ESP32. You can use one or combine more than one wake-up source. This tutorial covers how to use the touch pins as a wake-up source. For that, use the
touchSleepWakeUpEnable()
function and pass as arguments the touch pin and the touch threshold.
You can decide what peripherals to shut down or keep on during deep sleep. However, by default, the ESP32 automatically powers down the peripherals that are not needed with the wake-up source you define.
Finally, you use the
esp_deep_sleep_start()
function to put your ESP32 into deep sleep mode.
Touch Wake Up
The ESP32 board has 10 capacitive touch GPIOs (that might change depending on the ESP32 chip). These GPIOs can sense variations in anything that holds an electrical charge, like the human skin. So they can detect variations induced when touching the GPIOs with a finger. These ESP32 touch pins can be used to wake up the ESP32 from deep sleep.
Learn more about the ESP32 touch pins:
ESP32 Capacitive Touch Sensor Pins with Arduino IDE
Touch Pins
The ESP32 touch pins are highlighted in pink color in the following diagram.
You can see that touch sensor 0 corresponds to GPIO 4, touch sensor 2 to GPIO 2, and so on.
Learn everything you need to know about the ESP32 GPIOs
:
ESP32 Pinout Reference: Which GPIO pins should you use?
If you’re using an ESP32 with the S3 chip,
check this GPIOs guide instead
Enable Touch Wake-Up
Enabling the ESP32 to wake up using a touchpin is simple. In the Arduino IDE, you need to use the following function—pass as argument the touch pin and the touch threshold:
touchSleepWakeUpEnable(TOUCH_PIN, THRESHOLD);
Code – Touch Wake-Up
To program the ESP32 we’ll use Arduino IDE. So, you need to make sure you have the ESP32 core installed. Follow the next tutorial if you haven’t already:
Installing ESP32 Board in Arduino IDE 2 (Windows, Mac OS X, Linux)
Let’s see how touch wake-up works using an example from the ESP32 Arduino core. Open your Arduino IDE, and go to
File
>
Examples
>
ESP32
Deep Sleep
, and open the
TouchWakeUp
example sketch.
/*
Deep Sleep with Touch Wake Up
=====================================
This code displays how to use deep sleep with
a touch as a wake up source and how to store data in
RTC memory to use it over reboots
ESP32 can have multiple touch pads enabled as wakeup source
ESP32-S2 and ESP32-S3 supports only 1 touch pad as wakeup source enabled
This code is under Public Domain License.
Author:
Pranav Cherukupalli <
[email protected]
>
*/
#if CONFIG_IDF_TARGET_ESP32
#define THRESHOLD 40 /* Greater the value, more the sensitivity */
#else //ESP32-S2 and ESP32-S3 + default for other chips (to be adjusted) */
#define THRESHOLD 5000 /* Lower the value, more the sensitivity */
#endif
RTC_DATA_ATTR int bootCount = 0;
touch_pad_t touchPin;
/*
Method to print the reason by which ESP32
has been awaken from sleep
*/
void print_wakeup_reason() {
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch (wakeup_reason) {
case ESP_SLEEP_WAKEUP_EXT0: Serial.println(\"Wakeup caused by external signal using RTC_IO\"); break;
case ESP_SLEEP_WAKEUP_EXT1: Serial.println(\"Wakeup caused by external signal using RTC_CNTL\"); break;
case ESP_SLEEP_WAKEUP_TIMER: Serial.println(\"Wakeup caused by timer\"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD: Serial.println(\"Wakeup caused by touchpad\"); break;
case ESP_SLEEP_WAKEUP_ULP: Serial.println(\"Wakeup caused by ULP program\"); break;
default: Serial.printf(\"Wakeup was not caused by deep sleep: %d\
\", wakeup_reason); break;
}
}
/*
Method to print the touchpad by which ESP32
has been awaken from sleep
*/
void print_wakeup_touchpad() {
touchPin = (touch_pad_t) esp_sleep_get_touchpad_wakeup_status();
#if CONFIG_IDF_TARGET_ESP32
switch (touchPin) {
case 0: Serial.println(\"Touch detected on GPIO 4\"); break;
case 1: Serial.println(\"Touch detected on GPIO 0\"); break;
case 2: Serial.println(\"Touch detected on GPIO 2\"); break;
case 3: Serial.println(\"Touch detected on GPIO 15\"); break;
case 4: Serial.println(\"Touch detected on GPIO 13\"); break;
case 5: Serial.println(\"Touch detected on GPIO 12\"); break;
case 6: Serial.println(\"Touch detected on GPIO 14\"); break;
case 7: Serial.println(\"Touch detected on GPIO 27\"); break;
case 8: Serial.println(\"Touch detected on GPIO 33\"); break;
case 9: Serial.println(\"Touch detected on GPIO 32\"); break;
default: Serial.println(\"Wakeup not by touchpad\"); break;
}
#else
if (touchPin < TOUCH_PAD_MAX) {
Serial.printf(\"Touch detected on GPIO %d\
\", touchPin);
} else {
Serial.println(\"Wakeup not by touchpad\");
}
#endif
}
void setup() {
Serial.begin(115200);
delay(1000); //Take some time to open up the Serial Monitor
//Increment boot number and print it every reboot
++bootCount;
Serial.println(\"Boot number: \" + String(bootCount));
//Print the wakeup reason for ESP32 and touchpad too
print_wakeup_reason();
print_wakeup_touchpad();
#if CONFIG_IDF_TARGET_ESP32
//Setup sleep wakeup on Touch Pad 3 + 7 (GPIO15 + GPIO 27)
touchSleepWakeUpEnable(T3, THRESHOLD);
touchSleepWakeUpEnable(T7, THRESHOLD);
#else //ESP32-S2 + ESP32-S3
//Setup sleep wakeup on Touch Pad 3 (GPIO3)
touchSleepWakeUpEnable(T3, THRESHOLD);
#endif
//Go to sleep now
Serial.println(\"Going to sleep now\");
esp_deep_sleep_start();
Serial.println(\"This will never be printed\");
}
void loop() {
//This will never be reached
}
Setting the Threshold
The first thing you need to do is setting a threshold value for the touch pins.
#define THRESHOLD 40 // Greater the value, more the sensitivity
The values read by a touch pin decrease when you touch it. The threshold value means that the ESP32 will wake up when the value read on the touch pin is below 40. You can adjust that value depending on the desired sensitivity.
Important:
however, if you’re using an ESP32-S2 or ESP32-S3 model, things work a little
differently. That’s why there’s a different section in the code defining a different threshold for those boards. In this case, the lower the value, the more the sensitivity.
#if CONFIG_IDF_TARGET_ESP32
#define THRESHOLD 40 // Greater the value, more the sensitivity
#else // ESP32-S2 and ESP32-S3 + default for other chips (to be adjusted)
#define THRESHOLD 5000 // Lower the value, more the sensitivity
#endif
Setting Touch Pins as a Wake-up Source
To set a touch pin as a wake-up source, you can use the
touchSleepWakeUpEnable()
function that accepts as arguments the touch pin and the threshold value that will wake up the board.
In this example, it sets GPIO 15 (T3) and GPIO 27 (T7) as wake-up sources with the same threshold value.
#if CONFIG_IDF_TARGET_ESP32
// Setup sleep wakeup on Touch Pad 3 + 7 (GPIO15 + GPIO 27)
touchSleepWakeUpEnable(T3, THRESHOLD);
touchSleepWakeUpEnable(T7, THRESHOLD);
If you’re using an ESP32-S2 or S3 model, the example just defines wake-up on GPIO 3 (T3). Note that the touch pin numbering might be different depending on the board model you’re using.
#else // ESP32-S2 + ESP32-S3
// Setup sleep wakeup on Touch Pad 3 (GPIO3)
touchSl
Technologies used
Hardware components
- ESP32
- Arduino
Software & tools
- Arduino IDE
What you get
- Working project / hardware kit, tested before delivery
- Complete source code with explanation
- Project demo and guidance for your viva
- Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed.
Payment is in two parts:
• 50% advance to start the project
• Remaining 50% before delivery
Note:
• Documentation and PPT are not included by default
• If you need documentation/PPT, it can be arranged for extra time and cost
We'll confirm the exact timeline and cost with you after reviewing your requirements.