Morse sender on an ESP32-C3
An ESP32-C3 sends a word in Morse on a lamp and a piezo at once, and a button picks the next message. The whole build, an ESP32-C3 and 4 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 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $13.65. On Mokxi they're free.
What you'll learn
- How Morse code timing is built from one unit
- Looking up a code from an array by letter
- Driving a lamp and a tone together
- Reading a button during a delay
- Changing speed with a single constant
- Difficulty: Beginner
- Time: About 15 minutes
- Board: ESP32-C3-DevKitM-1
- Parts: 6
How it works
The ESP32-C3 sends a message in Morse code. A yellow LED on pin 8 and a piezo on pin 5 turn on together for each dot or dash. A dot lasts one unit of 120 ms, and a dash lasts three. Gaps follow the standard Morse spacing.
It sends SOS, MOKXI or HELLO WORLD, then waits 1.5 seconds and sends again. Press the button on pin 9 during the wait to move to the next message. The serial monitor shows each letter and its code.
Parts list
6 parts, plus the jumper wires. They retail for about $13.65; on Mokxi they're free.
- 1 × ESP32-C3-DevKitM-1In Handheld Console Kit, Desk Buddy Kit.
- 1 × Full-size breadboard
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, yellowIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × Piezo speakerIn Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
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 pin | Connects to |
|---|---|
| LED, yellow pin C | ESP32-C3-DevKitM-1 pin GND |
| Piezo speaker pin 2 | ESP32-C3-DevKitM-1 pin GND |
| Pushbutton pin 2a | ESP32-C3-DevKitM-1 pin GND |
| Pushbutton pin 2b | ESP32-C3-DevKitM-1 pin GND |
| Piezo speaker pin 1 | ESP32-C3-DevKitM-1 pin 5 |
| Resistor, 220 Ω pin 1 | ESP32-C3-DevKitM-1 pin 8 |
| Pushbutton pin 1a | ESP32-C3-DevKitM-1 pin 9 |
| Pushbutton pin 1b | ESP32-C3-DevKitM-1 pin 9 |
| LED, yellow pin A | Resistor, 220 Ω pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Morse: a word sent as light and sound, one element at a time.
//
// GPIO 8 the lamp, through 220 ohm to GND
// GPIO 5 a passive piezo straight to GND
// GPIO 9 a button to GND, internal pull-up: send the next message
//
// Morse is built out of one unit of time. A dot is one unit, a dash is three,
// the gap inside a letter is one, between letters three, between words seven.
// Everything below is that table and nothing else, so changing UNIT_MS changes
// the speed of the whole thing and every proportion stays right.
//
// Twenty words a minute is the usual yardstick, and the word it is measured on
// is PARIS, which is fifty units long: 60 000 / (20 x 50) is 60 ms a unit. The
// sketch starts slower than that, at 120 ms, because it is meant to be read
// off the lamp by eye.
//
// The table is one string a letter, dots and dashes, indexed by the letter
// itself. Anything not in it is skipped, so punctuation in a message costs
// nothing but is not sent either.
#include "mokxi_tone.h"
const uint8_t LAMP = 8;
const uint8_t PIEZO = 5;
const uint8_t BUTTON = 9;
const uint16_t UNIT_MS = 120;
const uint16_t PITCH_HZ = 700;
// A to Z, then 0 to 9.
const char *const MORSE[] = {
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----",
".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.",
};
const char *const MESSAGES[] = {"SOS", "MOKXI", "HELLO WORLD"};
const int MESSAGE_COUNT = 3;
int which = 0;
static const char *codeFor(char c) {
if (c >= 'a' && c <= 'z') {
c = (char)(c - 'a' + 'A');
}
if (c >= 'A' && c <= 'Z') {
return MORSE[c - 'A'];
}
if (c >= '0' && c <= '9') {
return MORSE[26 + (c - '0')];
}
return 0;
}
// One element: the lamp and the note together, for one unit or three.
static void element(bool dash) {
uint16_t ms = (uint16_t)(dash ? UNIT_MS * 3u : UNIT_MS);
digitalWrite(LAMP, HIGH);
mokxiTone(PIEZO, PITCH_HZ, ms);
digitalWrite(LAMP, LOW);
delay(UNIT_MS); // the gap inside a letter
}
static void sendLetter(const char *code) {
for (const char *p = code; *p; p++) {
element(*p == '-');
}
delay(UNIT_MS * 2u); // one unit is already spent, three between letters
}
static void send(const char *text) {
Serial.print("sending ");
Serial.println(text);
for (const char *p = text; *p; p++) {
if (*p == ' ') {
delay(UNIT_MS * 4u); // three are already spent, seven between words
continue;
}
const char *code = codeFor(*p);
if (!code) {
continue;
}
Serial.print(" ");
Serial.print(*p);
Serial.print(" ");
Serial.println(code);
sendLetter(code);
}
}
void setup() {
pinMode(LAMP, OUTPUT);
pinMode(PIEZO, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP);
Serial.begin(115200);
Serial.println("Mokxi ESP32-C3: Morse sender");
Serial.println("press the button to move to the next message");
}
void loop() {
send(MESSAGES[which]);
// a long gap, and the button read through it
unsigned long start = millis();
while (millis() - start < 1500UL) {
if (digitalRead(BUTTON) == LOW) {
which = (which + 1) % MESSAGE_COUNT;
Serial.println("next message");
while (digitalRead(BUTTON) == LOW) {
delay(10);
}
break;
}
delay(10);
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
codeFor sketch.ino
Turns a character into its Morse string. It makes lowercase letters uppercase, then looks letters up in MORSE by offset from A and digits by offset from 0. Anything else returns nothing and is skipped.
static const char *codeFor(char c) {
if (c >= 'a' && c <= 'z') {
c = (char)(c - 'a' + 'A');
}
if (c >= 'A' && c <= 'Z') {
return MORSE[c - 'A'];
}
if (c >= '0' && c <= '9') {
return MORSE[26 + (c - '0')];
}
return 0;
}element sketch.ino
Sends one dot or dash. It turns the lamp on, plays the tone for one or three units, turns the lamp off, then waits one unit as the gap inside a letter.
// One element: the lamp and the note together, for one unit or three.
static void element(bool dash) {
uint16_t ms = (uint16_t)(dash ? UNIT_MS * 3u : UNIT_MS);
digitalWrite(LAMP, HIGH);
mokxiTone(PIEZO, PITCH_HZ, ms);
digitalWrite(LAMP, LOW);
delay(UNIT_MS); // the gap inside a letter
}send sketch.ino
Walks through a message. Spaces add a 4 unit pause, which makes seven between words once the letter gap is counted. Other characters are looked up and sent letter by letter.
static void send(const char *text) {
Serial.print("sending ");
Serial.println(text);
for (const char *p = text; *p; p++) {
if (*p == ' ') {
delay(UNIT_MS * 4u); // three are already spent, seven between words
continue;
}
const char *code = codeFor(*p);
if (!code) {
continue;
}
Serial.print(" ");
Serial.print(*p);
Serial.print(" ");
Serial.println(code);
sendLetter(code);
}
}loop sketch.ino
Sends the current message, then watches the button for 1.5 seconds. A press moves to the next message.
void loop() {
send(MESSAGES[which]);
// a long gap, and the button read through it
unsigned long start = millis();
while (millis() - start < 1500UL) {
if (digitalRead(BUTTON) == LOW) {
which = (which + 1) % MESSAGE_COUNT;
Serial.println("next message");
while (digitalRead(BUTTON) == LOW) {
delay(10);
}
break;
}
delay(10);
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The message plays too fast to read. This project
RaiseUNIT_MS. All timings scale with it. - The button does nothing. This project
Press it during the pause after a message, not while it is sending. - The LED stays off. This project
The LED's anode goes through the 220 ohm resistor to pin 8, and the cathode 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. - A passive piezo stays silent when you just set the pin HIGH. Piezo speaker
A passive piezo needs a changing signal. Use tone(pin, frequency) or a PWM wave. Around 2 to 4 kHz is loudest. A buzzer with a built in oscillator makes sound from plain DC instead.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.