Breathing LED on an ESP32-C3
One PWM pin, two LEDs: one sees the raw 1 kHz chopping, the other an RC filter smoothing it. The whole build, an ESP32-C3 and 6 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.
Teaching? Assign it to your class in one click.
Intermediate About 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $13.45. On Mokxi they're free.
What you'll learn
- Software PWM by timing a pin high and low
- Duty cycle as brightness
- An RC filter turning pulses into a smooth voltage
- Why a cosine table looks like breathing
- Comparing raw and filtered signals
- Difficulty: Intermediate
- Time: About 20 minutes
- Board: ESP32-C3-DevKitM-1
- Parts: 8
How it works
The sketch makes its own PWM on pin 8: a 1 kHz square wave whose high time sets the brightness. A table shaped like (1 minus cos) over 2 steps the high time up and down, so the light seems to breathe.
Pin 8 feeds two LEDs. The first goes straight through a 150 ohm resistor, so it sees every pulse. The second goes through a 330 ohm resistor and a 47 microfarad capacitor first. That RC filter smooths the pulses into a steady voltage.
Compare the two: one chops, one glows.
Parts list
8 parts, plus the jumper wires. They retail for about $13.45; on Mokxi they're free.
- 1 × ESP32-C3-DevKitM-1In Handheld Console Kit, Desk Buddy Kit.
- 1 × Full-size breadboard
- 2 × Resistor, 150 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 2 × LED, redIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × Resistor, 330 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Capacitor, 47 µFIn Power Bank Kit.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| LED, red (1) pin C | ESP32-C3-DevKitM-1 pin GND |
| Capacitor, 47 µF pin 2 | ESP32-C3-DevKitM-1 pin GND |
| LED, red (2) pin C | ESP32-C3-DevKitM-1 pin GND |
| Resistor, 150 Ω (1) pin 1 | ESP32-C3-DevKitM-1 pin 8 |
| Resistor, 330 Ω pin 1 | ESP32-C3-DevKitM-1 pin 8 |
| LED, red (1) pin A | Resistor, 150 Ω (1) pin 2 |
| Capacitor, 47 µF pin 1 | Resistor, 330 Ω pin 2 |
| Resistor, 150 Ω (2) pin 1 | Resistor, 330 Ω pin 2 |
| LED, red (2) pin A | Resistor, 150 Ω (2) pin 2 |
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 from software PWM on GPIO 8.
//
// The ESP32-C3 has an LED PWM peripheral, but Mokxi does not model it, so
// this bit-bangs it: a 1 kHz square wave whose high time is the brightness.
// delayMicroseconds is accurate to a couple of instructions in the simulator,
// so the waveform is clean.
//
// The table is one cycle of (1 - cos)/2, which is what makes the light look
// like it is breathing rather than ramping. Values are 0..250 microseconds
// times four, so a step is 0..1000 us of the 1000 us period.
const unsigned long PERIOD_US = 1000; // 1 kHz
const int STEPS = 64;
const int CYCLES_PER_STEP = 30; // 30 ms at each brightness
const uint8_t breath[STEPS] = {
0, 1, 2, 5, 10, 15, 21, 28,
37, 46, 56, 66, 77, 89, 101, 113,
125, 137, 149, 161, 173, 184, 194, 204,
213, 222, 229, 235, 240, 245, 248, 249,
250, 249, 248, 245, 240, 235, 229, 222,
213, 204, 194, 184, 173, 161, 149, 137,
125, 113, 101, 89, 77, 66, 56, 46,
37, 28, 21, 15, 10, 5, 2, 1,
};
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
for (int step = 0; step < STEPS; step++) {
unsigned long onTime = (unsigned long)breath[step] * 4;
unsigned long offTime = PERIOD_US - onTime;
for (int cycle = 0; cycle < CYCLES_PER_STEP; cycle++) {
if (onTime > 0) {
digitalWrite(LED_BUILTIN, HIGH);
delayMicroseconds(onTime);
}
if (offTime > 0) {
digitalWrite(LED_BUILTIN, LOW);
delayMicroseconds(offTime);
}
}
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Sets LED_BUILTIN as an output. On this board that is pin 8, which is where the circuit is wired.
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}loop sketch.ino
Walks through the 64 values of the breath table. Each value times four is the high time in microseconds out of a 1000 microsecond period. For CYCLES_PER_STEP cycles it holds the pin HIGH for that time and LOW for the rest, using delayMicroseconds, then moves to the next step.
void loop() {
for (int step = 0; step < STEPS; step++) {
unsigned long onTime = (unsigned long)breath[step] * 4;
unsigned long offTime = PERIOD_US - onTime;
for (int cycle = 0; cycle < CYCLES_PER_STEP; cycle++) {
if (onTime > 0) {
digitalWrite(LED_BUILTIN, HIGH);
delayMicroseconds(onTime);
}
if (offTime > 0) {
digitalWrite(LED_BUILTIN, LOW);
delayMicroseconds(offTime);
}
}
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Both LEDs look the same This project
Check the 330 ohm resistor and the 47 microfarad capacitor sit between pin 8 and the second LED's resistor. The capacitor's other leg goes to ground. - Nothing lights This project
Pin 8 must connect to both branches, and each LED's cathode must go to GND. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The LED does not light, or it lights once and then dies. LED
LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin. - A blue or white LED stays dark on 3.3 V. LED
Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED. - An electrolytic capacitor heats up, bulges or pops. Capacitor
Electrolytics have a polarity. The stripe marks the negative leg, which goes toward ground. Also keep the voltage under the rated value, such as 10 V or 16 V. - An RC circuit charges much slower or faster than expected. Capacitor
The time constant is R times C. A 10 k resistor with a 100 uF capacitor gives 1 second, and about 5 seconds to charge fully. Check that you read the value on the part right.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.