Built-in project · ESP32-C3-DevKitM-1
Dice on an ESP32-C3
A button rolls a die and an ESP32-C3 tumbles the number onto a seven-segment display. The whole build, an ESP32-C3 and 3 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.
IntermediateRuns in your browser. Free, and no account needed.
Hold the button
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.
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
// Electronic dice: press the button, watch it tumble, read the number.
//
// segments a b c d e f g -> GPIO 2 3 4 5 6 7 10
// the common cathode -> 220 ohm to GND
// GPIO 18 -> the roll button, to GND, internal pull-up
//
// The same game as the Uno's `dice` example on the other board, and the only
// thing that changes is the pin numbers, which is the point of wiring a
// display to plain GPIOs rather than to a peripheral.
//
// The display is common cathode, so a segment lights when its pin goes HIGH
// and all of the return current leaves through the one resistor on the common
// pin. That is the cheap way to wire one: the more segments are lit the less
// current each gets, so an 8 is dimmer than a 1. Seven resistors, one per
// segment, is the right way and costs six more parts.
//
// DIGITS[] is the usual seven-segment font, one bit per segment, bit 0 = a.
// The tumble is the roll repeated with a delay that grows each step, which
// reads as a die slowing down and settling.
const uint8_t SEG_PINS[] = {2, 3, 4, 5, 6, 7, 10};
const uint8_t BUTTON = 18;
// bit 0 a, 1 b, 2 c, 3 d, 4 e, 5 f, 6 g
const uint8_t DIGITS[] = {
0x3f, // 0
0x06, // 1
0x5b, // 2
0x4f, // 3
0x66, // 4
0x6d, // 5
0x7d, // 6
};
static void show(uint8_t mask) {
for (int i = 0; i < 7; i++) {
digitalWrite(SEG_PINS[i], (mask >> i) & 1u ? HIGH : LOW);
}
}
static bool pressed() { return digitalRead(BUTTON) == LOW; }
void setup() {
for (int i = 0; i < 7; i++) {
pinMode(SEG_PINS[i], OUTPUT);
}
pinMode(BUTTON, INPUT_PULLUP);
Serial.begin(115200);
Serial.println("Mokxi ESP32-C3: press the button to roll");
show(DIGITS[6]);
}
void loop() {
if (!pressed()) {
delay(10);
return;
}
int face = 1;
uint32_t step = 40;
for (int i = 0; i < 14; i++) {
face = (int)random(1, 7);
show(DIGITS[face]);
delay(step);
step += step / 4 + 5;
}
Serial.print("rolled ");
Serial.println(face);
while (pressed()) {
delay(10);
}
}
Parts list
5 parts, plus the jumper wires. Every one is in the editor's parts bin.
How it is wired
10 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; Seven segment display pin A
- ESP32-C3-DevKitM-1 pin 3; Seven segment display pin B
- Ground: ESP32-C3-DevKitM-1 pin GND; Resistor, 220 Ω pin 2; Pushbutton pin 2b
- ESP32-C3-DevKitM-1 pin 10; Seven segment display pin G
- ESP32-C3-DevKitM-1 pin 4; Seven segment display pin C
- ESP32-C3-DevKitM-1 pin 5; Seven segment display pin D
- ESP32-C3-DevKitM-1 pin 6; Seven segment display pin E
- ESP32-C3-DevKitM-1 pin 7; Seven segment display pin F
- ESP32-C3-DevKitM-1 pin 18; Pushbutton pin 1b
- Seven segment display pin COM1; Resistor, 220 Ω pin 1
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.