Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
17. Morse Code
Morse code is like a secret language using dots (.) and dashes (-) invented by Samuel Morse in the 1840s. It was created to send messages across long distances using telegraphs. Each letter of the alphabet and number is represented by a unique combination of these signals. For example, the most famous Morse code message is “SOS” (··· ––– ···), which is an international signal for help. Morse code used to be essential for communication before the invention of phones and the internet, and it was especially popular among ship and airplane operators. Today, it’s fun to learn as a way to send secret messages to your friends!
In this lesson, you will learn:
Understand the workings of an active buzzer.
Learn to code the SOS signal in Morse code, enabling you to send messages using Morse code with a buzzer.
Morse Code Magic!
Imagine inventing a way to send secret messages using just dots and dashes! That’s what Samuel Morse did back in 1836 with Morse code. Initially a painter, Morse got inspired on a boat trip and later, with his buddy Alfred Vail, created the telegraph to send messages across wires.
Morse code uses dots (short signals) and dashes (long signals) to represent letters and numbers. The first Morse code message? “What hath God wrought”—sent in 1844 from Washington D.C. to Baltimore, kicking off the telegraph era.
Today, Morse code isn’t used as much, but it’s still cool for things like aviation and by amateur radio fans. Now, let’s explore how Morse code works with Arduino and a buzzer and have some fun with this piece of communication history!
Building the Circuit
Components Needed
1 * Arduino Uno R3 |
1 * Active Buzzer |
1 * Breadboard |
Jumper Wires |
|
|
|
|
1 * USB Cable |
|||
|
Building Step-by-Step
Locate an active buzzer which typically has a white sticker on the front and a sealed black back.
Buzzers, as electronic sound devices, have a rich history that traces back to the 19th century. The precursor to modern buzzers is rooted in 1831, when Michael Faraday discovered electromagnetic induction, forming the foundational principle behind the operation of electromagnetic buzzers. Following Faraday’s groundbreaking discovery, many scientists and inventors explored how to apply electromagnetic theories to practical devices. Today, Buzzers can be categorized as active and passive ones:
Active Buzzer
Sealed at the back, active buzzers contain an internal oscillator that sounds when powered, typically producing a single-tone beep.
Passive Buzzer
Open at the back, passive buzzers require an external frequency signal from a microcontroller to generate sound, allowing for a range of tones.
Active buzzer is also polar device. The front side has a “+” sign indicating its positive terminal (anode), which is also the longer pin. Now insert the buzzer into the breadboard with the anode in hole 15F and the cathode in hole 18F.
Connect the cathode to the GND pin on the Arduino Uno R3.
If you insert the anode of the buzzer into the 5V pin of the Arduino Uno R3, you will hear the active buzzer emit sound directly. Of course, you can also use this method to verify if the buzzer you have is correct. A passive buzzer will not produce sound when directly connected to a power source.
Now, remove the wire inserted into the 5V pin and insert it into pin 9 of the Arduino Uno R3, so that the buzzer can be controlled with code.
Code Creation
Open the Arduino IDE and start a new project by selecting “New Sketch” from the “File” menu.
Save your sketch as
Lesson17_Morse_CodeusingCtrl + Sor by clicking “Save”.First, create a constant called
buzzerPinand set it equal to pin 9.
const int buzzerPin = 9; // Assigns the pin 9 to the constant for the buzzer
void setup() {
// put your setup code here, to run once:
}
Initialize the pin: In the
void setup()function, set the buzzer pin to output mode.
const int buzzerPin = 9; // Assigns the pin 9 to the constant for the buzzer
void setup() {
// put your setup code here, to run once:
pinMode(buzzerPin, OUTPUT); // Set pin 9 as output
}
Making an active buzzer sound an alert is as simple as lighting an LED; you just need to use
digitalWrite()to set pin 9 high or low anddelay()to control the timing.
const int buzzerPin = 9; // Assigns the pin 9 to the constant for the buzzer
void setup() {
// put your setup code here, to run once:
pinMode(buzzerPin, OUTPUT); // Set pin 9 as output
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(buzzerPin, HIGH); // Turn buzzer ON
delay(250); // Beep duration: 250 milliseconds
digitalWrite(buzzerPin, LOW); // Turn buzzer OFF
delay(250); // Interval between signals: 250 milliseconds
}
You can upload your code to the Arduino Uno R3, and then you will hear the “beep beep” sound.
To make the buzzer emit Morse code, you need to create two functions after
void loop(), for emitting dots (short signals) and dashes (long signals).
Note
In Morse code, there are traditional timing rules for dots (short signals), dashes (long signals), and the intervals between signals to ensure the message is accurately received and understood. Here are some basic rules:
Length of a dot: the basic time unit.
Length of a dash: equals three dots.
Interval between dots: the length of one dot.
Interval within a character (between dots and dashes of a letter or number): the length of one dot.
Interval between characters (e.g., between two letters): three dots.
Interval between words (e.g., between two words): seven dots.
Therefore, we set the length of a dot to 250ms, a dash to 750ms, and the interval between elements to 250ms.
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(buzzerPin, HIGH); // Turn buzzer ON
delay(250); // Beep duration: 250 milliseconds
digitalWrite(buzzerPin, LOW); // Turn buzzer OFF
delay(250); // Interval between signals: 250 milliseconds
}
void dot() {
digitalWrite(buzzerPin, HIGH);
delay(250); // Short duration for a dot
digitalWrite(buzzerPin, LOW);
delay(250); // Interval between signals
}
void dash() {
digitalWrite(buzzerPin, HIGH);
delay(750); // Longer duration for a dash
digitalWrite(buzzerPin, LOW);
delay(250); // Interval between signals
}
Now, you can transmit Morse code. For example, to send “SOS” (… — …), the Morse code for ‘S’ consists of three dots, and ‘O’ is three dashes, so you simply call the dot and dash functions three times respectively.
void loop() {
dot();
dot();
dot(); // S: ...
dash();
dash();
dash(); // O: ---
dot();
dot();
dot(); // S: ...
delay(750); // Repeat after a period
}
Here is your complete code. You can now click “Upload” to upload the code to the Arduino Uno R3, after which you will hear the Morse code for “SOS” (… — …).
const int buzzerPin = 9; // Assigns the pin 9 to the constant for the buzzer
void setup() {
// put your setup code here, to run once:
pinMode(buzzerPin, OUTPUT); // Set pin 9 as output
}
void loop() {
dot();
dot();
dot(); // S: ...
dash();
dash();
dash(); // O: ---
dot();
dot();
dot(); // S: ...
delay(750); // Repeat after a period
}
void dot() {
digitalWrite(buzzerPin, HIGH);
delay(250); // Short duration for a dot
digitalWrite(buzzerPin, LOW);
delay(250); // Interval between signals
}
void dash() {
digitalWrite(buzzerPin, HIGH);
delay(750); // Longer duration for a dash
digitalWrite(buzzerPin, LOW);
delay(250); // Interval between signals
}
Finally, remember to save your code and tidy up your workspace.
Summary
In this lesson, you’ve explored the basics of Morse code, a unique form of communication developed in the 1840s by Samuel Morse. You learned how to use an active buzzer to send the Morse code for SOS, a universally recognized distress signal. This lesson not only taught you how to set up and code an active buzzer but also gave you a glimpse into the historical significance of Morse code in telecommunications. With these skills, you can now send secret Morse code messages to friends or further explore its applications in modern devices.
In this lesson, we only used the Morse codes for the letters “S” and “O.” Here is the chart of the Morse code 26 letters and 10 numerals.
Letter |
Code |
Letter |
Code |
Letter |
Code |
Letter |
Code |
|---|---|---|---|---|---|---|---|
A |
.- |
B |
-… |
C |
-.-. |
D |
-.. |
E |
. |
F |
..-. |
G |
--. |
H |
.… |
I |
.. |
J |
.--- |
K |
-.- |
L |
.-.. |
M |
-- |
N |
-. |
O |
--- |
P |
.--. |
Q |
--.- |
R |
.-. |
S |
... |
T |
- |
U |
..- |
V |
...- |
W |
.-- |
X |
-..- |
Y |
-.-- |
Z |
--.. |
1 |
.---- |
2 |
..--- |
3 |
...-- |
4 |
.…- |
5 |
.…. |
6 |
-…. |
7 |
--… |
8 |
---.. |
9 |
----. |
Question
Using the Morse code table provided, write a code to send the message “Hello”.




