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.

Morse Code on an LED

INSTRUCTIONS:

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.)

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!

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

Morse Code on an LED BoardNow :

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:
  GPIO.output(pinNum,GPIO.HIGH)
  time.sleep(0.5)
  GPIO.output(pinNum,GPIO.LOW)
  time.sleep(0.5)	    

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:

  • CODE['Z'] will return '--..', the Morse code for Z
  • There are only upper case characters, 'z'.upper() will return 'Z'
  • A dot should correspond to the LED being on for about half the time of a dash
  • You will want a pause between each character and for a space
  • You can get input from a user with raw_input('this is the prompt to be displayed ')
  • You can loop through a string one character at a time with
    for letter in string:
      print(letter)

Good Luck!

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

Source: Morse Code on an LED


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top