Morse code is a character encoding scheme used in telecommunication, and is named after Samuel F. B. Morse, an inventor of the telegraph. You would have probably seen movies where spies or secret agents transmit secret messages in form of Morse code. Actually, according to Crypto Museum, Morse code was heavily used for secret transmissions during WWI and WWII. Now it's time for you to try making a Morse code encode on your own.
I have used a Green LED and Active Buzzer in this project, so that people with hearing disabilities and visual impairment, respectively can decipher Morse Code. As you can see in the image above, the Morse code is composed of 'dot' and 'dash'. Dot is one unit long and dash is three units long.
I have prepared my sketch in such a way, that all the characters in the book from the image above can be encoded with ease.
ConnectionsLED
- Anode (+) - D3
- Cathode (-) - Ground (GND)
Active Buzzer Module (KY-012)
- S - D4
- (+) - 5V
- (-) - Ground (GND)
Even though the hardware setup of this project looks really simple, the sketch is actually composed of 994 lines. It can be compressed to nearly 950 lines. Those who use arrays will be able to compress the sketch even more. I will explain you prepare the sketch, but before you continue, please watch the YouTube video in the last section of this page to get an idea of how this works.
Create a String type variable named morseCode. It must look like this:String morseCode = ""
Create global variables for LED and the active buzzer. Create two functions named 'dot' and 'dash'. You would not need the functions to return any information so use the keyword void. As I already mentioned in the first section of this page, Morse code is composed of 'dot' and 'dash'. You may have noticed something in the video. The LED lights up for 100 milliseconds to denote the dot and 300 milliseconds to denote the dash. The buzzer also functions similar to the LED.
Within dot( ), set the pin of the LED HIGH and, after 100 ms, set the pin LOW. You can use the delay( ) function in this case. Before calling the delay( ) function, use the tone ( ) function to control your active buzzer. Calling the tone ( ) function before delay( ) function will make both buzzer and LED operate at the same time. If you are unfamiliar with the tone( ) function, please click the hyperlink and it will lead you to Arduino's function reference website. The buzzer must have a frequency of 200 Hz and must be switched on for 100 ms.
Within the dash( ) function, set the pin of the LED HIGH for 300 ms. The buzzer must have a frequency of 200 Hz and be switched on for 300ms as well. Now, within void setup( ), use the pinMode( ) function to configure each pin as an INPUT or OUTPUT. Call the Serial.begin( ) function and within the parentheses type 9600 which means the baud rate of the Serial port is 9600 bits per second. Use the Serial.print( ) function to print "Morse Code: " in your Serial Monitor.
Now, within void loop( ), type the following:
while (Serial.available( ) > 0){
int ascii = Serial.read( );
...
}
I will explain you how these lines of code works. The codes within the curly brackets will only be executed if you receive data. For more information on Serial.available( ), please visit:
https://www.arduino.cc/reference/en/language/functions/communication/serial/available/
You will be using 'switch...case' function to execute a certain set of codes only if the conditions match that case. The argument within switch( ) function will be ascii. Refer to this website to obtain the decimal values for each ASCII characters. Decimal values differ for uppercase and lowercase alphabet, which means you have to repeat the set of codes so that both uppercase and lowercase alphabet will execute the same function, as you might have seen in the video. The Morse code is the same for both uppercase and lowercase alphabet.
The backslash button must be pressed to encode a new text. The ASCII value for backlash button is 92. Here's an example:
switch (ascii){
case 92: //Set of functions that will be executed when backspace is pressed
Serial.println("\n\nMorse Code: ");
delay(100);
break;
case 82: // Set of functions that will be executed when letter 'R' is pressed.
morseCode.concat(".-.");
Serial.print(".-. ");
dot();
delay(100);
dash();
delay(100);
dot();
delay(300);
break;
case 114: // Set of functions that will be executed when letter 'r' is pressed.
morseCode.concat(".-.");
Serial.print(".-. ");
dot();
delay(100);
dash();
delay(100);
dot();
delay(300);
break;
...
}
Using morseCode.concat(paprameter) function, appends the parameter to the morseCode string. I have added 3 units space, so that when a word is typed, there are 3 units space between the letters. delay(100) function is used to add one unit space between parts of a letter and delay(300) function is used to add three units space between the letters. The delay( ) function makes the morse code to be interpreted corrected via the LED and buzzer.
You must write codes similar to this, for each characters (letters, numbers, punctuation marks and other symbols). For the morse codes of each character, please refer to the second image from the first section.
If you still have any questions with the codes, please feel free to comment below.
Final LookIf you have any questions or suggestions about this project, please feel free to comment below.
Comments