Contents

How to Make a Morse Code Translator with Arduino

Overview

Communicating in a coded way, besides being so fascinating, has many applications in various fields. One of the most common methods of code communicating is Morse code. In this tutorial, you’ll learn how to make an interpreter to send and receive Morse code with Arduino.

What You Will Learn

What Is Morse Code?

In the past times when communications were not as simple as today, one of the most common ways of communicating was Samuel Morse method called “Morse code“. In this method each letter or number represents by using short (dot) and long (dash) elements. Morse code, like any other language, has its own alphabet and is currently available in both American and International types, and the most commonly used one is international type.

Morse code can be transmitted in different ways: originally as electrical pulses along a telegraph wire, but also as an audio tone, a radio signal, light, body language, frequency and more. Imagine the dot as a unit of time, then the dash is three units of time, the distance between the parts of a letter is a unit of time, the distance between two consecutive letters is three units of time, and the distance between the words is seven units of time.

For instance the word SOS, the worldwide standard for requesting help is … — … in Morse code.

Try to write your name in Morse code for practice.

Electropeak = . .-.. . -.-. – .-. — .–. . .- -.-

Is Morse Code Still Practical?

Although Morse code is not used as much as the past anymore, it still has its own applications. Morse code is still popular among enthusiasts in the field of amateur radios. Morse code is also used in aeronautical navigation systems. Many ships use Morse code to send light for communication or help. Also, those who can not talk for any reason can also use Morse code to express their meaning. And besides all, learning and using Morse code to communicate can be fun and entertaining.

Required Materials

Hardware Components

Arduino UNO R3 × 1
Active Buzzer × 1
LED × 1
Jumper Wire × 1

Software Apps

Arduino IDE

Make a Morse Code Encoder w/ Arduino

It may be a little difficult to remember Morse code and convert texts to this code, so let’s make a translator to convert texts to Morse code! Here we used Arduino UNO to translate a text to Morse code. Upload this code on your Arduino board and open your serial monitor window. Type your desired word or text and receive it in Morse code, then you can send it as light and sound.

Circuit

Code

