breadboard
docs

Example project · Builds & projects

ESP32-S31 four fading LEDs

Four 220 Ω LED loads on GPIO8/9/2/3 follow interrupt-rearmed LEDC fades and fixed-duty steps. GPIO2/3 are inverted, so their LEDs fade in the opposite direction. Open Serial for the four phases. Enable verbose in Serial to see the stock INFO lines. Unmodified ESP-IDF v6.1.1 on the bounded S31 simulator profile; no physical flashing.

The ESP32-S31 four fading LEDs circuit as rendered by the simulator

How it works

ESP-IDF's ledc_fade example uses the LED PWM controller (LEDC) with a 13-bit duty resolution, so a duty of 4000 is a little under half of the 8192 possible steps. Four channels drive GPIO8, GPIO9, GPIO2 and GPIO3, each through a 220 Ω resistor to a red LED. The last two channels have output_invert set, so their pins are high when the others are low.

The program repeats four phases. It starts a hardware fade on all four channels up to duty 4000, requested over 3000 ms, then a fade back to 0, then sets duty 4000 directly, waits 1 s, sets 0 and waits 1 s again. The fades run in the LEDC hardware, and each channel's fade-end interrupt releases the program to start the next phase. Because of the inversion, the LEDs on GPIO2 and GPIO3 dim while the other two brighten, and the other way round.

What to watch

Serial prints the four phase lines, from 1. LEDC fade up to duty = 4000 to 4. LEDC set duty = 0 without fade. During phases 1 and 2 the LEDs change slowly; during 3 and 4 they jump. Open Scope on GPIO8 and GPIO2 together to see the two pulse widths move in opposite directions.

What's on the bench

  • 4× LED
  • 4× Resistor
  • Battery
  • ESP32-S31 (development source)

How it's wired

  • Battery · pos→ESP32-S31 (development source) · vin
  • Battery · neg→ESP32-S31 (development source) · gnd
  • ESP32-S31 (development source) · g8→Resistor · a
  • Resistor · b→LED · a
  • LED · k→ESP32-S31 (development source) · gnd
  • ESP32-S31 (development source) · g9→Resistor · a
  • Resistor · b→LED · a
  • LED · k→ESP32-S31 (development source) · gnd
  • ESP32-S31 (development source) · g2→Resistor · a
  • Resistor · b→LED · a
  • LED · k→ESP32-S31 (development source) · gnd
  • ESP32-S31 (development source) · g3→Resistor · a
  • Resistor · b→LED · a
  • LED · k→ESP32-S31 (development source) · gnd

This project runs prebuilt firmware on the emulated board; open it in the simulator to watch it work and inspect every part live.

Try this

  • Probe GPIO8 at the end of phase 1 and compare the high time with the period. It is about 4000 out of 8192 parts of the period, just under half.
  • Watch an inverted LED during phase 4: with duty 0 its pin stays high, so it is fully lit while the other two are dark.
  • Move one LED and its resistor from GPIO8 to another pad. That LED goes dark, because the stock program drives only these four GPIOs.

Related projects