Advertisement
CHU2

ELEC 2 LAB 4

May 11th, 2025
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 1.14 KB | Source Code | 0 0
  1. #include <Arduino.h>
  2. #include "esp_sleep.h"
  3.  
  4. const gpio_num_t button_pin = GPIO_NUM_33;       // pushbutton >> GPIO33 & GND
  5. const gpio_num_t led_pin    = GPIO_NUM_2;        // on-board LED >> GPIO2
  6.  
  7. // persist LED state across deep-sleep resets
  8. RTC_DATA_ATTR bool led_state = false;            
  9.  
  10. void setup() {
  11.   Serial.begin(115200);
  12.   pinMode(led_pin, OUTPUT);
  13.   pinMode(button_pin, INPUT_PULLUP);             // enable internal pull-up
  14.  
  15.   // check wake-up reason
  16.   if (esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_EXT0) {
  17.     // toggle persisted LED state
  18.     led_state = !led_state;
  19.     digitalWrite(led_pin, led_state);             // update LED
  20.     Serial.println("woke by button");             // report over Serial
  21.     Serial.flush();                               // ensure data is sent
  22.   }
  23.  
  24.   // configure external wake-up (ext0) on button_pin LOW
  25.   esp_sleep_enable_ext0_wakeup(button_pin, 0);    // 0 = wake on LOW
  26.   delay(50);                                      // allow Serial to flush
  27.   esp_deep_sleep_start();                         // enter deepest sleep
  28. }
  29.  
  30. void loop() {
  31.   // not used after deep sleep entry
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement