Proyek bawaan · ESP32-C3-DevKitM-1

Knight Rider di ESP32-C3

ESP32-C3 menggerakkan titik terang bolak-balik di enam LED, satu GPIO per LED. Seluruh rakitannya, ESP32-C3 dan 12 komponen lainnya, berjalan di browsermu dengan firmware di bawah; buka di editor untuk mengubah rangkaian atau kodenya, lalu jalankan lagi.

Mengajar? Berikan sebagai tugas ke kelasmu dengan sekali klik.

Pemula About 20 minutes to build and run. Berjalan di browsermu. Gratis, dan tidak perlu akun.

Komponennya dijual sekitar $13.90. Di Mokxi, semuanya gratis.

Knight Rider · ESP32-C3-DevKitM-1live0.000 s 0.00x
Klik untuk membukanya di editor
Rangkaiannya sendiri, berjalan di simulator di sini. Tekan yang bisa ditekan; klik yang lainnya untuk membukanya di editor.

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: Pemula
  • Time: About 20 minutes
  • Board: ESP32-C3-DevKitM-1
  • Parts: 14

Cara kerjanya

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.

Daftar komponen

14 komponen, ditambah kabel jumper. Harganya sekitar $13.90; di Mokxi, semuanya gratis.

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.

Komponen dan 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

Kodenya

Firmware persis seperti yang dibuka di editor. Ubah satu baris di sana lalu tekan Jalankan: kodenya dikompilasi di 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.

Ubah dan simpan

Buka di editor, ubah rangkaian atau kodenya, lalu simpan versimu di akun gratis.