Morse Code on the micro:bit

Bloggedin
5 min readJul 15, 2020

--

Being a lover of spy movies I thought it would be good to look at radio communication on the micro:bit and reflect on a old technology used to communicate over distance, Morse Code named after Samuel Morse.

Morse code has been around since the early 19th century with the advent of telegram communication. The beauty of Morse Code is that it can be represented by light and or sound making it a very accessible form of communication and with practice and regular use it is easily memorised.

For this experiment we are going to use two micro:bits flashed with the same code containing a slimmed down version of international morse code (just the letters and my own code for a space) but you can easily add the full character set as laid out here. The limitation to this program is that we can only send 19 characters as a string over radio on the micro:bit which means our messages have to be short.

If you study the dots and dashes assigned to letters you will notice that the most frequently occurring letters such as e have the fewest representations to speed up the sending of transmissions.

Morse code breaks down characters into a sequences of dots and dashes which works out perfect for the micro:bit equipped with two main buttons. We are going to program button A for dots and button B for dashes, we will need to make a space to break up our words and a means to add them to a variable (message). Because Morse code has varying amounts of dots and dashes we will also need a means to signal the end of our sequence of dots and dashes as well as signalling the message is over prior to transmission.

As mentioned we are going to stay within the native features of the micro:bit, so let’s run through the logic of the program before we build it in makecode.

On startup we will need to initiate the radio group, some variables and two arrays containing a means to look up our sequences and convert to letters.

Button A will produce dots, Button B will produce dashes and we need to capture them until we have finished the code for a letter, pressing button A + B will cover this as well as capturing the end of our message.

Let’s walk through the makecode:

We have declared three string variables message, currentChar and lookupChar and initiated them as empty strings. Then we make two arrays; one for our letters and one called mcode which will be a look up for our dots and dashes; dots are represented by 0 and dashes are represented by 1.

Here is the structure of the two arrays:

Space is non standard Morse Code something I have put in as a quick identifier that you will see in the code later on.

Button A & Button B are as follows:

Programatically they are similar in function, pressing button A shows a dot on our LED display and also adds a 0 to a variable called currentChar, the currentChar is appended to another variable called lookupChar. Conversely pressing button B shows a dash on our LED and adds a 1 to currentChar and also appends to lookupChar. Using button A + B builds up the dots and dashes (0’s & 1’s) into lookupChar which we then need to compare to our arrays to find our letter. This part is captured by pressing buttons A + B together.

First we give a visual indication of both buttons pressed with a tick icon. We then perform some logic checks on the lookupChar. By checking if lookupChar = “” (nothing) we are checking that no characters are present, this is a way to signify that we have finished our message and its ready to send via radio.

If the lookupChar is not empty i.e has 0’s & 1’s present then we perform different logic, first we evaluate the lookupChar by getting the index of our code from the mcode array. We then use this index against our other array called letters to retrieve the corresponding letter. You now see why I have added space if we lookupChar returns as space then we add a space in our message if not we continue to add the current letter to our message variable. This process continues to build up the message until we press A + B immediately after submitting a character.

Sounds a little confusing but to send a typical “SOS” message we do the following sequence:

AAA,(A+B),BBB,(A+B),AAA,(A+B),(A+B)

To send a two word message such as “Help Me” we would enter the following sequence:

H = AAAA (A+B)

E = A (A+B)

L = ABAA (A+B)

P = ABBA (A+B)

Space = BBBB (A+B)

M = BB (A+B)

E = A (A+B) (A+B)

When we send the (A+B) after an (A+B) we trigger the radio send

Copying the entire code to two micro:bits gives us a two way Morse Code device.

This is a basic working introduction slightly limited by the native features of the micro:bit but clearly capable and a fun experiment to try out. Why not see if you can improve the code and add the full Morse Code library, if you have speakers you will also note that we have sound in the code. Given that the message string is limited to 19 characters we could look to produce a sound only based system to remove this limitation. If you wanted to go even more secret squirrel why not see if you can encrypt the message so that it can only be decoded by your micro:bit? The possibilities are only limited by your imagination.

More articles available on www.bloggedin.co.uk

--

--

Bloggedin
Bloggedin

Written by Bloggedin

Author of https://www.bloggedin.co.uk, Coding Python, Raspberry Pi, micro:bit, IoT and Node-RED.

No responses yet