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 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 code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// 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);
}
Parts list
14 parts, plus the jumper wires. They retail for about $14; on Mokxi they're free.
- 1 × ESP32-C3-DevKitM-1
- 1 × Full-size breadboard
- 4 × Resistor, 220 Ω
- 1 × LED, red
- 1 × LED, yellow
- 1 × LED, green
- 1 × LED, blue
- 4 × Pushbutton
How it is wired
13 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- ESP32-C3-DevKitM-1 pin 2; Resistor, 220 Ω (1) pin 1
- ESP32-C3-DevKitM-1 pin 3; Resistor, 220 Ω (2) pin 1
- Ground: ESP32-C3-DevKitM-1 pin GND; LED, red pin C; LED, yellow pin C; LED, green pin C; LED, blue pin C; Pushbutton (1) pin 2a; Pushbutton (1) pin 2b; Pushbutton (2) pin 2a; Pushbutton (2) pin 2b; Pushbutton (3) pin 2a; Pushbutton (3) pin 2b; Pushbutton (4) pin 2a; Pushbutton (4) pin 2b
- ESP32-C3-DevKitM-1 pin 4; Resistor, 220 Ω (3) pin 1
- ESP32-C3-DevKitM-1 pin 5; Resistor, 220 Ω (4) pin 1
- ESP32-C3-DevKitM-1 pin 18; Pushbutton (1) pin 1a; Pushbutton (1) pin 1b
- ESP32-C3-DevKitM-1 pin 19; Pushbutton (2) pin 1a; Pushbutton (2) pin 1b
- ESP32-C3-DevKitM-1 pin 20; Pushbutton (3) pin 1a; Pushbutton (3) pin 1b
- ESP32-C3-DevKitM-1 pin 21; Pushbutton (4) pin 1a; Pushbutton (4) pin 1b
- Resistor, 220 Ω (1) pin 2; LED, red pin A
- Resistor, 220 Ω (2) pin 2; LED, yellow pin A
- Resistor, 220 Ω (3) pin 2; LED, green pin A
- Resistor, 220 Ω (4) pin 2; LED, blue pin A
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.