← Back to ESP32-C3 & LED Filament Project Update to ESP32-S3
Source Code
ESP32-C3 & LED Filament Project Update to ESP32-S3 · cpp
#include <esp_task_wdt.h>
#include <esp_sleep.h>
#include <WiFi.h> // Added to ensure radio can be explicitly shut down
// 4 hours in milliseconds, 20 hours in microseconds for deep sleep
#define ON_TIME_MS 14400000
#define OFF_TIME_US 72000000000ULL
// Updated for XIAO pin nomenclature
const int ledPin = D0;
const int pwmFreq = 5000;
const int pwmRes = 8; // 0-255 brightness
void setup() {
// Explicitly disable the power-hungry dual-core radios
WiFi.mode(WIFI_OFF);
// 80MHz is the lowest stable clock for the S3 without causing peripheral timing issues
setCpuFrequencyMhz(80);
// Core 3.x Watchdog Timer Initialization for Dual-Core (S3)
esp_task_wdt_config_t twdt_config = {
.timeout_ms = 5000,
.idle_core_mask = (1 << portNUM_PROCESSORS) - 1, // Monitors both cores
.trigger_panic = true,
};
esp_task_wdt_init(&twdt_config);
esp_task_wdt_add(NULL);
// Core 3.x PWM Initialization
ledcAttach(ledPin, pwmFreq, pwmRes);
unsigned long startTime = millis();
// Run flicker loop for exactly 4 hours
while (millis() - startTime < ON_TIME_MS) {
esp_task_wdt_reset(); // Feed the watchdog
// Lowered bounds: Flicker between ~4% and ~23% brightness
int brightness = random(10, 60);
ledcWrite(ledPin, brightness);
delay(random(20, 150));
}
// Turn off LED to prevent parasitic draw before sleeping
ledcWrite(ledPin, 0);
// Enter Deep Sleep for 20 hours
esp_sleep_enable_timer_wakeup(OFF_TIME_US);
esp_deep_sleep_start();
}
void loop() {
// Execution never reaches here due to deep sleep
}