Advertisement
CHU2

ELEC 2 LAB 4.1

May 11th, 2025
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 1.24 KB | Source Code | 0 0
  1. #include <Arduino.h>
  2. #include "esp_sleep.h"
  3.  
  4. const gpio_num_t led_pin     = GPIO_NUM_2;              // on-board LED (optional)
  5. RTC_DATA_ATTR uint32_t boot_count = 0;                  // persists through deep-sleep
  6.  
  7. void setup() {
  8.   Serial.begin(115200);
  9.   pinMode(led_pin, OUTPUT);
  10.  
  11.   // 1) Detect wake-up reason
  12.   esp_sleep_wakeup_cause_t wake_reason = esp_sleep_get_wakeup_cause();  
  13.   if (wake_reason == ESP_SLEEP_WAKEUP_TIMER) {
  14.     boot_count++;
  15.     Serial.printf("woke by timer #%u\n", boot_count);  // print wake count
  16.   } else {
  17.     Serial.println("power-on or other reset");
  18.   }
  19.  
  20.   // (Optional) toggle LED to show activity
  21.   digitalWrite(led_pin, boot_count % 2);
  22.  
  23.   // 2) Main task:
  24.   Serial.println("running main task");
  25.  
  26.   // 3) Configure next wake by timer in 5 seconds
  27.   const uint64_t wake_time_us = 5ULL * 1000000ULL;     // 5 s
  28.   esp_sleep_enable_timer_wakeup(wake_time_us);
  29.  
  30.   Serial.println("entering deep sleep for 5 s");
  31.   Serial.flush();                                       // ensure all data is sent
  32.   delay(50);                                            // allow USB-CDC re-enumeration
  33.   esp_deep_sleep_start();                               // enter deep sleep
  34. }
  35.  
  36. void loop() {
  37.   // never reached
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement