Blink, with a breakpoint, on an ESP32-C3
Stop the board on the line that lights the LED, and see it frozen mid-blink with its registers. The whole build, an ESP32-C3 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.
Teaching? Assign it to your class in one click.
Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $13.15. On Mokxi they're free.
What you'll learn
- Setting a breakpoint in the gutter
- Pausing a running program on one line
- Reading registers while stopped
- Stepping one line at a time
- Difficulty: Beginner
- Time: About 10 minutes
- Board: ESP32-C3-DevKitM-1
- Parts: 4
How it works
An ESP32-C3 board drives a blue LED from pin 8 through a 220 ohm resistor, with the other leg of the LED on ground. The sketch turns the pin on for half a second, then off for half a second, over and over.
The point here is the debugger. Click the gutter beside the digitalWrite(LED_BUILTIN, HIGH) line to set a red dot, then press Run. The board stops on that line before it runs, with the clock frozen too. The Debug tab shows where it stopped, the registers and a watch list. Step over runs one line, and Continue lets it go.
Parts list
4 parts, plus the jumper wires. They retail for about $13.15; on Mokxi they're free.
- 1 × ESP32-C3-DevKitM-1In Handheld Console Kit, Desk Buddy Kit.
- 1 × Full-size breadboard
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, blueIn Starter Kit, Halloween Prop Kit, Logic Lab 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, blue pin C | ESP32-C3-DevKitM-1 pin GND |
| Resistor, 220 Ω pin 1 | ESP32-C3-DevKitM-1 pin 8 |
| LED, blue pin A | Resistor, 220 Ω pin 2 |
The note on the canvas
Word for word, as it sits beside the circuit in the editor.
Set a breakpoint
1. Press Run and watch the LED blink.
2. Click the gutter left of the line numbers, beside digitalWrite(LED_BUILTIN, HIGH). A red dot appears.
3. Press Run again. The board stops on that line, before it runs, and the LED stays exactly as lit as it was. So does everything else: the clock stops with it.
4. The Debug tab, beside the serial monitor, has where it stopped, the registers, and a watch list.
5. Step over runs one line. Continue lets it go.
A hollow dot means that line generated no instruction, so nothing can stop there.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Blink: the smallest thing a board can do.
//
// GPIO 8 is the LED on the ESP32-C3-DevKitM-1, so LED_BUILTIN is 8. Wire an
// LED and a resistor from GPIO 8 to GND to see it on the breadboard too.
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Runs once. It sets LED_BUILTIN as an output. On this board that is GPIO 8, the same pin your wired LED uses.
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}loop sketch.ino
Turns the LED on, waits 500 ms, turns it off, waits 500 ms. Put your breakpoint on the digitalWrite(LED_BUILTIN, HIGH) line and the board stops right before the LED lights.
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The breakpoint dot is hollow and never stops This project
A hollow dot means that line made no instruction. Move it to a line with real code, like adigitalWrite. - The LED never lights This project
Check the LED direction: the flat side (cathode) goes toward ground, and the resistor goes between pin 8 and the LED. - 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. - Upload fails or the board is not found by the computer. ESP32-C3-DevKitM-1
Pick the ESP32-C3 board in your tools and the right port. If it still fails, hold BOOT, tap RESET, then release BOOT to enter download mode and try again. - GPIO pins act strangely or the board resets. ESP32-C3-DevKitM-1
GPIO is 3.3 V only, so never feed it 5 V. GPIO 18 and 19 are USB, and GPIO 2, 8 and 9 are boot strapping pins. Avoid loading them at reset.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.