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

ESP32-C3 Simon

Four LEDs flash a growing sequence, four buttons play it back. 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.

Advanced About 35 minutes to build and run. Runs in your browser. Free, and no account needed.

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

Step by step, with the wiring and the common mistakes: Arduino Simon Says Game: Four LEDs, Four Buttons

The circuit runs here once the page has loaded.

What you'll learn

  • Arrays that tie LEDs and buttons together by index
  • Input with internal pull-up resistors
  • Storing and replaying a sequence in memory
  • Waiting for a button release before reading the next one
  • Why a waiting loop needs a small delay on an ESP32
  • Difficulty: Advanced
  • Time: About 35 minutes
  • Board: ESP32-C3-DevKitM-1
  • Parts: 14

How it works

This is the classic memory game. Four LEDs flash a random sequence. You repeat it on four buttons. If you are right, the sequence gets one step longer. If you are wrong, the LEDs flash three times and a new game starts.

The LEDs sit on pins 2, 3, 4 and 5, each with a 220 ohm resistor. The buttons sit on pins 18 to 21 and connect to ground. The board's built-in pull-ups keep each input high until you press its button.

The sequence can grow to 16 steps. There is no time limit on your answers.

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 pin CESP32-C3-DevKitM-1 pin GND
LED, yellow pin CESP32-C3-DevKitM-1 pin GND
LED, green pin CESP32-C3-DevKitM-1 pin GND
LED, blue pin CESP32-C3-DevKitM-1 pin GND
Pushbutton (1) pin 2aESP32-C3-DevKitM-1 pin GND
Pushbutton (1) pin 2bESP32-C3-DevKitM-1 pin GND
Pushbutton (2) pin 2aESP32-C3-DevKitM-1 pin GND
Pushbutton (2) pin 2bESP32-C3-DevKitM-1 pin GND
Pushbutton (3) pin 2aESP32-C3-DevKitM-1 pin GND
Pushbutton (3) pin 2bESP32-C3-DevKitM-1 pin GND
Pushbutton (4) pin 2aESP32-C3-DevKitM-1 pin GND
Pushbutton (4) pin 2bESP32-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
Pushbutton (1) pin 1aESP32-C3-DevKitM-1 pin 18
Pushbutton (1) pin 1bESP32-C3-DevKitM-1 pin 18
Pushbutton (2) pin 1aESP32-C3-DevKitM-1 pin 19
Pushbutton (2) pin 1bESP32-C3-DevKitM-1 pin 19
Pushbutton (3) pin 1aESP32-C3-DevKitM-1 pin 20
Pushbutton (3) pin 1bESP32-C3-DevKitM-1 pin 20
Pushbutton (4) pin 1aESP32-C3-DevKitM-1 pin 21
Pushbutton (4) pin 1bESP32-C3-DevKitM-1 pin 21
LED, red pin AResistor, 220 Ω (1) pin 2
LED, yellow pin AResistor, 220 Ω (2) pin 2
LED, green pin AResistor, 220 Ω (3) pin 2
LED, blue pin AResistor, 220 Ω (4) 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
// Simon: a memory game on four buttons and four LEDs.
//
// Wiring, board to breadboard:
//   LEDs (each through a 220 ohm resistor to GND): GPIO 2, 3, 4, 5
//   Buttons (each to GND, internal pull-up):       GPIO 18, 19, 20, 21
//
// The game plays a sequence of one to sixteen lamps, one flash at a time, then
// waits for the same sequence back on the matching buttons. Get it right and
// the sequence grows by one; get it wrong and the game reports how far you got
// and starts over. There is no timeout: the buttons are read in a loop that
// pauses a millisecond each time round, so a player can take as long as they
// like on each step.
//
// `random()` is deliberately left unseeded, so the sequence is the same every
// time the board starts: the yellow lamp on GPIO 3 first, every run. Seeding
// it is the lesson's own closing exercise, and seeding it from micros() here
// would make the first lamp a function of how many instructions setup() took,
// which is not something a sketch should depend on.

const int LEDS[] = {2, 3, 4, 5};
const int BUTTONS[] = {18, 19, 20, 21};
const int COUNT = 4;
const int MAX_LEN = 16;

uint8_t sequence[MAX_LEN];
int length = 1;

static void flash(int i, int ms) {
  digitalWrite(LEDS[i], HIGH);
  delay(ms);
  digitalWrite(LEDS[i], LOW);
  delay(ms / 2);
}

// A short flourish so a fresh round looks different from a step of the game.
static void allFlash(int times, int ms) {
  for (int t = 0; t < times; t++) {
    for (int i = 0; i < COUNT; i++) digitalWrite(LEDS[i], HIGH);
    delay(ms);
    for (int i = 0; i < COUNT; i++) digitalWrite(LEDS[i], LOW);
    delay(ms);
  }
}

