🛠 Fondu de LED avec le PWM LEDC

ESP32 (original, ESP32-WROOM-32 class) · debutant · Schéma : IMG-esp32-debutant-2-ledc-pwm-fade.svg — ESP32, broche GPIO25 reliée par une résistance à une LED, courbe d'intensité en dents de scie

Objectives

Control the brightness of an LED using the ESP32's LEDC peripheral: PWM is generated by 8 independent channels (with configurable frequency and resolution), unlike a simple digitalWrite. You will learn how to configure a channel, attach it to a pin, and perform a rising and falling fade.

Hardware

Complete code

const uint8_t LED_PIN = 25;   // eviter GPIO2, broche de strapping
const uint8_t CANAL   = 0;    // 0 a 7 sur ESP32

void setup {
  ledcSetup(CANAL, 5000, 8);  // 5 kHz, resolution 8 bits (0..255)
  ledcAttachPin(LED_PIN, CANAL);
}

void loop {
  for (int v = 0; v <= 255; v++) { ledcWrite(CANAL, v); delay(8); }
  for (int v = 255; v >= 0; v--) { ledcWrite(CANAL, v); delay(8); }
}

Expected verification

The LED turns on and off gradually (a smooth fade of approximately 2 seconds per cycle), without visible steps. With an oscilloscope on GPIO25, you observe a 5 kHz square wave whose duty cycle follows the ramp.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials