Convert Text into Morse Code with beeps & back into Text with Arduino

Convert Text into Morse Code with beeps & back into Text with Arduino

This entry is part 4 of 4 in the series Advanced Arduino Projects

Morse code is a method of communication that uses short and long signals, represented by dots and dashes, to represent letters, numbers, and punctuation.
It was invented in the 1830s and 1840s by Samuel Morse to be used with the telegraph, an early electrical communication system.

Let us understand details of Morse code:

Signals: Morse code uses two basic signal lengths: dots (short) and dashes (long).

Encoding: These dots and dashes are combined in specific sequences to represent letters, numbers, and punctuation.

Transmission: Morse code can be transmitted in a variety of ways, including electrical pulses, sound beeps, or light flashes. While less common today, Morse code was once a vital tool for long-distance communication. It played a significant role in the early days of the telegraph and even had applications in maritime communication and early radio.

Morse Code Reference Chart

Morse Code Reference Chart

How to construct the project?

For that you need following material –

  1. Arduino UNO/ Arduino Nano
  2. Data cable
  3. Buzzer
  4. Jumper Wires

Make sure that Arduino IDE Software is installed on your PC. If not you can download it and install.

Now let us see the code and the connection details to construct the circuit.

The Code

/*
 * Written by: Prof. Yash Vidyasagar
 * for Vidyasagar Academy's Robotics Course students
 * Lab tested code. No errors...!!!
 */
const int buzzerPin = 11; // Pin connected to the buzzer
const int dotDuration = 100; // Duration of a dot in milliseconds
const int dashDuration = 300; // Duration of a dash in milliseconds
const int letterSpace = 300; // Space between letters
const int wordSpace = 700; // Space between words

// Morse code representation
String morseCode[36] = {
  ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---",
  "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
  "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---",
  "...--", "....-", ".....", "-....", "--...", "---..", "----."
};

// Function to convert a character to Morse code
String charToMorse(char c) {
  if (c >= 'A' && c <= 'Z') {
    return morseCode[c - 'A'];
  } else if (c >= 'a' && c <= 'z') {
    return morseCode[c - 'a'];
  } else if (c >= '0' && c <= '9') {
    return morseCode[c - '0' + 26];
  }
  return "";
}

// Function to beep the Morse code
void beepMorse(String morse) {
  for (int i = 0; i < morse.length(); i++) {
    if (morse[i] == '.') {
      digitalWrite(buzzerPin, HIGH);
      delay(dotDuration);
      digitalWrite(buzzerPin, LOW);
    } else if (morse[i] == '-') {
      digitalWrite(buzzerPin, HIGH);
      delay(dashDuration);
      digitalWrite(buzzerPin, LOW);
    }
    delay(dotDuration); // Space between dots and dashes
  }
}

void setup() {
  Serial.begin(9600);
  pinMode(buzzerPin, OUTPUT);
  Serial.println("Enter a string to convert to Morse code:");
}

void loop() {
  if (Serial.available() > 0) {
    String input = Serial.readStringUntil('\n');
    Serial.println("Input: " + input);
    Serial.print("Morse Code: ");
    for (int i = 0; i < input.length(); i++) {
      char c = input[i];
      if (c == ' ') {
        delay(wordSpace);
        Serial.print(" | ");
      } else {
        String morse = charToMorse(c);
        Serial.print(morse + " ");
        beepMorse(morse);
        delay(letterSpace);
      }
    }
    Serial.println();
  }
}

Connection Details

  1. Actually the connections of this project are very simple.
  2. Just take a breadboard and fix buzzer on it.
  3. Take Arduino Uno / Arduino Nano and connect its pin-11 to +ve terminal of the buzzer.
  4. Connect -ve terminal of the buzzer to ground (GND) of Arduino.

Procedure

  1. Connect Arduino to your PC through data cable and upload the above given code.
  2. Then open serial monitor and type a word “Hi” in it.
  3. You will hear six beeps from the buzzer and after that you will see the Morse Code of the word “Hi”.
  4. The Morse Code of the word “Hi” is 4 dots then a space and then 2 dots, like this: . . . . . .
  5. In this way, you can convert text like a long sentence into corresponding Morse Code with beeps and after that you get the corresponding Morse Code value of the text, that you had entered.

Hope you liked the project. Please let us know your feedback below in comments area. You can comment below to get the guidance about converting Morse Code into corresponding text.

Share on your network!
Prof. Dattaraj Vidyasagar
Prof. Dattaraj Vidyasagar

dfsdfsdf

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x