static int pressedButton() {
  for (int i = 0; i < COUNT; i++) {
    if (digitalRead(BUTTONS[i]) == LOW) return i;
  }
  return -1;
}

static void waitForRelease() {
  while (pressedButton() >= 0) delay(5);
  delay(20);  // a little settle time, cheaper than real debounce logic
}

static void newGame() {
  length = 1;
  for (int i = 0; i < MAX_LEN; i++) sequence[i] = (uint8_t)random(COUNT);
  Serial.println("new game");
  delay(400);
}

void setup() {
  for (int i = 0; i < COUNT; i++) {
    pinMode(LEDS[i], OUTPUT);
    digitalWrite(LEDS[i], LOW);
  }
  for (int i = 0; i < COUNT; i++) pinMode(BUTTONS[i], INPUT_PULLUP);
  Serial.begin(115200);
  Serial.println("Mokxi ESP32-C3: Simon, four lamps and four buttons");
  newGame();
}

void loop() {
  // Play the sequence so far.
  for (int i = 0; i < length; i++) flash(sequence[i], 350);
  delay(200);

  // Read it back.
  for (int i = 0; i < length; i++) {
    // delay(1) while waiting is not decoration: it lets the chip's idle task
    // run. A loop that spins without it is rebooted by the task watchdog
    // after five seconds, on a real ESP32-C3 as here, and the game restarts.
    int got = -1;
    while ((got = pressedButton()) < 0) delay(1);
    digitalWrite(LEDS[got], HIGH);

    if (got != sequence[i]) {
      digitalWrite(LEDS[got], LOW);
      Serial.print("wrong at step ");
      Serial.print(i + 1);
      Serial.print(" of ");
      Serial.println(length);
      allFlash(3, 120);
      waitForRelease();
      newGame();
      return;
    }

    waitForRelease();
    digitalWrite(LEDS[got], LOW);
  }

  Serial.print("round ");
  Serial.print(length);
  Serial.println(" correct");
  length++;
  if (length > MAX_LEN) {
    Serial.println("full sequence, starting over");
    allFlash(5, 100);
    newGame();
    return;
  }
  delay(500);
}

Code walkthrough

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

pressedButton sketch.ino

Checks each button in BUTTONS and returns the number of the first one reading LOW, which means pressed. It returns -1 when nothing is pressed. The same index picks the matching LED.

static int pressedButton() {
  for (int i = 0; i < COUNT; i++) {
    if (digitalRead(BUTTONS[i]) == LOW) return i;
  }
  return -1;
}

newGame sketch.ino

Resets the length to 1 and fills the whole sequence array with random numbers from 0 to 3. The sequence is the same every time the board starts, because random is never seeded.

static void newGame() {
  length = 1;
  for (int i = 0; i < MAX_LEN; i++) sequence[i] = (uint8_t)random(COUNT);
  Serial.println("new game");
  delay(400);
}

loop sketch.ino

Each round plays the sequence so far, then reads your presses one at a time. A wrong press ends the game. A full correct round adds one step. The delay(1) inside the waiting loop gives the chip time to run its background tasks.

void loop() {
  // Play the sequence so far.
  for (int i = 0; i < length; i++) flash(sequence[i], 350);
  delay(200);

  // Read it back.
  for (int i = 0; i < length; i++) {
    // delay(1) while waiting is not decoration: it lets the chip's idle task
    // run. A loop that spins without it is rebooted by the task watchdog
    // after five seconds, on a real ESP32-C3 as here, and the game restarts.
    int got = -1;
    while ((got = pressedButton()) < 0) delay(1);
    digitalWrite(LEDS[got], HIGH);

    if (got != sequence[i]) {
      digitalWrite(LEDS[got], LOW);
      Serial.print("wrong at step ");
      Serial.print(i + 1);
      Serial.print(" of ");
      Serial.println(length);
      allFlash(3, 120);
      waitForRelease();
      newGame();
      return;
    }

    waitForRelease();
    digitalWrite(LEDS[got], LOW);
  // ... 14 more lines in the editor

Troubleshooting

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

  • A button lights the wrong LED or does nothing This project
    Check that each button goes to the pin listed in BUTTONS, and that its other side goes to ground.
  • An LED never flashes This project
    LEDs are polarity sensitive. The longer leg, the anode, faces the resistor and the pin. The other leg goes to ground.
  • The game plays the same sequence after every reset This project
    That is expected here. To change it, seed the random generator in setup with a changing value.
  • 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.
  • The input reads random HIGH and LOW when the button is not pressed. Pushbutton
    A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.