Home > Articles > Sean's Raspberry Pi coding tutorials > Morse code transmitter

Morse code transmitter - flash messages to friends with an LED connected to a Raspberry Pi

For issue 144 of The MagPi magazine, I created a program that flashes Morse code messages. In this version, you can type in a message to have it converted to dots, dashes and flashes.

What is the Morse code transmitter?

The photo shows strips of paper with Morse code messages stamped into them Morse code is a way of sending messages using patterns of lights or sounds. Each letter and number is encoded using dots and dashes. A dot represents a short flash or sound, and a dash represents a long dash or sound (three times the length of a dot).

For issue 144 of The MagPi I wrote an introductory electronics tutorial. It shows you how to connect your first LED to a Raspberry Pi, and gives you the code to make it flash. To make the program a bit more useful, I turned it into a Morse code transmitter.

The version in the mag flashed a sequence of dots and dashes that was written in the program.

For the online version here, I've expanded it so that you can type in messages and they will be converted to Morse code. There wasn't room in the magazine for this longer listing.

On this webpage, you can download the code for the project.

For more information on how the code works, get issue 144 of The MagPi.

The photo shows strips of paper with Morse code messages stamped into them. I photographed it at the Porthcurno Museum of Global Communications.

Setting up your circuit

This picture shows you how to connect your LED to Raspberry Pi using a breadboard. Use a 330 ohm resistor to stop the LED drawing too much current.

Circuit board, showing the Raspberry Pi connected to a breadboard with an LED and a resistor connected in series.

Connect your LED to your Raspberry Pi like this. You can conncet to other GPIO pins, but you'll need to modify the code if you do.

Download the Morse code transmitter code

# Morse code transmitter by Sean McManus - www.sean.co.uk
from gpiozero import LED
from time import sleep

led1 = LED(14)

def blink(length):
    led1.on()
    sleep(length)
    led1.off()
    sleep(0.5)
        
def flash_message(message):
    for symbol in message:
        if symbol == ".":
            blink(0.5)
        if symbol == "-":
            blink(1.5)
        if symbol == " ":
            sleep(1.5)

morse_code_dict = {
    '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': '----.',
    ' ': ' ' # adds an extra space where there's a space in the message
}

while True:
    message = input("Enter your message: ")
    morse = ""
    for letter in message.upper():
        if letter in morse_code_dict:
            morse += morse_code_dict[letter] + " "
    flash_message(morse)

More Raspberry Pi projects

Find more Raspberry Pi projects and tutorials here.

Credits

© Sean McManus. All rights reserved.

Visit www.sean.co.uk for free chapters from Sean's coding books (including Mission Python, Scratch Programming in Easy Steps and Coder Academy) and more!

Discover my latest books

Earworm

Earworm

New 10th anniversary edition of my acclaimed novel about what happens when AI and the music business collide.

Coding Compendium

Coding Compendium

A free 100-page ebook collecting my projects and tutorials for Raspberry Pi, micro:bit, Scratch and Python.

Web Design in Easy Steps

Web Design IES

Web Design in Easy Steps, now in its 7th Edition, shows you how to make effective websites that work on any device.

100 Top Tips: Microsoft Excel

100 Top Tips: Microsoft Excel

Power up your Microsoft Excel skills with this powerful pocket-sized book of tips that will save you time and help you learn more from your spreadsheets.

Scratch Programming in Easy Steps

Scratch Programming IES

This book, now fully updated for Scratch 3, will take you from the basics of the Scratch language into the depths of its more advanced features. A great way to start programming.

Mission Python book

Mission Python

Code a space adventure game in this Python programming book published by No Starch Press.

Walking astronaut from Mission Python book Top | Search | Help | Privacy | Access Keys | Contact me
Home | Newsletter | Blog | Copywriting Services | Books | Free book chapters | Articles | Music | Photos | Games | Shop | About