Back to basics with Morse

Nowadays there are many ways to communicate with someone, by e-mail, using your voice through cellular or even by sending a message straight to your friend’s pocket. I wouldn’t be surprised if people back in the days would have accused you of using black magic if they saw how you are able to use the technology to your advantage. I’m talking about the time when horse riding and pigeons were the predominant way for message passing.

Sometime during the 19th-century, the telegraph surfaced as a faster approach for sending your words to distant families and with it the morse code. Although the simplicity of the morse code, translating a simple English phrase to the ‘dots’ and ‘dashes’ of the morse code can be quite a tedious task for the layman. Then I wondered, what if I used my raspberry pi together with the LEDs of the sense Hat to create a script that translates my messages for me? Fast forward, that was exactly what I did!

By using the python libraries argparse and sense_hat I was able to come up with a simple script that does all the encoding for me. I’ve programmed it so that you can use it directly from the terminal. If you own a raspberry pi and the sense Hat, I encourage you to try to send your own messages to your neighbors using the script.

Usage (Terminal)

python senseHat_morseCode.py -msg "Hello World"

Python Code

import argparse
import sense_hat

# Input Arguments
parser = argparse.ArgumentParser(description = 
'Convert a Message to Morse code')
parser.add_argument('-msg', help = 
'Message to Convert')
args =  parser.parse_args()

# Setting up sensehat
sense = sense_hat.SenseHat()



CODE = {'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': '--..',   ' ': '',

        '0': '-----',  '1': '.----',  '2': '..---',
        '3': '...--',  '4': '....-',  '5': '.....',
        '6': '-....',  '7': '--...',  '8': '---..',
        '9': '----.'
        }

def showSignal(signal):
    import time
    red = [255,0,0] # Flash colour
    
    # Setting the pixels
    flash = [red for i in range(8*8)] 
    
    #Length of signal

    shortSi, longSi = float(0.2), float(1.5)  
    sleepTime = {'.': shortSi,
                 '-': longSi
                }

    for sigChar in signal:
        sense.set_pixels(flash)
        time.sleep(sleepTime[sigChar])
        sense.clear()
        time.sleep(1)


def main():

    for char in str(args.msg):
        print("Letter %s : %s" %(char,CODE[char.upper()]))
        showSignal(CODE[char.upper()])

if __name__ == "__main__":
	main()

Arian Barakat

Arian Barakat

rss facebook twitter github youtube mail spotify instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora