Morse Code Transmitter Using Arduino

Morse Code Transmitter Using Arduino

The objective of this project is to create a Morse Code Transmitter using Arduino. The project will allow the user to communicate by transmitting Morse code through an LED using a button press. The code will blink an LED in short (dot) and long (dash) intervals, covering the use of digitalWrite() for output and delay() for timing control, along with if-else control structures.

Fundamental Programming Concepts

  • Digital Output (digitalWrite()): Controls the LED to turn it on or off, simulating Morse code.
  • Timing Functions (delay()): Controls the timing between the dots and dashes in Morse code.
  • Control Structures (if-else): Used to determine the duration of the LED blink based on the button press.
  • Variables: Used to store the state of the button and LED.

Requirement Components

To complete this Morse code Arduino project, you will need:

  • Arduino Uno Board
  • LED
  • Push Button
  • 220Ω Resistor (for the LED)
  • 10kΩ Resistor (for pull-down)
  • Breadboard
  • Jumper Wires
  • USB Cable (for connecting Arduino to your computer)

Circuit Diagram

Insert your Arduino Morse Code Transmitter circuit diagram here to visualize the setup.

Circuit Connection

Component Arduino Pin
Button (One Pin) Pin 2
Button (Other Pin) GND
10kΩ Resistor Between Pin 2 and GND
LED (Anode) Pin 9
LED (Cathode) GND
220Ω Resistor Between LED anode and Pin 9

How to Connect the Circuit

  1. Connect one leg of the push button to Pin 2 on the Arduino, and the other leg to GND.
  2. Place a 10kΩ pull-down resistor between Pin 2 and GND to ensure proper button behavior when not pressed.
  3. Connect the LED’s anode (longer leg) to Pin 9 and the cathode (shorter leg) to GND using a 220Ω resistor.
  4. Double-check all connections to ensure they are secure.

Explanation of Circuit

  • The push button is connected as an input to send Morse code signals. When pressed, the Arduino reads the input using digitalRead().
  • The LED is connected as an output and blinks in short or long bursts (dots and dashes) based on the duration of the button press, controlled by digitalWrite() and delay().

Programming Section for Morse Code Transmitter using Arduino

Arduino Syntax

Topic Name Syntax Explanation
digitalRead() digitalRead(pin) Reads the state of the button (HIGH or LOW).
digitalWrite() digitalWrite(pin, value) Sets a digital pin to HIGH or LOW to control the LED.
Timing (delay) delay(ms) Pauses the program for the specified number of milliseconds.
if-else Condition if (condition) { } else { } Executes code based on whether the button is pressed or not.

Arduino Code:

Here is the Arduino code to create a Morse Code Transmitter using an LED:

// Define pin numbers
const int buttonPin = 2;   // Pin connected to the button
const int ledPin = 9;      // Pin connected to the LED
// Variables to store button and LED states
int buttonState = 0;       // Variable for reading the button state
int lastButtonState = 0;   // Variable to store the previous button state
long pressTime = 0;        // Variable to track how long the button is pressed
void setup() {
  pinMode(buttonPin, INPUT);  // Set button pin as input
  pinMode(ledPin, OUTPUT);    // Set LED pin as output
}
void loop() {
  // Read the current state of the button
  buttonState = digitalRead(buttonPin);
  // Check if the button is pressed
  if (buttonState == HIGH && lastButtonState == LOW) {
    pressTime = millis();  // Record the time when the button is pressed
  }
  // Check if the button is released
  if (buttonState == LOW && lastButtonState == HIGH) {
    long pressDuration = millis() - pressTime;  // Calculate the press duration
    // Transmit Morse code based on press duration
    if (pressDuration < 500) {
      // Dot (short press)
      digitalWrite(ledPin, HIGH);  // Turn on LED (dot)
      delay(200);                  // LED on for short duration
      digitalWrite(ledPin, LOW);   // Turn off LED
      delay(200);                  // Pause between signals
    } else {
      // Dash (long press)
      digitalWrite(ledPin, HIGH);  // Turn on LED (dash)
      delay(600);                  // LED on for longer duration
      digitalWrite(ledPin, LOW);   // Turn off LED
      delay(200);                  // Pause between signals
    }
  }
 // Save the current button state as the last state for the next loop
  lastButtonState = buttonState;
}

Steps to Upload Code:

  1. Connect your Arduino to your computer using a USB cable.
  2. Open the Arduino IDE and select the correct Board and Port.
  3. Copy and paste the provided code into a new sketch.
  4. Click the Upload button to upload the code to your Arduino.
  5. Press the button and observe the Morse code transmitted through the LED.

Check Output

Once the code is uploaded, press the button:

  • Short press: The LED blinks briefly (dot).
  • Long press: The LED stays on longer (dash).

The LED will blink according to how long you hold the button, simulating Morse code.

Troubleshooting Tips

  • LED not blinking? Double-check the polarity of the LED (anode to Pin 9, cathode to GND) and ensure the connections are correct.
  • Button presses not registering? Verify that the pull-down resistor is properly connected between Pin 2 and GND to prevent floating values.
  • No LED response? Ensure the correct pins are defined in the code, and make sure the button is properly connected.

Further Exploration

  • Add More Morse Code Characters: Expand the project by implementing more Morse code characters using the button press duration.
  • Add Sound: Connect a buzzer to generate sound along with the LED blinks, providing an auditory Morse code signal.
  • Control Multiple LEDs: Use multiple LEDs to visually display Morse code signals across different outputs.

Note

This project introduces key concepts such as digital input and output, timing functions (delay), and control structures (if-else) in Arduino programming. These concepts are essential for creating interactive projects like Morse code transmitters and other communication devices.

FAQ

Q1: What does digitalWrite() do in Arduino?
The digitalWrite() function sends a HIGH or LOW signal to a digital pin, controlling whether the LED is on or off.

Q2: How does the button control the Morse code?
The button sends signals to the Arduino. Based on how long the button is pressed, the Arduino determines whether to transmit a dot (short press) or a dash (long press) by controlling the LED.

Q3: Why do we use the delay() function?
The delay() function is used to control how long the LED stays on or off, creating the short and long blinks needed for Morse code.

Q4: Can I add sound to this project?
Yes, you can add a buzzer or speaker to generate sound for the Morse code signals in addition to the LED blinks.

Q5: Can I control multiple LEDs with Morse code?
Yes, you can modify the code to control multiple LEDs or add additional output options to display the Morse code signals.