respiration.ino

ESP32-S3 · File : respiration.ino · Language : Arduino · Use case : Effet respiration sur LED avec le PWM LEDC

The hardware PWM (LEDC) generates the carrier by itself: analogWrite applies a 0-255 duty cycle and the LED breathes without burning CPU time. The chosen frequency avoids PWM whine.

Hardware required

An ESP32-S3 board (dual-core Xtensa with vector instructions, Wi-Fi + BLE 5, native USB) such as a DevKitC-1, XIAO or T-Display-S3, powered over USB. 1 LED + 330 Ω resistor (or common RGB LED + anode on the pin).

Wiring

LED on 48 through 330 Ω to GND. On pins also wired to flash memory, pick another GPIO.

How it works

analogWrite/analogWriteFrequency configure an LEDC channel; the loop only ramps the duty cycle 0-255 while the peripheral generates the signal. In the Arduino IDE (esp32 package), select « ESP32S3 Dev Module », enable « USB CDC On Boot », then Upload. Many boards put a WS2812 on GPIO48.

Going further

Three phase-shifted LEDC channels for an RGB LED, or a small MOSFET stage to drive a power LED strip.

respiration.ino

// Respiration LED par le contrôleur LEDC (PWM matériel du SoC).
const int LED = 48;
const int FREQ = 5000;    // 5 kHz : inaudible, pas de sifflement
const int RESOLUTION = 8; // 256 niveaux

int niveau = 0;
int sens = 4;             // pas par étape

void setup() {
  analogWriteResolution(RESOLUTION);
  analogWriteFrequency(FREQ);
  analogWrite(LED, 0);
}

void loop() {
  niveau += sens;
  if (niveau >= 255 || niveau <= 0) sens = -sens;
  analogWrite(LED, niveau);
  delay(8);               // 255/4*8 ms ≈ 0,5 s par rampe
}

← Back to the board