Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 16, 2020 02:41 am GMT

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, an inventor of the telegraph.

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 translate those points and lines as a binary code, being:

- = 1
= 0

If you see the P you can see a code like this - - but translated to binary you can see this code 0 1 1 0.
If you understand this convention we can create a python file called morse_conventions.py with the dictionary with all conventions.

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 GPIOimport 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:

from morse_conventions import letters

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

PIN = 7GPIO.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.

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

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)

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(3)    bin_letter = letters[letter]    print(bin_letter)    for number in bin_letter:        if number == 1:            led_control(0.5)        elif number == 0:            led_control(0.2)        else:            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 1 the led will keep ON 0.5 sec's and 0.2 sec's for each 0.
NOTE: The final else has the mission to handle space between letter and letter, if you see at the dict we have this space:

(" " : 2)

But you can handle this with an exception or with another way

GPIO.cleanup()

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

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


Original Link: https://dev.to/halcolo/morse-code-in-raspberry-pi-1h9b

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To