# My First Project - Morse Code

**URL:** <https://forum.arduino.cc/t/my-first-project-morse-code/1014104>\
**Category:** Showcase\
**Created:** [July 20, 2022, 2:10am UTC](https://forum.arduino.cc/t/my-first-project-morse-code/1014104 "2022-07-20T02:10:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![slowcoder](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@slowcoder](https://forum.arduino.cc/u/slowcoder)\
**Post date:** [July 20, 2022, 2:10am UTC](https://forum.arduino.cc/t/my-first-project-morse-code/1014104/1 "2022-07-20T02:10:37Z")

</div>

I know ... a lot of people do a morse code project. This is my version. I won't go deep into description, and allow the code to speak for itself.

I welcome comments and critiques.

```cpp
String sentence = "Hello! Welcome to my morse code project!"; // word, sentence or special message to be displayed
String morseCode;

bool repeat = true;

// durations
int morseDitDurationMs = 50;
int morseDahDurationMs = 500;
int morseSpaceDurationMs = 1500; // ms to pause between words
int morseDelayBetweenCharsMs = 500; // ms to pause between each symbol
int repeatDelayMs = 5000; // ms to pause before repeating

int ledPin = LED_BUILTIN;

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);

  sentence.toUpperCase();
  morseCode = getMorseCode(sentence);
  Serial.println("\"" + sentence + "\" = \"" + morseCode + "\"");
}

void loop() {
  for (int i = 0; i < morseCode.length(); i++) {
    char morseSymbol = morseCode[i];
    //Serial.print(morseSymbol);
    if (morseSymbol == ' ') {
      delay(morseSpaceDurationMs);
    } else {
      flashMorseSymbol(morseSymbol);
      delay(morseDelayBetweenCharsMs);      
    }
  }
  
  if (!repeat) exit(0);

  delay(repeatDelayMs);
}

// convert sentence to morse
String getMorseCode(String sentence) {
  if (sentence == "") return "";

  String morse = "";
  if (sentence[0] != '[' || sentence[sentence.length()-1] != ']') { 
    // convert each character
    for (int i = 0; i < sentence.length(); i++) {
      morse += getMorseCodeFromChar(sentence[i]);
    }
  } else {
    // convert special messages
    if (sentence == "[SOS]") morse = "...---...";
    if (sentence == "[NEW_LINE]") morse = ".-.-"; 
    if (sentence == "[NEW_PAGE]") morse = ".-.-.";
    if (sentence == "[NEW_PARAGRAPH]") morse = "-...-";
    if (sentence == "[ATTENTION]") morse = "-.-.-";
    if (sentence == "[ERROR]") morse = "........";
    if (sentence == "[WAIT]") morse = ".-...";
    if (sentence == "[BREAK]") morse = "-... -.-";
    if (sentence == "[CLOSING]") morse = "-.-. .-..";
    if (sentence == "[SHIFT_TO_WABAN]") morse = "-..---";
    if (sentence == "[END_OF_CONTACT]") morse = "...";
    if (sentence == "[UNDERSTOOD]") morse = "...-.";
    if (sentence == "[INVITE_NAMED_STATION_TO_TRANSMIT]") morse = "-.--.";
    if (sentence == "[INVITE_ANY_STATION_TO_TRANSMIT]") morse = "-.-";
  }
  return morse;
}

// convert single character to morse (international)
String getMorseCodeFromChar(char character) {
  String charMap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 .,?!'():;/_=+-$@";
  String morseMap[] = {".-.","-...","-.-.","-..","." // A-E
                      ,"..-.","..-","....","..",".---" // F-J
                      ,"-.-",".-..","--","-.","---" // K-O
                      ,".--.","--.-",".-.","...","-" // P-T
                      ,"..-","...-",".--","-..-","-.--","--.." // U-Z
                      ,".----","..---","...--","....-","....." // 1-5
                      ,"-....","--...","---..","----.","-----" // 6-0
                      ," ",".-.-.-","--..--","..--..","-.-.--" // [space].,?!
                      ,".----.","-.--.","-.--.-","---...","-.-.-." // '():;
                      ,"-..-.","..--.-","-...-",".-.-.","-....-" // /_=+-
                      ,"...-..-",".--.-." // $@
                    };

  int charPos = charMap.indexOf(character);
  return morseMap[charPos];
}

// flash dit or dah symbol
void flashMorseSymbol(char morseSymbol) {
  int ledOnDuration = 0;
  switch (morseSymbol) {
    case '.': ledOnDuration = morseDitDurationMs; break;
    case '-': ledOnDuration = morseDahDurationMs; break;
    default: ledOnDuration = 0;
  };

  digitalWrite(ledPin,HIGH);
  delay(ledOnDuration);
  digitalWrite(ledPin,LOW);
}

```

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [July 20, 2022, 4:37am UTC](https://forum.arduino.cc/t/my-first-project-morse-code/1014104/2 "2022-07-20T04:37:54Z")

</div>

A dit element is one time unit  
A dah element is three time units  
Inter-element gap is one time unit  
Inter-letters gap is three time units  
Inter-word gap is seven time units.

---

<div class="post-metadata">

**Author:** ![slowcoder](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@slowcoder](https://forum.arduino.cc/u/slowcoder)\
**Post date:** [July 20, 2022, 11:55am UTC](https://forum.arduino.cc/t/my-first-project-morse-code/1014104/3 "2022-07-20T11:55:32Z")

</div>

Excellent! I can update my timing accordingly.

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [July 20, 2022, 1:05pm UTC](https://forum.arduino.cc/t/my-first-project-morse-code/1014104/4 "2022-07-20T13:05:45Z")

</div>

Good work. 73

---

<div class="post-metadata">

**Author:** ![mrburnette](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mrburnette/32/702556_2.png) [@mrburnette](https://forum.arduino.cc/u/mrburnette)\
**Post date:** [July 20, 2022, 3:46pm UTC](https://forum.arduino.cc/t/my-first-project-morse-code/1014104/5 "2022-07-20T15:46:04Z")

</div>

> [@xfpd](#):
>
> A dit element is one time unit  
> A dah element is three time units  
> Inter-element gap is one time unit  
> Inter-letters gap is three time units  
> Inter-word gap is seven time units.

In [Magic Morse](https://trybotics.com/project/magic-morse-on-arduino-f48633), it looks like:

```arduino
void setspeed(byte value)
{
  WPM = value;
  DITmS = 1200 / WPM;
  DAHmS = 3 * 1200 / WPM;
  wordSpace = 7 * 1200 / WPM;
}

```

---

<div class="post-metadata">

**Author:** ![pert](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/pert/32/217111_2.png) [@pert](https://forum.arduino.cc/u/pert)\
**Post date:** [May 15, 2026, 6:15am UTC](https://forum.arduino.cc/t/my-first-project-morse-code/1014104/8 "2026-05-15T06:15:35Z")

</div>

Closed on May 15, 2026, 6:15am UTC


