Esp32-c3 super mini deepsleep doorsensor

What if the door is opened or closed during your 5 second delay?

it will immediately wakeup
the 5 second delay and LED blink loops at the start of setup() were in the original test program
they can be removed

HI. tried Horace code but still it doesn’t wake up after sleep,

do i have to use a resistor to pin3 och us it godd enough with INPUT_PULLUP?

That sensor likely has pull-up/down. How is your circuit?

grio3——magnetsensor—–gnd

I just used a jumper wire and moved GPIO3 between 3v3 and GND

it would go to sleep and wakeup

Ahh.. bare sensor. Then you need pullup.

From the ESP documentation:
esp_deep_sleep_enable_gpio_wakeup ()

You don't need to worry about pull-up or pull-down resistors before using this function because the ESP_SLEEP_GPIO_ENABLE_INTERNAL_RESISTORS option is enabled by default. It will automatically set pull-up or pull-down resistors internally in esp_deep_sleep_start based on the wakeup mode. However, when using external pull-up or pull-down resistors, please be sure to disable the ESP_SLEEP_GPIO_ENABLE_INTERNAL_RESISTORS option, as the combination of internal and external resistors may cause interference. BTW, when you use low level to wake up the chip, we strongly recommend you to add external resistors (pull-up).

Don't want to fry your ESP by doing the wrong thing

it seems to work :slight_smile: however i put in:

esp_deep_sleep_gpio_disable_internal_resistors();

but get:

expected constructor,destructor, or type conversion before ‘;’ token

also i wonder since i only use gpio:s to wake and sleep is it possible to reduce the code?

cannot see problem from fragment of code - post all code

ESP32-C3 - wakeup from deep sleep using timer and GPIO3

// ESP32-C3 - wakeup from deep sleep using timer and  GPIO3
// 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

// 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() {
  Serial.begin(115200);
  delay(1000);  // Give time to open the Serial Monitor

  Serial.println("\nGPIO and timer wakeup from deep sleep  ");
  bootCount++;  // display bootcount
  Serial.printf("Boot number: %d\n", bootCount);
  printWakeupReason();  // display wakeup reason
  // enable timer wakeup after 10 second
  esp_sleep_enable_timer_wakeup(10000000);  // time in micro seconds

  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);
    //Serial.printf("errRc  (A): %d\n", (int)errRc);
    Serial.printf("Entering deep sleep in 5 second. 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);
    //Serial.printf("errRc  (A): %d\n", (int)errRc);
    Serial.printf("Entering deep sleep in 5 second. GPIO%d pulled HIGH to wake up\n", WAKEUP_PIN);
  }
  delay(5000);
  Serial.println("Entering deep sleep now.");
  esp_deep_sleep_start();
}

void loop() {}

serial monitor output - ESP32C3 wakes after 10 seconds or when GPIO3 changes state

