Built-in project · ESP32-C3-DevKitM-1

Knight Rider on an ESP32-C3

An ESP32-C3 sweeps a bright dot back and forth along six LEDs, one GPIO each. The whole build, an ESP32-C3 and 12 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 20 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $13.90. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • One GPIO pin per LED
  • Arrays to hold pin numbers
  • Nested counting loops to sweep a light
  • Writing a helper function to show one state
  • Difficulty: Beginner
  • Time: About 20 minutes
  • Board: ESP32-C3-DevKitM-1
  • Parts: 14

How it works

Six red LEDs sit in a row, each on its own GPIO pin with a 220 ohm resistor. The LAMPS array lists the pins in breadboard order: 7, 6, 5, 4, 3, 2. Only one is on at a time.

The dot sweeps forward along the row, then back. The return trip stops one short of each end, so the end lamps light once per pass and the middle ones twice. That keeps the bounce from looking like it sticks at the ends.

Each step lasts 90 milliseconds.

Parts list

14 parts, plus the jumper wires. They retail for about $13.90; on Mokxi they're free.

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 pinConnects to
Resistor, 220 Ω (1) pin 1ESP32-C3-DevKitM-1 pin 2
Resistor, 220 Ω (2) pin 1ESP32-C3-DevKitM-1 pin 3
LED, red (1) pin CESP32-C3-DevKitM-1 pin GND
LED, red (2) pin CESP32-C3-DevKitM-1 pin GND
LED, red (3) pin CESP32-C3-DevKitM-1 pin GND
LED, red (4) pin CESP32-C3-DevKitM-1 pin GND
LED, red (5) pin CESP32-C3-DevKitM-1 pin GND
LED, red (6) pin CESP32-C3-DevKitM-1 pin GND
Resistor, 220 Ω (3) pin 1ESP32-C3-DevKitM-1 pin 4
Resistor, 220 Ω (4) pin 1ESP32-C3-DevKitM-1 pin 5
Resistor, 220 Ω (5) pin 1ESP32-C3-DevKitM-1 pin 6
Resistor, 220 Ω (6) pin 1ESP32-C3-DevKitM-1 pin 7
LED, red (1) pin AResistor, 220 Ω (1) pin 2
LED, red (2) pin AResistor, 220 Ω (2) pin 2
LED, red (3) pin AResistor, 220 Ω (3) pin 2
LED, red (4) pin AResistor, 220 Ω (4) pin 2
LED, red (5) pin AResistor, 220 Ω (5) pin 2
LED, red (6) pin AResistor, 220 Ω (6) pin 2

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · ESP32-C3-DevKitM-1
// Knight Rider: a dot sweeping back and forth along six LEDs.
//
// One GPIO per lamp, which is the simplest thing that can work and the reason
// it is the first thing anyone builds with more than one LED. GPIO 7, 6, 5 and
// 4 come out next to each other on the DevKitM-1's left header and GPIO 3 and 2
// a little further along, so the array below is the LEDs in the order they sit
// on the breadboard, not in pin order.
//
// The sweep turns round one short of each end, so the two end lamps are lit
// once per pass and the middle ones twice, which is what stops the bounce
// looking like it sticks at the ends.

const int LAMPS[] = {7, 6, 5, 4, 3, 2};
const int COUNT = 6;
const int STEP_MS = 90;

static void show(int lit) {
  for (int i = 0; i < COUNT; i++) {
    digitalWrite(LAMPS[i], i == lit ? HIGH : LOW);
  }
}

void setup() {
  for (int i = 0; i < COUNT; i++) {
    pinMode(LAMPS[i], OUTPUT);
    digitalWrite(LAMPS[i], LOW);
  }
  Serial.begin(115200);
  Serial.println("Mokxi ESP32-C3: six lamps, one dot");
}

void loop() {
  for (int i = 0; i < COUNT; i++) {
    show(i);
    delay(STEP_MS);
  }
  for (int i = COUNT - 2; i > 0; i--) {
    show(i);
    delay(STEP_MS);
  }
}

Code walkthrough

The key functions of the sketch, and what each one does.

show sketch.ino

Takes the number of the lamp to light. It loops over all six, writing HIGH to the one that matches and LOW to the rest. So only one LED is on at a time.

static void show(int lit) {
  for (int i = 0; i < COUNT; i++) {
    digitalWrite(LAMPS[i], i == lit ? HIGH : LOW);
  }
}

setup sketch.ino

Makes each pin in LAMPS an output and writes it LOW. It opens serial at 115200 baud and prints a message.

void setup() {
  for (int i = 0; i < COUNT; i++) {
    pinMode(LAMPS[i], OUTPUT);
    digitalWrite(LAMPS[i], LOW);
  }
  Serial.begin(115200);
  Serial.println("Mokxi ESP32-C3: six lamps, one dot");
}

loop sketch.ino

Runs one pass forward from lamp 0 to 5, calling show and waiting STEP_MS each time. Then a second loop runs back from COUNT - 2 down to 1, skipping both end lamps so they are not lit twice in a row.

void loop() {
  for (int i = 0; i < COUNT; i++) {
    show(i);
    delay(STEP_MS);
  }
  for (int i = COUNT - 2; i > 0; i--) {
    show(i);
    delay(STEP_MS);
  }
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The dot jumps out of order This project
    The pin order in LAMPS must match the physical order of the LEDs on the board.
  • One LED never lights This project
    Check its LED is the right way around: the long leg (anode) goes to the resistor, the short leg 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.
  • 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.