Arduino Project
Morse Code
Course Introduction
In this lesson, you’ll use a button, an LED, and a buzzer with the Arduino R4 UNO to create a simple Morse code trainer.
By pressing the button for short or long durations, you can input dots and dashes. The LED and buzzer provide immediate feedback, and the serial monitor displays whether you sent a dot (·) or a dash (—). This project is great for learning Morse code basics interactively.
Note
If this is your first time working with an Arduino project, we recommend downloading and reviewing the basic materials first.
1.1 Install Arduino IDE(Important)
1.2 Introduction of Arduino IDE
Wiring

Common Connections:
LED
Connect the LED to a 1kΩ resistor, then to anode to 3 on the Arduino, and the cathode to the negative power bus on the breadboard.
Passive Buzzer
+: Connect to 2 on the Arduino.
-: Connect to breadboard’s negative power bus.
Button
Connect to the breadboard’s negative power bus, and the other end to 4 on the Arduino board.
Writing the Code
Note
You can copy this code into the Arduino IDE.
Don’t forget to select the board(Arduino UNO R4 Minima) and the correct port before clicking the Upload button.
// Pin Definitions
const int BUTTON_PIN = 4; // Push button (connect to GND)
const int BUZZER_PIN = 2; // Active buzzer (+)
const int LED_PIN = 3; // LED anode (with 220Ω resistor)
// Timing Constants (milliseconds)
const int DOT_DURATION = 200; // Standard dot duration
const int DASH_DURATION = 600; // Standard dash duration
const int DEBOUNCE_DELAY = 50; // Button debounce time
// Global Variables
bool lastButtonState = HIGH; // Track previous button state
unsigned long pressStartTime = 0; // Timing variable
void setup() {
// Initialize pins
pinMode(BUTTON_PIN, INPUT_PULLUP); // Internal pull-up
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
// Ensure outputs start OFF
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
// Start serial communication
Serial.begin(9600);
Serial.println("Morse Code Trainer Ready");
Serial.println("Short press: Dot(·) Long press: Dash(—)");
}
void loop() {
int currentButtonState = digitalRead(BUTTON_PIN);
// Handle button press (active LOW)
if (currentButtonState == LOW && lastButtonState == HIGH) {
delay(DEBOUNCE_DELAY); // Debounce
if (digitalRead(BUTTON_PIN) == LOW) { // Confirmed press
pressStartTime = millis(); // Record start time
activateFeedback(); // Turn on LED and buzzer
Serial.println("Button PRESSED");
}
}
// Handle button release
if (currentButtonState == HIGH && lastButtonState == LOW) {
delay(DEBOUNCE_DELAY);
if (digitalRead(BUTTON_PIN) == HIGH) { // Confirmed release
deactivateFeedback(); // Turn off immediately
handleMorseSymbol(); // Process the symbol
}
}
lastButtonState = currentButtonState; // Update state
}
// Turn on both visual and audio feedback
void activateFeedback() {
digitalWrite(LED_PIN, HIGH);
digitalWrite(BUZZER_PIN, HIGH);
}
// Turn off both feedback channels
void deactivateFeedback() {
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
}
// Determine if it's a dot or dash
void handleMorseSymbol() {
unsigned long pressDuration = millis() - pressStartTime;
if (pressDuration < DASH_DURATION) {
Serial.println("Sent: DOT(·)");
// Ensure minimum dot duration
if (pressDuration < DOT_DURATION) {
delay(DOT_DURATION - pressDuration);
}
} else {
Serial.println("Sent: DASH(—)");
// Ensure minimum dash duration
if (pressDuration < DASH_DURATION) {
delay(DASH_DURATION - pressDuration);
}
}
}
