Morse Code Decoder

27,178

47

39

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.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Parts Required

Parts required for building this project:

  1. Arduino UNO
  2. Breadboard
  3. Tactile switch (Push button)
  4. LED 5mm
  5. Resistor 220 Ω
  6. Jumper wires Male-to-Male (4 nos.)
  7. 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.

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.

Microcontroller Contest 2017

Participated in the
Microcontroller Contest 2017

Share

Recommendations

  • Indoor Lighting Contest

    Indoor Lighting Contest
  • Make It Fly Challenge

    Make It Fly Challenge
  • Growing Beyond Earth Maker Contest

    Growing Beyond Earth Maker Contest

39 Discussions

0
None
MayankP37

Question 16 days ago on Step 4

I made it, now can you help me to add SPACE & BACKSPACE to it ??!

0
None
thatrandomcatboi

Question 2 months ago on Step 3

so ive replaced the button with a ky-038 microphone, and re coded the led to pin 13, so i have the builtin led on my side, and it works, it shows that beeping into the mic counts as an input, but minor problem, the serial moniter shows wrong input any ideas?

5 answers
0
None
pinaki_s91thatrandomcatboi

Answer 2 months ago

Using a microphone might need different amount of time than a push button. Try changing the 600 millisec time used for signal length and try out different values.

0
None
pinaki_s91thatrandomcatboi

Answer 2 months ago

Not quite sure why that would happen.
What output is it showing on the serial monitor?

0
None
andyneymit

2 months ago

Can this code be modified to work with Arduino Adafruit CircuitPlayround Classics?

1 reply
0
None
pinaki_s91andyneymit

Reply 2 months ago

Yes, you can use this code on Adafruit Circuit Playground Classic.
I have never coded a Circuit Playground myself, but I feel the code should work fine. The only things you might have to change is codes relating to the input push button and output LED and directly make use of the onboard LEDs and buttons.
Do try out and post your results on the website.

0
None
quarshie_mekaila

Question 5 months ago on Step 4

im having trouble outputing the right letters. The only thing that work is I E S. I use the same code but instead use LCD

0
None
quarshie_mekaila

5 months ago

im having trouble outputing the right letters. The only thing that work is I E S. I use the same code but instead use LCD

0
None
clrwl04

10 months ago

Thanks for this, I had this up and running in minutes. no changes to the code required. Downloaded, opened with Arduino 1.6.5 uploaded it to Arduino UNO and run.

1 reply
0
None
reinder2003

Question 11 months ago on Step 4

hello im new! trying to install a piezo speaker but i dont know what code to use and how to apply it ,any ideas?

1 answer
0
None
chimmytoo

1 year ago

How did you decide to use a 220 ohm resistor? Was there a calculation for this?

1 reply
0
None
pinaki_s91chimmytoo

Reply 1 year ago

Hi. No I didn't really do any calculation for this. The only purpose of the resistor is to limit the current flow through the circuit. Without the resistor, the Arduino may get damaged over long period of usage. As for selection of 220 ohms, it's just a thumb rule that I follow. You can use a higher or lower resistance resistor - it will only affect the current flowing through the circuit, thereby changing the intensity of the LED. If you are still not satisfied and want to learn more about it, go through the following two articles - they explain it very nicely.

https://hardwarefun.com/tutorials/calculating-corr...

https://www.evilmadscientist.com/2012/resistors-fo...

0
None
chimmytoo

Question 1 year ago

Awesome idea! How did you decide to use the 220 ohms resister specifically? What calculation did you do?

1 answer
0
None
pinaki_s91chimmytoo

Answer 1 year ago

Hi. No I didn't really do any calculation for this. The only purpose of the resistor is to limit the current flow through the circuit. Without the resistor, the Arduino may get damaged over long period of usage. As for selection of 220 ohms, it's just a thumb rule that I follow. You can use a higher or lower resistance resistor - it will only affect the current flowing through the circuit, thereby changing the intensity of the LED. If you are still not satisfied and want to learn more about it, go through the following two articles - they explain it very nicely.

https://hardwarefun.com/tutorials/calculating-corr...

https://www.evilmadscientist.com/2012/resistors-fo...

0
None
tylbellew

Question 1 year ago

I get this error when compiling:

F7P3LDOIYOQISM2.cpp.o: In function `_GLOBAL__sub_D_signal_len':
/usr/share/arduino/F7P3LDOIYOQISM2.ino:10: undefined reference to `atexit'
collect2: error: ld returned 1 exit status

Anyone have any suggestions.