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:
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.
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.
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
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
from morse_coded_letters import letters
Now set the pin number that will send the signal to the led.
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.
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(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 ´/´.
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
and you have the code for the handler:
If you want see the complete source code see my GIST in Github.