06:18:26.906 -> GPIO and timer wakeup from deep sleep  
06:18:26.906 -> Boot number: 1
06:18:26.906 -> Wakeup not caused by deep sleep: 0
06:18:26.906 -> GPIO3 is LOW setting wakeup to GPIO_HIGH
06:18:26.906 -> Entering deep sleep in 5 second. GPIO3 pulled HIGH to wake up
06:18:42.952 -> 
06:18:42.952 -> GPIO and timer wakeup from deep sleep  
06:18:42.952 -> Boot number: 2
06:18:42.952 -> Wakeup caused by timer
06:18:42.952 -> GPIO3 is LOW setting wakeup to GPIO_HIGH
06:18:42.952 -> Entering deep sleep in 5 second. GPIO3 pulled HIGH to wake up
06:18:51.855 -> 
06:18:51.855 -> GPIO and timer wakeup from deep sleep  
06:18:51.855 -> Boot number: 3
06:18:51.855 -> Wakeup caused by GPIO
06:18:51.855 -> GPIO3 is HIGH setting wakeup to GPIO_LOW
06:18:51.855 -> Entering deep sleep in 5 second. GPIO3 pulled LOW to wake up
06:19:02.287 -> 
06:19:02.287 -> GPIO and timer wakeup from deep sleep  
06:19:02.287 -> Boot number: 4
06:19:02.287 -> Wakeup caused by GPIO
06:19:02.287 -> GPIO3 is LOW setting wakeup to GPIO_HIGH
06:19:02.287 -> Entering deep sleep in 5 second. GPIO3 pulled HIGH to wake up
06:19:18.380 -> 
06:19:18.380 -> GPIO and timer wakeup from deep sleep  
06:19:18.380 -> Boot number: 5
06:19:18.380 -> Wakeup caused by timer
06:19:18.380 -> GPIO3 is LOW setting wakeup to GPIO_HIGH
06:19:18.380 -> Entering deep sleep in 5 second. GPIO3 pulled HIGH to wake up
06:19:23.313 -> Entering deep sleep now.
06:19:34.342 -> 
06:19:34.342 -> GPIO and timer wakeup from deep sleep  
06:19:34.342 -> Boot number: 6
06:19:34.342 -> Wakeup caused by timer
06:19:34.342 -> GPIO3 is LOW setting wakeup to GPIO_HIGH
06:19:34.342 -> Entering deep sleep in 5 second. GPIO3 pulled HIGH to wake up
06:19:39.337 -> Entering deep sleep now.
06:19:43.656 -> 
06:19:43.656 -> GPIO and timer wakeup from deep sleep  
06:19:43.656 -> Boot number: 7
06:19:43.656 -> Wakeup caused by GPIO
06:19:43.656 -> GPIO3 is HIGH setting wakeup to GPIO_LOW
06:19:43.656 -> Entering deep sleep in 5 second. GPIO3 pulled LOW to wake up
06:19:48.627 -> Entering deep sleep now.
06:19:59.660 -> 
06:19:59.660 -> GPIO and timer wakeup from deep sleep  
06:19:59.660 -> Boot number: 8
06:19:59.660 -> Wakeup caused by timer
06:19:59.660 -> GPIO3 is HIGH setting wakeup to GPIO_LOW
06:19:59.660 -> Entering deep sleep in 5 second. GPIO3 pulled LOW to wake up
06:20:04.648 -> Entering deep sleep now.
06:20:15.683 -> 
06:20:15.683 -> GPIO and timer wakeup from deep sleep  
06:20:15.683 -> Boot number: 9
06:20:15.683 -> Wakeup caused by timer
06:20:15.683 -> GPIO3 is HIGH setting wakeup to GPIO_LOW
06:20:15.683 -> Entering deep sleep in 5 second. GPIO3 pulled LOW to wake up
06:20:20.667 -> Entering deep sleep now.
06:20:31.707 -> 

Works perfect! thanks all

Do you know if ESP_SLEEP_GPIO_ENABLE_INTERNAL_RESISTORS is possible to configure from PlatformIO with framework = arduino?
I add build_flags = -D ESP_SLEEP_GPIO_ENABLE_INTERNAL_RESISTORS=0 but I measure 17K resistance with my multimeter between pin 3 and GND.

I know nothing about PlatformIO
You can't measure the internal pullup resistance with an ohmmeter.
If the pin is an input with pullup enabled you should measure 3.3V on the pin.

Some findings:

  • To configure ESP_SLEEP_GPIO_ENABLE_INTERNAL_RESISTORS (or CONFIG_ESP_SLEEP_GPIO_ENABLE_INTERNAL_RESISTORS) while using Arduino framework (through PlatformIO or others) requires recompiling the Arduino library, like documented here: GitHub · Where software is built
  • ESP_SLEEP_GPIO_ENABLE_INTERNAL_RESISTORS configuration works in ESP-IDF, and thanks to that it also works through ESPHome. This way I moved my project from Arduino code to ESPHome and got the internal pull up/down resistors disabled.
  • Captured my analysis here: Disabling ESP32 C3 Mini RTC Internal Resistors

How can I disable the automatic internal pull-up/pull-down resistors on the ESP32-C3 during deep sleep?

I am using an ESP32-C3 SuperMini to make a simple door limit-switch sensor.
The limit switch has only 2 wires (NO/NC type).

My goal is:

  • Use an external pull-up resistor (normal behavior)
  • When the limit switch is pressed → GPIO becomes LOW → wake up by LOW
  • When the limit switch is released → GPIO becomes HIGH → wake up by HIGH

The problem:
On the ESP32-C3, when I enable deep-sleep GPIO wakeup, the chip automatically forces internal pull-up or pull-down resistors, even if I do not configure any pull resistors in my code.

So the GPIO level is changed by the internal resistor, and I cannot reliably read the real state of my limit switch.
This makes it impossible to use one 2-wire limit switch to detect both LOW and HIGH transitions.

I tried to disable it using:

build_flags =
  -DCONFIG_ESP_SLEEP_GPIO_ENABLE_INTERNAL_RESISTORS=0
  -DESP_SLEEP_GPIO_ENABLE_INTERNAL_RESISTORS=0

…but the ESP32-C3 still enables the internal resistors automatically.

I found that ESP32-S3 does NOT have this problem, and works correctly with custom pull-ups during deep sleep.
But ESP32-C3 seems to force internal pull resistors in hardware.

:right_arrow: Is there any way to completely disable these automatic pull-up/pull-down resistors during deep sleep on the ESP32-C3?
:right_arrow: Or is this a hardware limitation of the C3 that cannot be changed?

Thank you!