DEV Community

Cover image for Morse code in Raspberry PI
Halcolo
Halcolo

Posted on

Morse code in Raspberry PI

Morse code in Raspberry PI

I know this isn't the most awesome project in the RPI history, but this project is targeted for all the people how wants to start using their RPI in programming electronic components from scratch easily with their favorite language, you can transcript this project to another programming language but here we will use Python as a comfortable option some samples for the controllers GPIO for RPIO in the elinux wiki:

https://elinux.org/RPi_GPIO_Code_Samples

What you will need?

  • A Raspberry Pi with SD card and peripherals or ssh/serial connection(optional).
  • A led.
  • A protoboard.
  • 220Ω Resistor.

Before anything, if you don't know what is morse code I will simplify the explanation as an encoded language used for the military forces principally to communicate messages for the aliases in the war.

A little example of a "hello world" in morse code.

For more info and the table with the letters and numbers encoded look this post in wikipedia.

Morse code is a method used in telecommunication to encode text characters as standardized sequences of two different signal durations, called dots and dashes, or dits and dahs. Morse code is named after Samuel Morse, one of the early developers of the system adopted for electrical telegraphy.

First of all, we will prepare our RPI connection,

I prefer use my RPI connected via SSH or serial AND USE the neo vim editor in my shell, this is optional you can connect an HDMI and a keyboard but if you prefer this option like me, for the SSH connection you can see this post from RPI blog, or see this guide for the Serial connection.

After that we will set our cables, you can copy this schema.

Alt Text

in my case I used an RPI 4B but you can copy this connection in an RPI 3 or search the same configuration in another model with the following schema.

Alt Text

Now we will register those points and lines as a morse dictionary like this:

After that we will create another python file, call it as you prefer, in my case, I will call it main.py, and the first lines have the import of two libraries, the first is so important.

import RPi.GPIO as GPIO
import time
Enter fullscreen mode Exit fullscreen mode

RPI.GPIO is the controller of the pins of the Raspberry Pi.
time is a controller to keep our LED in the state on or off.
Now we will import the dictionary from the morse conventions.py file:

from morse_coded_letters import letters
Enter fullscreen mode Exit fullscreen mode

Now set the pin number that will send the signal to the led.

PIN = 7

GPIO.setmode(GPIO.BOARD)
GPIO.setup(PIN, GPIO.OUT)

Enter fullscreen mode Exit fullscreen mode

The GPIO.setmode allow us to call the pins in Board mode, you can set this in BCM mode (Basically a code for each pin) or board mode (basically a number for each pin), please see more information in the following URL.

Note: if you follow the schema and prefer use the mode BSM change the pin constant for the number 4 if you follow the above schema.

word = str(input("Write a word: "))
word = list(word.upper())
Enter fullscreen mode Exit fullscreen mode

After that, we will get a word and transform it into a list of characters.

def led_control(time_sleep):
    GPIO.output(PIN, GPIO.HIGH)
    time.sleep(time_sleep)
    GPIO.output(PIN, GPIO.LOW)
    time.sleep(0.5)
Enter fullscreen mode Exit fullscreen mode

We will define a function to handle our led and the time and it will receive a number, this will be a time in seconds that the led will wait ON.

for letter in word:
    time.sleep(2)
    bin_letter = letters[letter]
    print(bin_letter)
    for codes in bin_letter:
        if codes == "-":
            led_control(0.5)
        elif codes == ".":
            led_control(0.2)
        elif codes == "/":
            time.sleep(1)
Enter fullscreen mode Exit fullscreen mode

Now, this is the core function of our microprogram.

First of all, browse all positions in our list word then search the letter in the dictionary and identify the binary convention, for each ´-´ the led will keep ON 0.5 sec's and 0.2 sec's for each point´.´.
**NOTE: We will handle the space with a ´/´.

GPIO.cleanup()

Enter fullscreen mode Exit fullscreen mode

Finally, close the connection with cleanup for turn OFF the led.

NOTE: If you prefer a binary code e.g. For a P you can see a code like this • - - • but translated to binary you can see this code 0 1 1 0.

- = 1
= 0

If you understand this convention we can create a python file called morse_binary_conventions.py with the dictionary with all conventions.
For more examples see this gist

https://gist.github.com/halcolo/eafa6b8574bfda953bcc307fbe33060c#file-morse_binary_conventions-py
and you have the code for the handler:
https://gist.github.com/halcolo/eafa6b8574bfda953bcc307fbe33060c#file-morse_code_binary_handler-py

If you want see the complete source code see my GIST in Github.

Top comments (4)

Collapse
 
ghengki profile image
ghengki

how do you run those 3 codes in raspberry pi 4? do I have to run one by one? thanks

Collapse
 
vlasales profile image
Vlastimil Pospichal

I prefer this form of dictionary:

letters = {
    "A" : ".-",
    "B" : "-...",
...
Enter fullscreen mode Exit fullscreen mode
Collapse
 
halcolo profile image
Halcolo • Edited

Nice, that's better, I will implement this way to. Thank you!

Collapse
 
daviddalbusco profile image
David Dal Busco

As a former scout, I've just one word to say: -.-. / --- / --- / .-..