Created
June 28, 2024 08:38
-
-
Save morgz/c566e88cf9162db756329d90b4cd493e to your computer and use it in GitHub Desktop.
Pin 19
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <esp_sleep.h> | |
// Define the pin connected to the V_USB signal | |
#define WAKEUP_PIN GPIO_NUM_19 | |
void printWakeupReason() { | |
esp_sleep_wakeup_cause_t 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\n", wakeup_reason); | |
break; | |
} | |
} | |
void setup() { | |
// Initialize Serial communication for debugging | |
Serial.begin(115200); | |
delay(1000); // Give time for the serial monitor to start | |
// Print a message to indicate that the ESP32 is awake | |
Serial.println("ESP32 is awake"); | |
// Configure the wakeup pin | |
pinMode(WAKEUP_PIN, INPUT); | |
// Read and print the initial state of the wakeup pin | |
int pinState = digitalRead(WAKEUP_PIN); | |
int analogPinState = analogRead(WAKEUP_PIN); | |
Serial.print("Initial digital state of pin 19 (V_USB): "); | |
Serial.println(pinState); | |
Serial.print("Initial analog state of pin 19 (V_USB): "); | |
Serial.println(analogPinState); | |
// Enable wakeup on pin 19 for a LOW -> HIGH change (assuming V_USB goes high when USB is plugged in) | |
esp_sleep_enable_ext0_wakeup(WAKEUP_PIN, 1); // 1 for HIGH level, 0 for LOW level | |
// Print a message indicating that the ESP32 is going to sleep | |
Serial.println("Going to deep sleep now"); | |
// Delay to allow the message to be printed before going to sleep | |
delay(1000); | |
// Start deep sleep | |
esp_deep_sleep_start(); | |
} | |
void loop() { | |
// This part of the code will be executed when the ESP32 wakes up | |
// Print the wakeup reason | |
printWakeupReason(); | |
// Read and print the state of the wakeup pin after wakeup | |
int pinState = digitalRead(WAKEUP_PIN); | |
int analogPinState = analogRead(WAKEUP_PIN); | |
Serial.print("State of pin 19 (V_USB) after wakeup: "); | |
Serial.println(pinState); | |
Serial.print("Analog state of pin 19 (V_USB) after wakeup: "); | |
Serial.println(analogPinState); | |
// Go back to sleep | |
Serial.println("Going back to deep sleep"); | |
delay(1000); | |
esp_deep_sleep_start(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment