Lesson 2
This week we will create Morse Code Generator on Arduino Uno
Project outline
The diagram of the Morse code generator based on the Arduino Uno board is shown in the following figure.

As you can see, the scheme is very simple. Simply connect the positive contact of the buzzer to pin 8 of the Arduino Uno board, and its negative contact to the ground of the Arduino Uno board. Load the program code into the Arduino board and you can start typing characters in the serial communication monitor window – the buzzer will start emitting Morse code signals corresponding to these characters. The scheme can also be improved by including a 16×2 LCD display in it.
Explanation of the program for Arduino
The full code of the program is given at the end of the article, here we will briefly review its main fragments.
First, we will declare a variable in the program in which we will write characters that we will later convert to Morse code.
char stringToMorseCode[] = "";
Then we will announce pin 8, to which the buzzer is connected, and determine the type of tone that will be used for the signal emitted by the buzzer.
int audio8 = 8; // output audio on pin 8 int note = 1200; // music note/pitch int dotLen = 100; // length of the morse code 'dot' int dashLen = dotLen * 3; // length of the morse code 'dash'
Then in the function void loop(), if any data is received via the serial port, we will save it in an indata character array. We will write characters to this array one at a time (one at a time) using the inData[index1] command. After that, the variable.toUpperCase() command is used to change lowercase characters to the corresponding uppercase characters (that is, uppercase letters are made from lowercase letters). Then we form sounds corresponding to each of these symbols.
void loop()
{
char inChar = 0;
char inData[100] = ""; // data length of 6 characters
String variable = "";
String variable1 = "";
int index1 = 0;
if ( Serial.available() > 0 ) {
while (Serial.available() > 0 && index1 < 100)
{
delay(100);
inChar = Serial.read();
inData[index1] = inChar;
index1++;
inData[index1] = '�';
}
variable.toUpperCase();
for (byte i = 0 ; i < 100 ; i++) {
variable.concat(String(inData[i]));
}
delay(20);
Next, the MorseDot and MorseDash functions are used to form the sound of a dot and a dash, respectively.
void MorseDot()
{
tone(audio8, note, dotLen); // start playing a tone
delay(dotLen); // hold in this position
}
void MorseDash()
{
tone(audio8, note, dashLen); // start playing a tone
delay(dashLen); // hold in this position
}
Then the GetChar function is used to generate the corresponding code for all the characters of the alphabet – that’s what you need to change if you want to redo this program to generate Morse code for the Russian alphabet. As a result, each character will be represented by a sequence of sounds corresponding to its Morse code.
void GetChar(char tmpChar)
{
switch (tmpChar) {
case 'a':
MorseDot();
delay(100);
MorseDash();
delay(100);
break;
…
…
…
default:
break;
}
}
After you have loaded the program into the Arduino board, you can start typing characters in the serial communication monitor window (max. 6 – limit in the program). To transfer the printed character sequence to the Arduino board press ‘Enter’ on the keyboard. We have printed the ‘SOS’ (distress signal) characters as an example – you can see the result of the project in the video at the end of the article.

Programme (sketch) source code
char stringToMorseCode[] = "";
int audio8 = 8; // к контакту 8 подключен зуммер
int note = 1200; // music note/pitch (используемый тон)
int dotLen = 100; // length of the morse code 'dot' (длительность звучания точки)
int dashLen = dotLen * 3; // length of the morse code 'dash' (длительность звучания тире)
void setup() {
Serial.begin(9600);
}
void loop()
{
char inChar = 0;
char inData[100] = ""; // data length of 6 characters
String variable = "";
String variable1 = "";
int index1 = 0;
if ( Serial.available() > 0 ) { // если в последовательном порту есть принятые данные
while (Serial.available() > 0 && index1 < 100) // read till 6th character
{
delay(100);
inChar = Serial.read(); // начинаем их последовательно считывать и сохранять в символьный массив (по одному символу)
inData[index1] = inChar;
index1++;
inData[index1] = '�'; // добавляем 0 в конец
}
variable.toUpperCase(); // преобразуем нижний регистр в верхний
for (byte i = 0 ; i < 100 ; i++) {
variable.concat(String(inData[i])); // объединяем строки
}
delay(20);
}
String stringToMorseCode = String(variable);
for (int i = 0; i < sizeof(stringToMorseCode) - 1; i++)
{
char tmpChar = stringToMorseCode[i];
tmpChar = toLowerCase(tmpChar);
GetChar(tmpChar);
}
}
void MorseDot()
{
tone(audio8, note, dotLen); // start playing a tone
delay(dotLen); // hold in this position
}
void MorseDash()
{
tone(audio8, note, dashLen); // start playing a tone
delay(dashLen); // hold in this position
}
void GetChar(char tmpChar)
{
switch (tmpChar) {
case 'a':
MorseDot();
delay(100);
MorseDash();
delay(100);
break;
case 'b':
MorseDash();
delay(100);
MorseDot();
delay(100);
MorseDot();
delay(100);
MorseDot();
delay(100);
break;
case 'c':
MorseDash();
delay(100);
MorseDot();
delay(100);
MorseDash();
delay(100);
MorseDot();
delay(100);
break;
case 'd':
MorseDash();
delay(100);
MorseDot();
delay(100);
MorseDot();
delay(100);
break;
case 'e':
MorseDot();
delay(100);
break;
case 'f':
MorseDot();
delay(100);
MorseDot();
delay(100);
MorseDash();
delay(100);
MorseDot();
delay(100);
break;
case 'g':
MorseDash();
delay(100);
MorseDash();
delay(100);
MorseDot();
delay(100);
break;
case 'h':
MorseDot();
delay(100);
MorseDot();
delay(100);
MorseDot();
delay(100);
MorseDot();
delay(100);
break;
case 'i':
MorseDot();
delay(100);
MorseDot();
delay(100);
break;
case 'j':
MorseDot();
delay(100);
MorseDash();
delay(100);
MorseDash();
delay(100);
MorseDash();
delay(100);
break;
case 'k':
MorseDash();
delay(100);
MorseDot();
delay(100);
MorseDash();
delay(100);
break;
case 'l':
MorseDot();
delay(100);
MorseDash();
delay(100);
MorseDot();
delay(100);
MorseDot();
delay(100);
break;
case 'm':
MorseDash();
delay(100);
MorseDash();
delay(100);
break;
case 'n':
MorseDash();
delay(100);
MorseDot();
delay(100);
break;
case 'o':
MorseDash();
delay(100);
MorseDash();
delay(100);
MorseDash();
delay(100);
break;
case 'p':
MorseDot();
delay(100);
MorseDash();
delay(100);
MorseDash();
delay(100);
MorseDot();
delay(100);
break;
case 'q':
MorseDash();
delay(100);
MorseDash();
delay(100);
MorseDot();
delay(100);
MorseDash();
delay(100);
break;
case 'r':
MorseDot();
delay(100);
MorseDash();
delay(100);
MorseDot();
delay(100);
break;
case 's':
MorseDot();
delay(100);
MorseDot();
delay(100);
MorseDot();
delay(100);
break;
case 't':
MorseDash();
delay(100);
break;
case 'u':
MorseDot();
delay(100);
MorseDot();
delay(100);
MorseDash();
delay(100);
break;
case 'v':
MorseDot();
delay(100);
MorseDot();
delay(100);
MorseDot();
delay(100);
MorseDash();
delay(100);
break;
case 'w':
MorseDot();
delay(100);
MorseDash();
delay(100);
MorseDash();
delay(100);
break;
case 'x':
MorseDash();
delay(100);
MorseDot();
delay(100);
MorseDot();
delay(100);
MorseDash();
delay(100);
break;
case 'y':
MorseDash();
delay(100);
MorseDot();
delay(100);
MorseDash();
delay(100);
MorseDash();
delay(100);
break;
case 'z':
MorseDash();
delay(100);
MorseDash();
delay(100);
MorseDot();
delay(100);
MorseDot();
delay(100);
break;
default:
break;
}
}
