Unexpected Deep Sleep Behavior

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Post Reply
User avatar
surffl
 
Posts: 10
Joined: Fri Feb 23, 2024 1:44 am

Unexpected Deep Sleep Behavior

Post by surffl »

Hi all,

I am trying to get deep sleep to work on my esp32-2s Reverse TFT Feather. My goal is to have the board go into deep sleep after a given amount of time (in this example 30 seconds) and then remain in deep sleep until I press the D1 button on the board.

It appears that the board correctly goes to sleep after 30sec, but then immediately wakes up again without any button press. The test script that I have been using and getting this behavior with is below.

Does anybody have any ideas as to why this may be happening? I keep messing with it, but am not making any progress. Thanks in advance!

Code: Select all

#define BUTTON_PIN 1
#define SLEEP_TIMEOUT 30000  // 2 minutes

unsigned long lastButtonPressTime = 0;
bool deviceAwake = true;

void setup() {
  Serial.begin(115200);
    delay(1000); // Give time for serial to start

    pinMode(BUTTON_PIN, INPUT); // Ensure internal pull-up is enabled
    lastButtonPressTime = millis(); // Reset timer on wake-up
  Serial.println("Setup complete");
}

void loop() {
  // Serial.println("Looping");
if (digitalRead(BUTTON_PIN) == HIGH) {
    Serial.println("Button Pressed");
    lastButtonPressTime = millis();
    delay(200); // Debounce delay
}

// Check for inactivity timeout
    if (millis() - lastButtonPressTime > SLEEP_TIMEOUT) {
        goToSleep();
    }

}

void goToSleep() {
    Serial.println("Going to sleep...");
    delay(100); // Allow time for display to update
    esp_sleep_enable_ext0_wakeup(GPIO_NUM_1, HIGH); // Wake up when button is pressed
    Serial.println("Really going to sleep");
    esp_deep_sleep_start();
}

User avatar
T_Mo
 
Posts: 1722
Joined: Thu Mar 15, 2018 7:10 pm

Re: Unexpected Deep Sleep Behavior

Post by T_Mo »

(only a community member)
Check the Learn page for that board, verify whether that button uses a pull-up or a pull-down.

User avatar
sj_remington
 
Posts: 1346
Joined: Mon Jul 27, 2020 4:51 pm

Re: Unexpected Deep Sleep Behavior

Post by sj_remington »

Code: Select all

pinMode(BUTTON_PIN, INPUT); // Ensure internal pull-up is enabled
Use INPUT_PULLUP to enable the pullup. A floating pin will trigger an interrupt.

User avatar
surffl
 
Posts: 10
Joined: Fri Feb 23, 2024 1:44 am

Re: Unexpected Deep Sleep Behavior

Post by surffl »

Makes sense that the pin was floating and causing it. The info on the Adafruit Learn page says the following about the board
These two pins are pulled LOW by default, e.g. when not pressed, the signal is low. When pressed, the signal goes HIGH. This is required to wake the ESP32-S2 from deep sleep. This means you need to look for the signal to go high to track a button press.
With this in mind, I tried using the internal pulldown resistor to keep the pin from floating and triggering. To do this I have tried using

Code: Select all

pinMode(BUTTON_PIN, INPUT_PULLDOWN);
Button presses are still properly detected, but I am guessing I am still getting floating behavior as the board still immediately wakes from deep sleep and restarts after going to sleep. My revised test code is below. Thanks again for your help!

Code: Select all

#define BUTTON_PIN 1
#define SLEEP_TIMEOUT 30000  // 30 sec

unsigned long lastButtonPressTime = 0;
bool deviceAwake = true;

void setup() {
  Serial.begin(115200);
    delay(1000); // Give time for serial to start

    pinMode(BUTTON_PIN, INPUT_PULLDOWN); 
    lastButtonPressTime = millis(); // Reset timer on wake-up
  Serial.println("Setup complete");
}

void loop() {
  // Serial.println("Looping");
if (digitalRead(BUTTON_PIN) == HIGH) {
    Serial.println("Button Pressed");
    lastButtonPressTime = millis();
    delay(200); // Debounce delay
}

// Check for inactivity timeout
    if (millis() - lastButtonPressTime > SLEEP_TIMEOUT) {
        goToSleep();
    }

}

void goToSleep() {
    Serial.println("Going to sleep...");
    delay(100); // Allow time for display to update
    esp_sleep_enable_ext0_wakeup(GPIO_NUM_1, HIGH); // Wake up when button is pressed
    Serial.println("Really going to sleep");
    esp_deep_sleep_start();
}

User avatar
T_Mo
 
Posts: 1722
Joined: Thu Mar 15, 2018 7:10 pm

Re: Unexpected Deep Sleep Behavior

Post by T_Mo »

If I read your code correctly, if you push the button for more than 200 msec, it will go to sleep and then immediately wake up.

User avatar
surffl
 
Posts: 10
Joined: Fri Feb 23, 2024 1:44 am

Re: Unexpected Deep Sleep Behavior

Post by surffl »

Hmm. I might be missing something, but I'm not interpreting the code that way. I can also can confirm that the button press behavior functions as expected when the board is awake. The issue is that after the board goes to sleep, it automatically restarts without any input.

I'm wondering if despite using the internal pulldown, that there might still be noise on that pin that is momentarily pulling it high and is triggering the wakeup? I will try to do some more testing to see if this might be the case (unfortunately I don't own a scope...).

I'd like to use the D1 button as the wakeup trigger (constrained by mechanical design of a housing) - I'm wondering if anyone has ever added an external pulldown on GPIO1 on the feather and if that might also be a solution?

Thanks for your help!

User avatar
surffl
 
Posts: 10
Joined: Fri Feb 23, 2024 1:44 am

Re: Unexpected Deep Sleep Behavior

Post by surffl »

Well, just to close this out, from the testing I have done it seems like using the built-in D1 button on this board as a wakeup trigger isn't an option despite it being talked about in the documentation. I tested my code with an external button on a different GPIO pin that isn't shared with Tx for serial and it works as expected.

Kind of frustrating that the pin mapping seems to make all the built in buttons useless for being a reliable trigger for waking up the esp32 from deep sleep...

Post Reply
Please be positive and constructive with your questions and comments.

Return to “Feather - Adafruit's lightweight platform”