Nithitsuki's Website

Projects Tutorials About-Me Blogs Photography


Arduino IDE Morse Code Library

The Simplest and most well documented Morse Code Library for the Arduino IDE!

wanna skip to the installation? scroll down you lazy.

     How it all started (a memoir)

  • When I joined Amrita University in fall 2024, one of my first memories was a hackathon called Electronika—an overnight event with Arduinos, late-night coffee, and a cool prize pool. Though I never participated (^_^;), it’s how I met two friends. One of them teamed up with me to practice, compete, and (hopefully) win.

     The Spark for the Library

  • My teammate was seriously dedicated to the hackathon, borderline scary levels of focus. Inspired, I brought my Arduino to campus, and during practice, we had the idea to transmit Morse code—something simple like SOS. That’s when I thought of creating functions to generalize Morse code transmission, like a library. I went home, whipped up dot(), dash(), and char_to_morse() for lowercase letters, and threw it on GitHub for a few months.

     A Semester Later

  • Fast forward to the end of the semester. With a determination to finish something for once, I decided to revisit my simple code. I polished it up (re-wrote the entire thing from scratch), added lots of features, documented it thoroughly, and made sure it was easy for anyone to use.

     The Result: My First Ever Tech Project

  • And here we are! I’m proud to say that this is the only Morse code library for Arduino I know of, which is both easy to install, well-documented, and simple to use. It might not have been a huge technical challenge, but the real victory for me was in finishing it and ending up with a polished product—a far cry from the rough draft it started as.

     Looking Ahead

  • This project was my first real tech endeavor, and while I’ve still got plenty of unfinished projects on my website (as you can see in the projects page!), this is one that I’m proud of. I hope this library helps you as much as it’s helped me, and I look forward to finishing the rest of my projects the same way!


About the Library

This library is designed to be as simple as possible while still providing everything you need to send Morse code signals reliably and efficiently. Whether you’re a beginner or an experienced maker, this library can help you quickly get your Morse code project up and running.


Key Features:

  • Pin Control: Set the pin that will output Morse code signals.
  • Adjustable Speed: Easily control the speed of the transmission using Words Per Minute (WPM).
  • String Transmit: Convert entire strings into Morse code and transmit them through the Arduino pin.
  • Character Transmit: transmit single characters through the Arduino pin.

How to Install the Library:

To get started with the Morse Code Library:

  1. Go to the Arduino IDE.
  2. Navigate to Sketch > Add File… and select the Morse.h file
    which you should download from the Github Release page (click me) or Here (click me), Make sure to download the Morse.h file (as a .h file, and not a text file) if your browser decides to display it
  3. That’s it! No complicated installations or setup steps.

Usage:

Setting Up Your Project

The library comes with predefined variables that allow for easy customization. Here’s how to use it:

  • Set the Morse Code Pin:
    The output pin for Morse code transmission is set by the MORSE_PIN variable.
    It’s already predefined in the Morse.h file, so please dont try to redefine it. (int MORSE_pin)
//Set the pin that will output the Morse code
MORSE_PIN = 13;
//Make Sure to set said morse pin to output
pinMode(MORSE_PIN, OUTPUT);
  • Set the Transmission Speed:
    The UNIT_TIME variable defines the speed of the Morse code transmission in milliseconds.
    The dot (or “dit”) duration is calculated based on the formula 1200 / wpm, where wpm is the speed in Words Per Minute. The default speed is already defined, so there’s no need to manually adjust it. However, you can change the speed if needed.
    WARNING: Do not redefine UNIT_TIME in your sketch. The variable is already set in the header. WARNING: Its value must NOT be lower than 0
// Adjust the time of the dot
// (unit time in milliseconds)
UNIT_TIME = 150; 

Core Functions

The library offers a few core functions that make transmitting Morse code easy and efficient:

  • dot(int n)
    This function sends a dot (dit) signal for a specified number of repetitions.
    The parameter n must be greater than or equal to 1.

  • dash(int n)
    This function sends a dash (dah) signal for a specified number of repetitions.
    Again, the parameter n must be greater than or equal to 1.

  • char_to_morse(char c)
    This function converts a single character into its corresponding Morse code and transmits it.
    It works for both uppercase and lowercase letters, digits, and spaces.

  • string_to_morse(String mCode)
    This function converts an entire string into Morse code and sends it character by character.
    It automatically handles spaces between words, which are represented by a longer pause.


Example Code

Here’s a simple example that demonstrates how to use the library to send Morse code:

#include "Morse.h"

void setup() {
 // Set the output pin and transmission speed
 // Pin 13 will output the Morse signals
 MORSE_PIN = 13;
 //Make Sure to set said morse pin to output
 pinMode(MORSE_PIN, OUTPUT);

// Time of dot in milliseconds
//UNIT_TIME = (1200 / wpm) (8wpm default)
 UNIT_TIME = 150;
}

void loop() {
 // Send the string "HELLO WORLD"
 string_to_morse("HELLO WORLD"); 
 // This will repeatedly transmit "HELLO WORLD"
}

Explanation of the Code:

  • In the setup() function, we configure the output pin (MORSE_PIN) and the transmission speed (UNIT_TIME).
  • The loop() function continuously transmits the string “HELLO WORLD” using the string_to_morse() function. Each character is converted to Morse code and transmitted one after another.

Supported Characters

This library supports the following characters:

  • Letters: a-z (both uppercase and lowercase)
  • Digits: 0-9
  • Space: Used to represent a word space in Morse code.
  • few special chaarcters . , ? ' ! / ( ) & : ; = + - \ " $ _

Conclusion (TL;DR)

My first project, proud of it, easy to use, email if any doubts

Feel free to check out the source code on GitHub (click me),
I’d love to see the projects you create with it! Please share them with me if you can.