Make a Morse Code Transceiver Using Arduino
by Lisleapex Blog in Circuits > Arduino
315 Views, 1 Favorites, 0 Comments
Make a Morse Code Transceiver Using Arduino
Morse code expresses different English letters, numbers, punctuation marks, etc. through different arrangements. The International Morse Code is still in use today. For example, the most well-known use of Morse code is to send the distress signal SOS. The combination of SOS signals is:. As another example, suppose we send "Arduino" via Morse code with the combination: ".- .-. -.. ..- .. -. —".
Supplies
Breadboard
Arduino IDE
Circuit Part
Arduino to Breadboard Pinout:
Pin D2 is connected to one end of button 1 and then to ground through a resistor. The other end of button 1 is connected to 5V.
Pin D7 is connected to one end of button 2, and then connected to ground through a resistor. The other end of button 2 is connected to 5V.
Pin D8 is connected to the LED positive terminal through a resistor, and the negative terminal is connected to ground.
Pin D12 is connected to the positive pole of the buzzer through a resistor, and the negative pole is connected to ground.
Code Part-1
At the end of the article is the complete code, save it as MorseCode.ino, then open the Arduino IDE "File->Open->MorseCode.ino" and upload it to Arduino. After the upload is completed, open the serial monitor and you will see the following display:
Code Part-2
The operation sequence of the Morse code decoder: first write Morse code by clicking button 1 and button 2; fill in the space between letters with 2 in the input box above, and then press the Enter key. For the spaces between words, fill in 3 in the input box above, and then press Enter. After all input is completed, fill in 1 in the above input box and press Enter, the Morse code content will be translated. The translated Morse code will be displayed below on the serial monitor. For example: let's write ".- .-. -.. ..- .. -. —", which will be displayed as "ARDUINO" after translation.
The Complete Arduino Code Is As Follows
/*
This Program is for demonstration of MORSE CODE Communication
which was use to send information secretly using codes of combinations dots . and dashes -
Thanks to open source community
*/
#define SIZE 26
const int ledPin = 8;
const int speakerPin = 12;
const int dotButton = 2;
const int dashButton = 7;
String morseCode = "";
String text = "";
int characterAscii = 0;
int startPos = 0, endPos = 0;
int startPos1 = 0, endPos1 = 0;
String characterCode = "";
int dashButtonState = 0;
int dotButtonState = 0;
//Array of MorseCode for letters of English Language A to Z
String letters[SIZE] = {
// A to I
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
// J to R
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
// S to Z
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."
};
void setup() {
// put your setup code here, to run once:
//Getting things Ready
pinMode(ledPin, OUTPUT);
pinMode(dotButton, INPUT);
pinMode(dashButton, INPUT);
Serial.begin(9600);
Serial.println("*************************************************************");
Serial.println(" Demonstration of Morse Code ");
Serial.println("************************************************************* ");
Serial.println("\nInstructions");
Serial.println("1. First Write Your Morse code");
Serial.println("2. When you are done Write 1 on above input box and Press Enter or click Send Button ");
Serial.println("3. For Space between letters write 2 and Press Enter ");
Serial.println("4. For Space between words write 3 and Press Enter ");
Serial.println("5. Thats all Translation of Morse Code will then be Shown ");
Serial.println("\n\nEnter Your Morse Code Here ");
}
void loop() {
// put your main code here, to run repeatedly:
while (Serial.available() > 0) {
int ascii = Serial.read();
switch (ascii) {
case 49: // 49 is Ascii value of 1
Serial.print("\n");
morseCode.concat('#'); // Placeing # at the end of morseCode to simplify further processing
Serial.print("\nYour Morse code Translation : ");
endPos1 = morseCode.indexOf('#');
while (endPos1 < morseCode.length()) {
extractLetters(morseCode.substring(startPos1, endPos1)); // This function would extract Letter as name suggest and would convert code to text SIMPLE!
startPos1 = endPos1 + 1;
if (startPos1 == morseCode.length()) {
break;
}
endPos1 = morseCode.indexOf('#', startPos1);
}
startPos1 = 0;
endPos1 = 0;
text = ""; // For New Translation
morseCode = "";
Serial.println("\n\nEnter Your Morse Code Here ");
break;
case 50: // 50 is Ascii value of 2
morseCode.concat("@");
Serial.print("@");
delay(200);
break;
case 51: // 51 is Ascii value of 3
morseCode.concat("#");
Serial.print("#");
delay(200);
break;
}
}
process();
}
void turnONLedSpeaker(int du) {
//Turn ON LED
digitalWrite(ledPin, HIGH);
tone(speakerPin, 4699, du); // tone(speakerPin, frequency, duration in milliSec)
}
void process() {
dotButtonState = digitalRead(dotButton);
dashButtonState = digitalRead(dashButton);
if (dashButtonState == HIGH) {
turnONLedSpeaker(400);
morseCode.concat("-"); // Storing code in variable morseCode with the help of concatenation function
Serial.print("-"); //Prints User entered Code
delay(200);
} else if (dotButtonState == HIGH) {
turnONLedSpeaker(300);
morseCode.concat(".");
Serial.print(".");
delay(200);
} else {
//Turn OFF LED
digitalWrite(ledPin, LOW);
}
}
char convertIntoText(String characterCode) {
characterAscii = 65;
for (int index = 0; index < SIZE; index++) {
if (characterCode == letters[index]) {
return characterAscii;
}
characterAscii++;
}
}
void extractLetters(String words) {
words.concat('@'); // Placeing @ at the end of word to simplify further processing
endPos = words.indexOf('@');
//Loop to extracting single character morse Code from string of word
while (endPos < words.length()) {
characterCode = words.substring(startPos, endPos);
//Now CharacterCode will now convert in text
text.concat(convertIntoText(characterCode));
startPos = endPos + 1;
characterCode = "";
// if condition is just to terminate loop when our extracting single character code is complete thats all
if (startPos == words.length()) {
break;
}
endPos = words.indexOf('@', startPos);
}
Serial.print(text);
Serial.print(" ");
startPos = 0;
endPos = 0;
text = "";
}