I am trying to get the ESP32-c3 super mini to wake up from deepsleep when the door contact is closed and open. But so far i am only able to get it to wake up when the sensor is open.
try the following on an ESP32C3 where wakeup from deep sleep is GPIO3 pulled HIGH or GPIO2 pulled LOW
// ESP32-C3 - wakeup from deep sleep GPIO3 pulled HIGH GPIO2 pulled LOW
// adapted from https://forum.arduino.cc/t/esp32c3-not-waking-up-from-deep-sleep-with-gpio-button-press/1358556/4
/*
ESP32 C3 Deep Sleep and wakeup
1. works with RTC pins 2, 3, 4 and 5 but not 0 and 1
see https://github.com/espressif/arduino-esp32/issues/6656
and https://github.com/espressif/arduino-esp32/issues/8447
*/
#include <Arduino.h>
#include "esp_sleep.h"
#include "driver/rtc_io.h"
#define DEBUG 1
#define WAKEUP_PIN 3 // GPIOs taken LOW to HIGH to wakeup
#define WAKEUP_PIN2 2
#define LED_BUILTIN 8
RTC_DATA_ATTR int bootCount; // stored in RTC memory during deep sleep
esp_err_t errRc;
int esp_deep_sleep_enable_gpio_wakeup(uint64_t, int);
// display wakeup reason
void printWakeupReason() {
esp_sleep_wakeup_cause_t wakeup_reason = esp_sleep_get_wakeup_cause();
switch (wakeup_reason) {
case ESP_SLEEP_WAKEUP_EXT1:
if (DEBUG) Serial.println("Wakeup caused by external signal using EXT1");
break;
case ESP_SLEEP_WAKEUP_EXT0:
if (DEBUG) Serial.println("Wakeup caused by external signal using EXT0");
break;
case ESP_SLEEP_WAKEUP_TIMER:
if (DEBUG) Serial.println("Wakeup caused by timer");
break;
case ESP_SLEEP_WAKEUP_GPIO:
if (DEBUG) Serial.println("Wakeup caused by GPIO");
break;
default:
if (DEBUG) Serial.printf("Wakeup not caused by deep sleep: %d\n", wakeup_reason);
break;
}
}
void setup() {
if (DEBUG) Serial.begin(115200);
delay(1000); // Give time to open the Serial Monitor
Serial.println();
pinMode(LED_BUILTIN, OUTPUT); // blink LED
for (int i = 0; i < 3; i++) {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000);
digitalWrite(LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level)
delay(1000);
}
if (DEBUG) Serial.println("starting. . . ");
bootCount++; // display bootcount
if (DEBUG) Serial.printf("Boot number: %d\n", bootCount);
printWakeupReason(); // display wakeup reason
Serial.printf("GPIO%d %d GPIO%d %d\n", WAKEUP_PIN, digitalRead(WAKEUP_PIN), WAKEUP_PIN2, digitalRead(WAKEUP_PIN2) );
if(digitalRead(WAKEUP_PIN)) Serial.printf("Wakeup due to GPIO %d LOW>HIGH\n", WAKEUP_PIN);
if(!digitalRead(WAKEUP_PIN2)) Serial.printf("Wakeup due to GPIO %d HIGH>LOW\n", WAKEUP_PIN2);
// enable wakeup from deep sleep on two GPIO pins
errRc = esp_deep_sleep_enable_gpio_wakeup((1ull << WAKEUP_PIN), ESP_GPIO_WAKEUP_GPIO_HIGH);
// errRc = esp_deep_sleep_enable_gpio_wakeup((1ull << WAKEUP_PIN), ESP_GPIO_WAKEUP_GPIO_HIGH);
//errRc = esp_deep_sleep_enable_gpio_wakeup(1ull | 0b11111, ESP_GPIO_WAKEUP_GPIO_HIGH); // all RTC pins can wake 0 to 5
if (DEBUG) Serial.printf("errRc (A): %d\n", (int)errRc);
errRc = esp_deep_sleep_enable_gpio_wakeup((1ull << WAKEUP_PIN2), ESP_GPIO_WAKEUP_GPIO_LOW);
if (DEBUG) Serial.printf("errRc (A): %d\n", (int)errRc);
if (DEBUG) Serial.printf("Entering deep sleep in 5 seconds."
" Press the button (GPIO%d pulled HIGH GPIO%d LOW) to wake up.\n", WAKEUP_PIN, WAKEUP_PIN2);
delay(5000);
if (DEBUG) Serial.println("Entering deep sleep now.");
esp_deep_sleep_start();
}
void loop() {
}
serial monitor output
starting. . .
Boot number: 1
Wakeup not caused by deep sleep: 0
GPIO3 0 GPIO2 1
errRc (A): 0
errRc (A): 0
Entering deep sleep in 5 seconds. Press the button (GPIO3 pulled HIGH GPIO2 LOW) to wake up.
starting. . .
Boot number: 2
Wakeup caused by GPIO
GPIO3 1 GPIO2 1
Wakeup due to GPIO 3 LOW>HIGH
errRc (A): 0
errRc (A): 0
Entering deep sleep in 5 seconds. Press the button (GPIO3 pulled HIGH GPIO2 LOW) to wake up.
starting. . .
Boot number: 3
Wakeup caused by GPIO
GPIO3 0 GPIO2 0
Wakeup due to GPIO 2 HIGH>LOW
errRc (A): 0
errRc (A): 0
Entering deep sleep in 5 seconds. Press the button (GPIO3 pulled HIGH GPIO2 LOW) to wake up..
starting. . .
Boot number: 4
Wakeup caused by GPIO
GPIO3 0 GPIO2 0
Wakeup due to GPIO 2 HIGH>LOW
errRc (A): 0
errRc (A): 0
Entering deep sleep in 5 seconds. Press the button (GPIO3 pulled HIGH GPIO2 LOW) to wake up.
starting. . .
Boot number: 5
Wakeup caused by GPIO
GPIO3 1 GPIO2 1
Wakeup due to GPIO 3 LOW>HIGH
errRc (A): 0
errRc (A): 0
Entering deep sleep in 5 seconds. Press the button (GPIO3 pulled HIGH GPIO2 LOW) to wake up
EDIT: to enable wakeup from deep sleep on two GPIO pins going LOW>HIGH
esp32c3 V5.5 api-reference sleep_modes specifies two wakeup condition parameters for esp_deep_sleep_enable_gpio_wakeup(mask, mode) so mode -- Select logic function used to determine wakeup condition:
ESP_GPIO_WAKEUP_GPIO_LOW: wake up when the gpio turn to low.*
ESP_GPIO_WAKEUP_GPIO_HIGH: wake up when the gpio turn to high.*
following up on idea by @kmin in post 20
this reads the state of the pin before sleep and sets the wakeup logic to the inverse, e.g. if GPIO is LOW on reset wakeup is for when it goes HIGH
// ESP32-C3 - wakeup from deep sleep GPIO3
// reads the state of the pin before sleep and sets the wakeup logic to the inverse
// adapted from https://forum.arduino.cc/t/esp32c3-not-waking-up-from-deep-sleep-with-gpio-button-press/1358556/4
/*
ESP32 C3 Deep Sleep and wakeup
1. works with RTC pins 2, 3, 4 and 5 but not 0 and 1
see https://github.com/espressif/arduino-esp32/issues/6656
and https://github.com/espressif/arduino-esp32/issues/8447
*/
#include <Arduino.h>
#include "esp_sleep.h"
#include "driver/rtc_io.h"
#define DEBUG 1
#define WAKEUP_PIN 3 // GPIOs taken LOW or HIGH to wakeup
#define LED_BUILTIN 8
RTC_DATA_ATTR int bootCount; // stored in RTC memory during deep sleep
esp_err_t errRc;
int esp_deep_sleep_enable_gpio_wakeup(uint64_t, int);
// display wakeup reason
void printWakeupReason() {
esp_sleep_wakeup_cause_t wakeup_reason = esp_sleep_get_wakeup_cause();
switch (wakeup_reason) {
case ESP_SLEEP_WAKEUP_EXT1:
if (DEBUG) Serial.println("Wakeup caused by external signal using EXT1");
break;
case ESP_SLEEP_WAKEUP_EXT0:
if (DEBUG) Serial.println("Wakeup caused by external signal using EXT0");
break;
case ESP_SLEEP_WAKEUP_TIMER:
if (DEBUG) Serial.println("Wakeup caused by timer");
break;
case ESP_SLEEP_WAKEUP_GPIO:
if (DEBUG) Serial.println("Wakeup caused by GPIO");
break;
default:
if (DEBUG) Serial.printf("Wakeup not caused by deep sleep: %d\n", wakeup_reason);
break;
}
}
void setup() {
if (DEBUG) Serial.begin(115200);
delay(1000); // Give time to open the Serial Monitor
Serial.println();
pinMode(LED_BUILTIN, OUTPUT); // blink LED
for (int i = 0; i < 3; i++) {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000);
digitalWrite(LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level)
delay(1000);
}
if (DEBUG) Serial.println("starting. . . ");
bootCount++; // display bootcount
if (DEBUG) Serial.printf("Boot number: %d\n", bootCount);
printWakeupReason(); // display wakeup reason
if (digitalRead(WAKEUP_PIN)) { // if GPIO is HIGH set for HIGH to LOW wakeup
Serial.printf("GPIO%d is HIGH setting wakeup to GPIO_LOW\n", WAKEUP_PIN);
errRc = esp_deep_sleep_enable_gpio_wakeup((1ull << WAKEUP_PIN), ESP_GPIO_WAKEUP_GPIO_LOW);
if (DEBUG) Serial.printf("errRc (A): %d\n", (int)errRc);
if (DEBUG) Serial.printf("Entering deep sleep in 5 seconds. GPIO%d pulled LOW to wake up\n", WAKEUP_PIN);
} else { // if GPIO is LOW set for LOW to HIGH wakeup
Serial.printf("GPIO%d is LOW setting wakeup to GPIO_HIGH\n", WAKEUP_PIN);
errRc = esp_deep_sleep_enable_gpio_wakeup((1ull << WAKEUP_PIN), ESP_GPIO_WAKEUP_GPIO_HIGH);
if (DEBUG) Serial.printf("errRc (A): %d\n", (int)errRc);
if (DEBUG) Serial.printf("Entering deep sleep in 5 seconds. GPIO%d pulled HIGH to wake up\n", WAKEUP_PIN);
}
delay(5000);
if (DEBUG) Serial.println("Entering deep sleep now.");
esp_deep_sleep_start();
}
void loop() {
}
serial monitor output
starting. . .
Boot number: 1
Wakeup not caused by deep sleep: 0
GPIO3 is HIGH setting wakeup to GPIO_LOW
errRc (A): 0
Entering deep sleep in 5 seconds. GPIO3 pulled LOW to wake up
starting. . .
Boot number: 2
Wakeup caused by GPIO
GPIO3 is LOW setting wakeup to GPIO_HIGH
errRc (A): 0
Entering deep sleep in 5 seconds. GPIO3 pulled HIGH to wake up
Entering deep sleep now.
starting. . .
Boot number: 3
Wakeup caused by GPIO
GPIO3 is HIGH setting wakeup to GPIO_LOW
errRc (A): 0
Entering deep sleep in 5 seconds. GPIO3 pulled LOW to wake up
starting. . .
Boot number: 4
Wakeup caused by GPIO
GPIO3 is LOW setting wakeup to GPIO_HIGH
errRc (A): 0
Entering deep sleep in 5 seconds. GPIO3 pulled HIGH to wake up