Morse code on an active buzzer (pin 9), on an Arduino Uno
An active buzzer on pin 9 beeps SOS in Morse code, three short, three long and three short, with a dot and a dash written as functions. The whole build, an Arduino Uno and 1 more part, 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 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $30. On Mokxi they're free.
What you'll learn
- Active buzzers versus passive buzzers
- Timing patterns with
delay - Small helper functions to avoid repeating code
- Morse code timing rules
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 3
How it works
An active buzzer has its own tone generator inside. Pin 9 HIGH makes it sound, and LOW makes it stop. So the sketch only needs digitalWrite and timing.
Morse code is all about lengths. A dot is one UNIT (150 ms), a dash is three, and a short gap follows each beep. Longer pauses separate letters.
Each pass of loop prints "... --- ..." in the monitor and beeps S, O, S: three dots, three dashes, three dots. Then it waits before it repeats.
Parts list
3 parts, plus the jumper wires. They retail for about $30; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × BuzzerIn Starter Kit, Rocket Flight Computer Kit.
Kits that can build it
These 9 starter kits have every part in the circuit. Each kit's page lists what else it can build.
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 |
|---|---|
| Buzzer pin 2 | Arduino Uno R3 pin GND |
| Buzzer pin 1 | Arduino Uno R3 pin 9 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Morse code: an active buzzer on pin 9 beeps SOS.
//
// pin 9 -> the buzzer's + leg; the other leg -> GND
//
// An active buzzer has its own oscillator inside: give it 5 V and it sounds,
// take the 5 V away and it stops. So digitalWrite is all it takes.
//
// In Morse code a dash is three dots long, the gap inside a letter is one
// dot, and the gap between letters is three. dot() and dash() keep the
// timing in one place; change UNIT to send faster or slower.
const int BUZZER = 9;
const int UNIT = 150; // the length of a dot, in milliseconds
void beep(int units) {
digitalWrite(BUZZER, HIGH);
delay(units * UNIT);
digitalWrite(BUZZER, LOW);
delay(UNIT);
}
void dot() { beep(1); }
void dash() { beep(3); }
void setup() {
pinMode(BUZZER, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: SOS in Morse code on the buzzer on pin 9");
}
void loop() {
Serial.println("... --- ...");
dot(); dot(); dot();
delay(2 * UNIT);
dash(); dash(); dash();
delay(2 * UNIT);
dot(); dot(); dot();
delay(6 * UNIT);
}
Code walkthrough
The key functions of the sketch, and what each one does.
beep sketch.ino
Turns the buzzer on, waits for the given number of units, turns it off, and waits one more unit. That last wait is the gap inside a letter.
void beep(int units) {
digitalWrite(BUZZER, HIGH);
delay(units * UNIT);
digitalWrite(BUZZER, LOW);
delay(UNIT);
}dot sketch.ino
A one unit beep. It calls beep(1).
void dot() { beep(1); }dash sketch.ino
A three unit beep. It calls beep(3). Keeping both lengths in one place means a change to UNIT speeds up or slows down all the code.
void dash() { beep(3); }loop sketch.ino
Plays three dots, a pause, three dashes, a pause, three dots, then a longer rest before it starts again.
void loop() {
Serial.println("... --- ...");
dot(); dot(); dot();
delay(2 * UNIT);
dash(); dash(); dash();
delay(2 * UNIT);
dot(); dot(); dot();
delay(6 * UNIT);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The buzzer stays silent This project
Check which leg is positive. The buzzer's pin 1 goes to Arduino pin 9 and its pin 2 goes to ground. - The letters run together This project
Keep the pauses inlooplonger than the gap inbeep. Otherwise you cannot tell where one letter ends. - The pattern is too fast to follow This project
RaiseUNIT. Every duration scales with it. - The buzzer makes a constant tone, or you cannot change its pitch. Buzzer
An active buzzer has its own oscillator, so it only beeps on and off. A passive buzzer needs tone() to set pitch. Check which type you have. - The buzzer is silent or very quiet. Buzzer
Check polarity: the plus mark goes to the pin or supply. For loud buzzers, drive them through a transistor instead of straight from a pin. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.