Introduction: Arduino Morse Code Transmitter
In this instructable, you'll use an Arduino Uno to create a Morse Code transmitter, and use the serial monitor to read the messages you've transmitted.
Parts you'll need:
Step 1: Provide Power to Your Breadboard
Connect a jumper wire from the 5V pin on your Arduino Uno to the positive line on your breadboard.
Step 2: Ground Your Breadboard
Now connect a wire from any of the GND pins on the Arduino to the negative line on your breadboard.
Step 3: Insert Your Button
Insert your button. Make sure that two of its legs are on each side of the channel down the middle of your breadboard, and the legs are inserted firmly. It is easy to bend them when you press hard, so take care while you push down on the button.
Step 4: Ground Your Button
Connect the button to ground by inserting one end into the same row as the top leg of your button, and the other end into the negative row that you previously connected to ground.
Step 5: Connect Your Button
Close the button circuit and allow the Arduino to read its input by connecting a wire from the same row as the bottom button leg, and pin 7 on the Arduino.
Step 6: Insert Your Buzzer
Insert your buzzer so that the "+' sign on the top, or the slightly longer leg, are on the same side of the breadboard as your wire connected to 5V.
Step 7: Ground the Buzzer
Connect the button to ground with a wire from the same row as its shorter leg to the negative line on the breadboard that you previously connected to GND.
Step 8: Power the Buzzer
Provide power to the buzzer and allow the Arduino to control it with a wire from the same row as its longer leg to pin 8 on the Arduino.
Step 9: Write Your Code
Copy and paste our code, or download the attached file.
<p>static String Morse[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "E" };</p>
static char Alphabet[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y', 'z', 'E'}; unsigned long push_length,start_push,end_push; //time for which button is pressed int button = 7; //input pin for push button int buzzer = 8; //outpu pin for LED String code = ""; //string in which one alphabet is store
void setup() { Serial.begin(9600); pinMode(button, INPUT_PULLUP); //internal pullup resistor is used to simplify the circuit pinMode(buzzer,OUTPUT); Serial.println("Begin your message!"); }
void loop() { MorseTransmission: while (digitalRead(button) == HIGH) {} start_push = millis(); //time at button press tone(buzzer, 150); while (digitalRead(button) == LOW) {} end_push = millis(); //time at button release noTone(buzzer); push_length = end_push - start_push; //time for which button is pressed if (push_length > 50){ //to account for switch debouncing code += dot_or_dash(push_length); //function to read dot or dash } while ((millis() - end_push) < 500) //if time between button press greater than 0.5sec, skip loop and go to next alphabet { if (digitalRead(button) == LOW) { goto MorseTransmission; } } Morse_translation(code); //function to decipher code into alphabet }
char dot_or_dash(float length) { if (length < 600 && length > 50) { return '.'; //if button press less than 0.6sec, it is a dot } else if (length > 600) { return '-'; //if button press more than 0.6sec, it is a dash } }
void Morse_translation(String morsecode) { int i = 0; if (code == ".-.-.-") { Serial.print("."); //for break } else { while (Morse[i] != "E") //loop for comparing input code with letters array { if (Morse[i] == morsecode) { Serial.print(Alphabet[i]); break; } i++; } if (Morse[i] == "E") { Serial.println("Error!"); //if input code doesn't match any letter, error } } code = ""; //reset code to blank string }
Attachments
Step 10: Use the Serial Monitor to Read Your Output!
Open the serial monitor to view your messages as you press the button to create Morse Code. Use the guide above to sequence your dots and dashes appropriately!
Step 11: Want More Projects Like This?
Get parts for 2-3 projects per month and instructions and video to build them with MakeCrate!