/*

Morse code - Transmitter 

modified on 14 Apr 2019
by Saeed Hosseini @ Electropeak
Home
*/ const int led = 13; const int buz = 8; String code = ""; int len = 0; char ch; char new_char; int unit_delay = 250; void dot() { Serial.print("."); digitalWrite(led, HIGH); digitalWrite(buz, HIGH); delay(unit_delay); digitalWrite(led, LOW); digitalWrite(buz, LOW); delay(unit_delay); } void dash() { Serial.print("-"); digitalWrite(led, HIGH); digitalWrite(buz, HIGH); delay(unit_delay * 3); digitalWrite(led, LOW); digitalWrite(buz, LOW); delay(unit_delay); } void A() { dot(); delay(unit_delay); dash(); delay(unit_delay); } void B() { dash(); delay(unit_delay); dot(); delay(unit_delay); dot(); delay(unit_delay); dot(); delay(unit_delay); } void C() { dash(); delay(unit_delay); dot(); delay(unit_delay); dash(); delay(unit_delay); dot(); delay(unit_delay); } void D() { dash(); delay(unit_delay); dot(); delay(unit_delay); dot(); delay(unit_delay); } void E() { dot(); delay(unit_delay); } void f() { dot(); delay(unit_delay); dot(); delay(unit_delay); dash(); delay(unit_delay); dot(); delay(unit_delay); } void G() { dash(); delay(unit_delay); dash(); delay(unit_delay); dot(); delay(unit_delay); } void H() { dot(); delay(unit_delay); dot(); delay(unit_delay); dot(); delay(unit_delay); dot(); delay(unit_delay); } void I() { dot(); delay(unit_delay); dot(); delay(unit_delay); } void J() { dot(); delay(unit_delay); dash(); delay(unit_delay); dash(); delay(unit_delay); dash(); delay(unit_delay); } void K() { dash(); delay(unit_delay); dot(); delay(unit_delay); dash(); delay(unit_delay); } void L() { dot(); delay(unit_delay); dash(); delay(unit_delay); dot(); delay(unit_delay); dot(); delay(unit_delay); } void M() { dash(); delay(unit_delay); dash(); delay(unit_delay); } void N() { dash(); delay(unit_delay); dot(); delay(unit_delay); } void O() { dash(); delay(unit_delay); dash(); delay(unit_delay); dash(); delay(unit_delay); } void P() { dot(); delay(unit_delay); dash(); delay(unit_delay); dash(); delay(unit_delay); dot(); } void Q() { dash(); delay(unit_delay); dash(); delay(unit_delay); dot(); delay(unit_delay); dash(); delay(unit_delay); } void R() { dot(); delay(unit_delay); dash(); delay(unit_delay); dot(); delay(unit_delay); } void S() { dot(); delay(unit_delay); dot(); delay(unit_delay); dot(); delay(unit_delay); } void T() { dash(); delay(unit_delay); } void U() { dot(); delay(unit_delay); dot(); delay(unit_delay); dash(); delay(unit_delay); } void V() { dot(); delay(unit_delay); dot(); delay(unit_delay); dot(); delay(unit_delay); dash(); delay(unit_delay); } void W() { dot(); delay(unit_delay); dash(); delay(unit_delay); dash(); delay(unit_delay); } void X() { dash(); delay(unit_delay); dot(); delay(unit_delay); dot(); delay(unit_delay); dash(); delay(unit_delay); } void Y() { dash(); delay(unit_delay); dot(); delay(unit_delay); dash(); delay(unit_delay); dash(); delay(unit_delay); } void Z() { dash(); delay(unit_delay); dash(); delay(unit_delay); dot(); delay(unit_delay); dot(); delay(unit_delay); } void one() { dot(); delay(unit_delay); dash(); delay(unit_delay); dash(); delay(unit_delay); dash(); delay(unit_delay); dash(); delay(unit_delay); } void two() { dot(); delay(unit_delay); dot(); delay(unit_delay); dash(); delay(unit_delay); dash(); delay(unit_delay); dash(); delay(unit_delay); } void three() { dot(); delay(unit_delay); dot(); delay(unit_delay); dot(); delay(unit_delay); dash(); delay(unit_delay); dash(); delay(unit_delay); } void four() { dot(); delay(unit_delay); dot(); delay(unit_delay); dot(); delay(unit_delay); dot(); delay(unit_delay); dash(); delay(unit_delay); } void five() { dot(); delay(unit_delay); dot(); delay(unit_delay); dot(); delay(unit_delay); dot(); delay(unit_delay); dot(); delay(unit_delay); } void six() { dash(); delay(unit_delay); dot(); delay(unit_delay); dot(); delay(unit_delay); dot(); delay(unit_delay); dot(); delay(unit_delay); } void seven() { dash(); delay(unit_delay); dash(); delay(unit_delay); dot(); delay(unit_delay); dot(); delay(unit_delay); dot(); delay(unit_delay); } void eight() { dash(); delay(unit_delay); dash(); delay(unit_delay); dash(); delay(unit_delay); dot(); delay(unit_delay); dot(); delay(unit_delay); } void nine() { dash(); delay(unit_delay); dash(); delay(unit_delay); dash(); delay(unit_delay); dash(); delay(unit_delay); dot(); delay(unit_delay); } void zero() { dash(); delay(unit_delay); dash(); delay(unit_delay); dash(); delay(unit_delay); dash(); delay(unit_delay); dash(); delay(unit_delay); } void morse() { if (ch == 'A' || ch == 'a') { A(); Serial.print(" "); } else if (ch == 'B' || ch == 'b') { B(); Serial.print(" "); } else if (ch == 'C' || ch == 'c') { C(); Serial.print(" "); } else if (ch == 'D' || ch == 'd') { D(); Serial.print(" "); } else if (ch == 'E' || ch == 'e') { E(); Serial.print(" "); } else if (ch == 'f' || ch == 'f') { f(); Serial.print(" "); } else if (ch == 'G' || ch == 'g') { G(); Serial.print(" "); } else if (ch == 'H' || ch == 'h') { H(); Serial.print(" "); } else if (ch == 'I' || ch == 'i') { I(); Serial.print(" "); } else if (ch == 'J' || ch == 'j') { J(); Serial.print(" "); } else if (ch == 'K' || ch == 'k') { K(); Serial.print(" "); } else if (ch == 'L' || ch == 'l') { L(); Serial.print(" "); } else if (ch == 'M' || ch == 'm') { M(); Serial.print(" "); } else if (ch == 'N' || ch == 'n') { N(); Serial.print(" "); } else if (ch == 'O' || ch == 'o') { O(); Serial.print(" "); } else if (ch == 'P' || ch == 'p') { P(); Serial.print(" "); } else if (ch == 'Q' || ch == 'q') { Q(); Serial.print(" "); } else if (ch == 'R' || ch == 'r') { R(); Serial.print(" "); } else if (ch == 'S' || ch == 's') { S(); Serial.print(" "); } else if (ch == 'T' || ch == 't') { T(); Serial.print(" "); } else if (ch == 'U' || ch == 'u') { U(); Serial.print(" "); } else if (ch == 'V' || ch == 'v') { V(); Serial.print(" "); } else if (ch == 'W' || ch == 'w') { W(); Serial.print(" "); } else if (ch == 'X' || ch == 'x') { X(); Serial.print(" "); } else if (ch == 'Y' || ch == 'y') { Y(); Serial.print(" "); } else if (ch == 'Z' || ch == 'z') { Z(); Serial.print(" "); } else if (ch == '0') { zero(); Serial.print(" "); } else if (ch == '1') { one(); Serial.print(" "); } else if (ch == '2') { two(); Serial.print(" "); } else if (ch == '3') { three(); Serial.print(" "); } else if (ch == '4') { four(); Serial.print(" "); } else if (ch == '5') { five(); Serial.print(" "); } else if (ch == '6') { six(); Serial.print(" "); } else if (ch == '7') { seven(); Serial.print(" "); } else if (ch == '8') { eight(); Serial.print(" "); } else if (ch == '9') { nine(); Serial.print(" "); } else if(ch == ' ') { delay(unit_delay*7); Serial.print("/ "); } else Serial.println("Unknown symbol!"); } void String2Morse() { len = code.length(); for (int i = 0; i < len; i++) { ch = code.charAt(i); morse(); } } void setup() { Serial.begin(9600); pinMode(led, OUTPUT); pinMode(buz, OUTPUT); Serial.println("I am ready..."); } void loop() { while (Serial.available()) { code = Serial.readString(); Serial.print(code); Serial.print(" = "); String2Morse(); Serial.println(""); } delay(1000); }

