Morse Code on an LED

Difficulty: beginner

This tutorial will guide you through safely connecting up an LED to your Raspberry Pi and being able to turn it on and off from Python. Then you will write a program to take input from the keyboard and send it out in Morse code from the LED.



If you are using one of our SD cards or images the necessary tools should already be included; if not you will need to download and install the Python RPi.GPIO library.

Plug your ribbon cable into your breakout board and the breakout board into your breadboard. Plug the other end of the ribbon cable into your Raspberry Pi, being careful to ensure that the red stripe corresponding to pin 1 is on the correct side (it should be at the edge of the board).

Wire up the LED through the 220Ω current-limiting resistor to a pin of your choice, observing the polarity of the LED. (The flat side of the LED should be connected to the ground pin, and the other side should be connected to the GPIO pin.) Your breadboard should now look something like this:

Here, pin 8 has been used to control the LED.

If you wish to drive a much larger LED or another device which draws more power you will need to use a transistor so as not to burn out the Pi’s pin.

Now that our circuit is wired up correctly (you can connect to 3.3V instead of the GPIO pin to test it’s working), it’s time to get programming!

In a terminal, type

sudo python  

to get a Python console running as root – this is necessary as GPIO pin access isn’t available in userspace.

Now :

import RPi.GPIO as GPIO
import time
pinNum = 8
GPIO.setmode(GPIO.BCM) #numbering scheme that corresponds to breakout board and pin layout
GPIO.setup(pinNum,GPIO.OUT) #replace pinNum with whatever pin you used, this sets up that pin as an output
#set LED to flash forever
while True:

Your LED should now be flashing: success! You can now control your LED from Python. You can stop the flashing by pressing Ctrl-C.

Now let’s write a program to translate your input to LED Morse code.

Here’s a dictionary to help you out:

CODE = {' ': ' ', 
        "'": '.----.', 
        '(': '-.--.-', 
        ')': '-.--.-', 
        ',': '--..--', 
        '-': '-....-', 
        '.': '.-.-.-', 
        '/': '-..-.', 
        '0': '-----', 
        '1': '.----', 
        '2': '..---', 
        '3': '...--', 
        '4': '....-', 
        '5': '.....', 
        '6': '-....', 
        '7': '--...', 
        '8': '---..', 
        '9': '----.', 
        ':': '---...', 
        ';': '-.-.-.', 
        '?': '..--..', 
        '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': '--..', 
        '_': '..--.-'}

A few helpful tips:

    for letter in string:

Good Luck!

Try out your code and see if you can decode the message again.

An example of a finished program can be found here.


Now that you know how to operate a single LED, try having a group of them. You could arrange that they all flash together to form a particular pattern or sequence. Perhaps you could make a seven-segment display.