Introduction: Morse Code Decoder
In this project, my sister and I use an Arduino UNO board along with a push button switch and a serial monitor to decode Morse code. It is a very simple project and any Arduino beginners will have fun doing it.
This is the first project I am uploading on instructables.com. So any feedback is welcome.
Step 1: Parts Required
Parts required for building this project:
- Arduino UNO
- Breadboard
- Tactile switch (Push button)
- LED 5mm
- Resistor 220 Ω
- Jumper wires Male-to-Male (4 nos.)
- Laptop/Desktop computer for Serial Monitor
Step 2: Connections
1. Connect one end of the push button to the digital I/O pin no. 2 of the Arduino UNO board and the other end to the ground.
2. Connect the anode of the LED to the digital I/O pin no. 4 and the cathode to the ground through a 220 Ω resistor.
3. Connect the Arduino UNO to a PC using the serial communication USB cable.
4. After completion, your circuit should look similar to the given picture.
Step 3: Arduino Sketch
Initialize all the variables:
unsigned long signal_len,t1,t2; //time for which button is pressed
int inputPin = 2; //input pin for push button int ledPin = 4; //outpu pin for LED String code = ""; //string in which one alphabet is stored
Setup the serial connection and I/O pins:
void setup()
{
Serial.begin(9600); pinMode(inputPin, INPUT_PULLUP); //internal pullup resistor is used to simplify the circuit pinMode(ledPin,OUTPUT); }
Main Loop for running program and decoding:
void loop()
{ NextDotDash: while (digitalRead(inputPin) == HIGH) {} t1 = millis(); //time at button press digitalWrite(ledPin, HIGH); //LED on while button pressed while (digitalRead(inputPin) == LOW) {} t2 = millis(); //time at button release digitalWrite(ledPin, LOW); //LED off on button release signal_len = t2 - t1; //time for which button is pressed if (signal_len > 50) //to account for switch debouncing { code += readio(); //function to read dot or dash } while ((millis() - t2) < 500) //if time between button press greater than 0.5sec, skip loop and go to next alphabet { if (digitalRead(inputPin) == LOW) { goto NextDotDash; } } convertor(); //function to decipher code into alphabet }
Function for reading dot or dash:
char readio()
{ if (signal_len < 600 && signal_len > 50) { return '.'; //if button press less than 0.6sec, it is a dot } else if (signal_len > 600) { return '-'; //if button press more than 0.6sec, it is a dash } }
Function for converting dots and dashes into alphabet:
void convertor()
{ static String letters[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "E" }; int i = 0; if (code == ".-.-.-") { Serial.print("."); //for break } else { while (letters[i] != "E") //loop for comparing input code with letters array { if (letters[i] == code) { Serial.print(char('A' + i)); break; } i++; } if (letters[i] == "E") { Serial.println(""); //if input code doesn't match any letter, error } } code = ""; //reset code to blank string }
Download attached file for the complete code.
Upload the code on Arduino using the serial USB connection.
Attachments
Step 4: Using the Morse Code Decoder
Once you have uploaded the program to the Arduino board, you are ready to use your morse code decoder.
You can refer the attached morse code chart to try out your decoder.
The LED helps to know for how long the push button is pressed, so that you can differentiate between a dot and a dash.
You will have to open the serial monitor from your Arduino app on the PC. All decoded morse codes will appear on the serial monitor.
I have attached a youtube video where I am using the morse code decoder to decode a message.
In case of a any doubt, feel free to contact me or comment.

Participated in the
Microcontroller Contest 2017
56 Comments
3 years ago
This is great! It was just what I was looking for. I would say that I had to play around with the timings a little. I used 10 for time between de-bounce, 800 for skip to next dot or dash, less than 300 for dit, greater than 300 for dash. OH... and I added support for numbers. Would you guys mind if I created a new indestructible with my version? I will totally credit you for most of the work.
Here are the key lines I added for number support. I did it in a very similar way to your letter search:
bool numberFound = false;
static String numbers[] = { "-----",".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "N"
};
int i = 0;
while (numbers[i] != "N") //loop for comparing input code with letters array
{
if (numbers[i] == code)
{
Serial.print(char('0' + i));
numberFound = true;
break;
}
i++;
}
i=0
<<<<< your code checks for letters >>>>>>
if ( (letters[i] == "E") &&(numberFound == false))
{
Serial.println("<Wrong input>"); //if input code doesn't match any letter, error
}
code = ""; //reset code to blank string
numberFound = false; //reset to search for numbers again.
Reply 2 years ago
Where would I insert you extra coding?
I have successfully made the push button set up BUT can only type Es and Ts as I'm too heavy fingered!
Reply 3 years ago
Nice job. Sure, go ahead and create your own version of the instructable with numbers. Do post a link to the new instructable in the comments here.
6 years ago
I'd like to make this print to a portable thermal printer. Any notion on where I might start? :)
Reply 6 years ago
I'm thinking two sets with transceivers attached to actuate the 'switch' used. Steampunk spy walkie talkies. Without the talkie bit. Dottie Dashies?
Reply 3 years ago
Well, a line feed for every character would be pretty wasteful of paper. This code also has no breaks for words, which would make your print out a bit harder to read. I would suggest creating a variable that counts horizontal position, then line feeds to the next line when it reaches the end of each line. (probably 40 characters on most reciept printers.
Reply 6 years ago
Awesome idea. I have never really worked with a thermal printer but I guess you should check out the one by Adafruit. It's quite neat ans simple. As for your idea of using a transceiver, I am working on something similar right now. I am using an HC-05 bluetooth module to send data to my Android phone. But, your idea with with transceivers sounds cool too. Do keep me posted of your progress.
3 years ago
Your code causes "Soft WDT reset" on the NodeMCU board (ESP8266).
The ESP8266 is a little different than the standard Arduino boards in
that it has the watchdog(WDT) turned on by default. If the watchdog
timer isn't periodically reset then it will automatically reset your
ESP8266. The watchdog is reset every time loop() runs or you call
delay() or yield() but if you have blocking code like the above then the
watchdog may time out, resulting in reset.
Reply 3 years ago
Did yield() make it work for the ESP? Would like to try it out here.
Question 3 years ago
Hello,
I am making this project for my school science fair. I am trying to build it, but I am having some problems.
1) when I press the button on the breadboard, the light doesn't turn on, but when I press the reset button on the Arduino, it works.
2) I am using an Elegoo Uno R3, and you are using an Arduino UNO. Will my Elegoo work?
3) the diagram and your video are showing three different circuits. I need help, so which one do I use?
Thank you for all of your help. I hope you can help me soon!
Reply 3 years ago
Hey there,
1. Can't pinpoint what the problem is. When you press the reset button all I/Os are triggered so the LED must be lighting because of that. There must be some issue with either the circuit, the code or in worse case the board. If possible, upload a pic of the circuit. I will be able to help you better with that. Also did you directly download and use the file 'Morse_code_decoder.ino' or did you type the code manually in your compiler.
2. Using an Elegoo Uno R3 should be fine as Arduino is opens source. The technical specs of both the Elegoo board and the Arduino board will be same, the only difference being it is manufactured by different companies. Also, I used a generic board from China, which didn't even have the manufacturer's name.
3. All the three circuits are the same. They are just different representations. The first one is a circuit sketch, second one is a circuit diagram and the third one is an actual photograph.
Try checking the connections and code again. If it is still not working, upload with pic and I'll help you troubleshoot further.
Reply 3 years ago
Hello,
Below I have uploaded a few photos of the circuit. I first copied and pasted the code, not knowing that you could download the whole program. Earlier this morning I downloaded the code, so it should be working now. I also rebuilt the circuit and it looks identical to the diagrams and pictures. Still, the tactile switch on the breadboard doesn't turn on the light (LED). We thought te switch itself was faulty, so we tried to replace it, yet it still didn't work. Hopefully, with photos, you can identify the problem better. Thank you!
Reply 3 years ago
The circuit seems fine. Can't say what the problem could be?
When you press the tactile switch, does anything show up on the serial monitor on your PC?
To troubleshoot, you can try one thing. Instead of using the external LED use the built in LED on your board. Don't change anything in the circuit. Only change the LED pin number in the code. Pin no. 13 is connected to the LED on your board with an 'L' printed near it.
So instead of "int ledPin = 4;", write "int ledPin = 13;" Rest of the program remains the same.
Now check if pressing the tactile switch lights up the onboard LED labelled L.
Reply 3 years ago
Hello,
in the video below, I changed the wire that goes to the LED to #13 on the elegoo. Then, I pressed the tactile switch and the orange light on the elegoo blinked, which is what I think you mean...
also, the LED on the breadboard itself still didn't work.
Thank you for your help!
Reply 3 years ago
Is the LED glowing continuously when you keep the switch pressed? If not, there is some problem.
Because, from what I can see in the video, the LED is blinking at an interval of every 1 second. Is there any error when you upload the code to your board. Because it seems that your board is running the default 'Blink program' that it came with and your program didn't get uploaded to the board.
Reply 3 years ago
Hello,
After reading your replies several times, I understood that I should change the wiring to make the LED blink on the breadboard. Now, I made the LED on the Elegoo and the LED on the breadboard blink at the same time as each other.
These two are following the "blink each second" code/rule thing that you said, so I was looking at the program to try and delete that. I could not find the code that makes it blink. I think that once I delete the code, the actual code that you put on this site will run and the circuit will be completed. The tactile switch doesn't affect anything when you click it.
Is there anything that I can do on the Elegoo, like making the default blinking code inactive? Thanks.
Reply 3 years ago
Hey,
The Uno board is capable of having only one program at a time. Whenever you upload any program on the board, the previous program gets overwritten. So since the default Blink program is running on your board, I guess the Morse code program is not getting uploaded.
When you upload a program using the compiler, you should get an error if the code if doesn't get uploaded. There can be multiple reasons for this (eg. wrong board selected in the compiler, Arduino drivers are not installed correctly on your PC, communication IC may be damaged on the board, etc.)
I think since you are new to this, go through some youtube videos for basic Arduino Uno programming. It will help and you should be able to do the project very easily after that.
Do try again. If you are still facing problem, message me again.
Reply 3 years ago
Hello,
do you know how to make the font on the serial monitor bigger? I want to make it easier to read. thanks.
Reply 3 years ago
Go to File>Preferences and increase 'Editor font size'. This should also increase font size in serial monitor.
Or just mouse over on the serial monitor and press Ctrl + scroll mouse up.
Reply 3 years ago
Hello,
I finally got it to work! I watched videos on YouTube and I figured out what I needed to do. Instead of the online sketch, I opened the actual software in files and I opened the "serial monitor" instead of the plain old monitor. Thank you for all of your help! I really appreciate it! 😊 😊