ESP32-C6 fade
Breathes an LED on GPIO 10 with analogWrite, so the waveform comes out of the LEDC hardware and the core sleeps between steps. The whole build, an ESP32-C6 and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Runs in your browser. Free, and no account needed.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Fade: a breathing LED on hardware PWM.
//
// LEDC, the chip's LED PWM controller, makes the waveform; the sketch only
// changes a number thirty-three times a second and sleeps in between. The core
// is parked in `wfi` for almost all of the two seconds a breath takes, and the
// edges still land where they should, because the PWM is in the hardware and
// not in the loop.
//
// analogWrite(pin, 0..255) is all this needs. Any GPIO will do: LEDC reaches
// the pins through the GPIO matrix, so there is no fixed set of `~` pins on
// this board.
//
// Wire an LED and a 220 R resistor from GPIO 10 to ground.
const uint8_t LED = 10;
const int STEPS = 64;
const unsigned long STEP_MS = 30; // a whole breath takes about two seconds
// One cycle of (1 - cos)/2 scaled to 0..255. A linear ramp looks wrong: the
// eye is roughly logarithmic, so an even climb in duty is an uneven climb in
// brightness, and this is the shape that reads as breathing.
const uint8_t breath[STEPS] = {
0, 1, 2, 6, 10, 16, 22, 29,
38, 47, 57, 68, 79, 91, 103, 116,
128, 140, 153, 165, 177, 188, 199, 209,
218, 226, 234, 240, 246, 250, 253, 255,
255, 255, 253, 250, 246, 240, 234, 226,
218, 209, 199, 188, 177, 165, 153, 140,
128, 116, 103, 91, 79, 68, 57, 47,
38, 29, 22, 16, 10, 6, 2, 1,
};
void setup() {
// Nothing to do: the first analogWrite attaches the pin to a free channel.
}
void loop() {
for (int step = 0; step < STEPS; step++) {
analogWrite(LED, breath[step]);
delay(STEP_MS);
}
}
Parts list
4 parts, plus the jumper wires. Every one is in the editor's parts bin.
- 1 × ESP32-C6-DevKitC-1
- 1 × Full-size breadboard
- 1 × Resistor, 220 Ω
- 1 × LED, yellow
How it is wired
3 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- ESP32-C6-DevKitC-1 pin 10; Resistor, 220 Ω pin 1
- Ground: ESP32-C6-DevKitC-1 pin GND; LED, yellow pin C
- Resistor, 220 Ω pin 2; LED, yellow pin A
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.