@@ -5,13 +5,20 @@ This code displays how to use deep sleep with
55a touch as a wake up source and how to store data in
66RTC memory to use it over reboots
77
8+ ESP32 can have multiple touch pads enabled as wakeup source
9+ ESP32-S2 and ESP32-S3 supports only 1 touch pad as wakeup source enabled
10+
811This code is under Public Domain License.
912
1013Author:
1114Pranav Cherukupalli <cherukupallip@gmail.com>
1215*/
1316
14- #define Threshold 40 /* Greater the value, more the sensitivity */
17+ #if CONFIG_IDF_TARGET_ESP32
18+ #define THRESHOLD 40 /* Greater the value, more the sensitivity */
19+ #else // ESP32-S2 and ESP32-S3 + default for other chips (to be adjusted) */
20+ #define THRESHOLD 5000 /* Lower the value, more the sensitivity */
21+ #endif
1522
1623RTC_DATA_ATTR int bootCount = 0 ;
1724touch_pad_t touchPin;
@@ -42,24 +49,31 @@ has been awaken from sleep
4249void print_wakeup_touchpad (){
4350 touchPin = esp_sleep_get_touchpad_wakeup_status ();
4451
45- switch (touchPin)
46- {
47- case 0 : Serial.println (" Touch detected on GPIO 4" ); break ;
48- case 1 : Serial.println (" Touch detected on GPIO 0" ); break ;
49- case 2 : Serial.println (" Touch detected on GPIO 2" ); break ;
50- case 3 : Serial.println (" Touch detected on GPIO 15" ); break ;
51- case 4 : Serial.println (" Touch detected on GPIO 13" ); break ;
52- case 5 : Serial.println (" Touch detected on GPIO 12" ); break ;
53- case 6 : Serial.println (" Touch detected on GPIO 14" ); break ;
54- case 7 : Serial.println (" Touch detected on GPIO 27" ); break ;
55- case 8 : Serial.println (" Touch detected on GPIO 33" ); break ;
56- case 9 : Serial.println (" Touch detected on GPIO 32" ); break ;
57- default : Serial.println (" Wakeup not by touchpad" ); break ;
58- }
59- }
60-
61- void callback (){
62- // placeholder callback function
52+ #if CONFIG_IDF_TARGET_ESP32
53+ switch (touchPin)
54+ {
55+ case 0 : Serial.println (" Touch detected on GPIO 4" ); break ;
56+ case 1 : Serial.println (" Touch detected on GPIO 0" ); break ;
57+ case 2 : Serial.println (" Touch detected on GPIO 2" ); break ;
58+ case 3 : Serial.println (" Touch detected on GPIO 15" ); break ;
59+ case 4 : Serial.println (" Touch detected on GPIO 13" ); break ;
60+ case 5 : Serial.println (" Touch detected on GPIO 12" ); break ;
61+ case 6 : Serial.println (" Touch detected on GPIO 14" ); break ;
62+ case 7 : Serial.println (" Touch detected on GPIO 27" ); break ;
63+ case 8 : Serial.println (" Touch detected on GPIO 33" ); break ;
64+ case 9 : Serial.println (" Touch detected on GPIO 32" ); break ;
65+ default : Serial.println (" Wakeup not by touchpad" ); break ;
66+ }
67+ #else
68+ if (touchPin < TOUCH_PAD_MAX)
69+ {
70+ Serial.printf (" Touch detected on GPIO %d\n " , touchPin);
71+ }
72+ else
73+ {
74+ Serial.println (" Wakeup not by touchpad" );
75+ }
76+ #endif
6377}
6478
6579void setup (){
@@ -74,11 +88,16 @@ void setup(){
7488 print_wakeup_reason ();
7589 print_wakeup_touchpad ();
7690
77- // Setup interrupt on Touch Pad 3 (GPIO15)
78- touchAttachInterrupt (T3, callback, Threshold);
91+ #if CONFIG_IDF_TARGET_ESP32
92+ // Setup sleep wakeup on Touch Pad 3 + 7 (GPIO15 + GPIO 27)
93+ touchSleepWakeUpEnable (T3,THRESHOLD);
94+ touchSleepWakeUpEnable (T7,THRESHOLD);
95+
96+ #else // ESP32-S2 + ESP32-S3
97+ // Setup sleep wakeup on Touch Pad 3 (GPIO3)
98+ touchSleepWakeUpEnable (T3,THRESHOLD);
7999
80- // Configure Touchpad as wakeup source
81- esp_sleep_enable_touchpad_wakeup ();
100+ #endif
82101
83102 // Go to sleep now
84103 Serial.println (" Going to sleep now" );
@@ -88,4 +107,4 @@ void setup(){
88107
89108void loop (){
90109 // This will never be reached
91- }
110+ }
0 commit comments