Make a Morse Code Decoder with Arduino

In another case, you are a Morse Code Recipient and you must convert the received code to text. To simulate this, send the Morse code to Arduino using the key and see the result as text in the Serial Monitor.

Circuit

Code

/*

  Morse code - Receiver

  modified on 14 Apr 2019
  by Saeed Hosseini @ Electropeak
  
Home
*/ String code = ""; int len = 0; char ch; char new_char; const int but = 2; const int led = 13; unsigned long pres_len = 0, rel_time, pres_time = 0, old_time_len = 0, old_pres = 0, space = 0; int state = 0; int unit_delay = 250; int min_delay = 10; char MakeString() { if (pres_len < (unit_delay*3) && pres_len > 50) { return '.'; //if button press less than 0.6sec, it is a dot } else if (pres_len > (unit_delay*3)) { return '-'; //if button press more than 0.6sec, it is a dash } } void Morse_decod() { static String morse[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "!" }; int i = 0; while (morse[i] != "!") { if (morse[i] == code) { Serial.print(char('A' + i)); Serial.print(" "); break; } i++; } if (morse[i] == "!") { Serial.println(""); Serial.println("This code is not exist!"); } code = ""; } void setup() { Serial.begin(9600); pinMode(but, INPUT_PULLUP); pinMode(led, OUTPUT); } void loop() { label: while (digitalRead(but) == HIGH) {} old_pres = rel_time; pres_time = millis(); digitalWrite(led, HIGH); while (digitalRead(but) == LOW) {} rel_time = millis(); digitalWrite(led, LOW); pres_len = rel_time - pres_time; space = pres_time - old_pres; if (pres_len > min_delay) { code += MakeString(); } while ((millis() - rel_time) < (unit_delay * 3)) { if (digitalRead(but) == LOW) { goto label; } } Morse_decod(); }

What’s Next?

  • Try to build a wireless Morse encoder and decoder using the Bluetooth, NRF24L01,…
Liked What You See?​
Get Updates And Learn From The Best​

Leave a Reply

Your email address will not be published. Required fields are marked *