Morse code in Raspberry PI
Tags: raspberrypi, python, electronics, hardware
Published: 2020-08-16 02:41:18

---
title: Morse code in Raspberry PI
published: True
description: A little tutorial to learn how to translate the human language to morse code using raspberry pi and a led
tags: RaspberryPi, python, electronics, hardware
cover_image: https://cdn.hswstatic.com/gif/morse-code.jpg
---

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

{% youtube L6gxfX4GrbI %}

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

{% wikipedia https://en.wikipedia.org/wiki/Morse_code %}

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](https://www.raspberrypi.org/documentation/remote-access/ssh/) from RPI blog, or see [this guide](https://cdn-learn.adafruit.com/downloads/pdf/adafruits-raspberry-pi-lesson-5-using-a-console-cable.pdf) for the Serial connection.

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/14gne175gsxiz8vs87mc.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/yobv9m3gire19karboi3.png)

Now we will register those points and lines as a morse dictionary like this:
{% gist https://gist.github.com/halcolo/eafa6b8574bfda953bcc307fbe33060c file=morse_coded_letters.py %}

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.

```python
import RPi.GPIO as GPIO
import time
```
> 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:

```ptyhon
from morse_coded_letters import letters
```

Now set the pin number that will send the signal to the led.
```python
PIN = 7

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

```
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](https://raspi.tv/2013/rpi-gpio-basics-4-setting-up-rpi-gpio-numbering-systems-and-inputs).

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

```python
word = str(input("Write a word: "))
word = list(word.upper())
```
After that, we will get a word and transform it into a list of characters.

```python
def led_control(time_sleep):
    GPIO.output(PIN, GPIO.HIGH)
    time.sleep(time_sleep)
    GPIO.output(PIN, GPIO.LOW)
    time.sleep(0.5)
```
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.

```python
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)
```
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 ´/´.

```python
GPIO.cleanup()

```
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](https://gist.github.com/halcolo/eafa6b8574bfda953bcc307fbe33060c).