Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
4.1.11 Morse Code Generator¶
Introduction¶
In this project, we’ll make a Morse code generator, where you type in a series of English letters in the Raspberry Pi to make it appear as Morse code.
Required Components¶
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Raphael Kit |
337 |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
---|---|
- |
|
Schematic Diagram¶
T-Board Name |
physical |
wiringPi |
BCM |
GPIO17 |
Pin 11 |
0 |
17 |
GPIO22 |
Pin 15 |
3 |
22 |
Experimental Procedures¶
Step 1: Build the circuit. (Pay attention to poles of the buzzer: The one with + label is the positive pole and the other is the negative.)
Step 2: Open the code file.
cd ~/raphael-kit/python-pi5
Step 3: Run.
sudo python3 4.1.16_MorseCodeGenerator_zero.py
After the program runs, type a series of characters, and the buzzer and the LED will send the corresponding Morse code signals.
Code
#!/usr/bin/env python3
from gpiozero import Buzzer, LED
import time
# Initialize Buzzer and LED to GPIO pins
BeepPin = Buzzer(22)
ALedPin = LED(17)
# Morse code representation for characters
MORSECODE = {
'A': '01', 'B': '1000', 'C': '1010', 'D': '100', 'E': '0', 'F': '0010', 'G': '110',
'H': '0000', 'I': '00', 'J': '0111', 'K': '101', 'L': '0100', 'M': '11', 'N': '10',
'O': '111', 'P': '0110', 'Q': '1101', 'R': '010', 'S': '000', 'T': '1',
'U': '001', 'V': '0001', 'W': '011', 'X': '1001', 'Y': '1011', 'Z': '1100',
'1': '01111', '2': '00111', '3': '00011', '4': '00001', '5': '00000',
'6': '10000', '7': '11000', '8': '11100', '9': '11110', '0': '11111',
'?': '001100', '/': '10010', ',': '110011', '.': '010101', ';': '101010',
'!': '101011', '@': '011010', ':': '111000',
}
def on():
""" Turn on the buzzer and LED. """
BeepPin.on()
ALedPin.on()
def off():
""" Turn off the buzzer and LED. """
BeepPin.off()
ALedPin.off()
def beep(dt): # dt for delay time.
"""
Produce a beep sound and LED flash for the specified duration.
:param dt: Duration for the beep and flash.
"""
on()
time.sleep(dt)
off()
time.sleep(dt)
def morsecode(code):
"""
Convert the input code into Morse code and signal it using the buzzer and LED.
:param code: The text to be converted to Morse code.
"""
pause = 0.25
for letter in code:
for tap in MORSECODE[letter]:
if tap == '0':
beep(pause / 2) # Short beep for dot
if tap == '1':
beep(pause) # Long beep for dash
time.sleep(pause) # Pause between letters
def destroy():
""" Clean up resources on script termination. """
print("")
BeepPin.off()
ALedPin.off()
try:
while True:
code = input("Please input the messenger:")
code = code.upper() # Convert to uppercase for Morse code lookup
print(code)
morsecode(code)
except KeyboardInterrupt:
destroy()
Code Explanation
This code imports the Buzzer and LED classes from the gpiozero library. These classes are essential for controlling the corresponding GPIO devices on the Raspberry Pi.
#!/usr/bin/env python3 from gpiozero import Buzzer, LED import time
Initializes the buzzer on GPIO pin 22 and the LED on GPIO pin 17, facilitating the control of these components.
# Initialize Buzzer and LED to GPIO pins BeepPin = Buzzer(22) ALedPin = LED(17)
Defines the
MORSE
structure, a dictionary containing Morse code representations for characters A-Z, numbers 0-9, and symbols like “?”, “/”, “:”, “,”, “.”, “;”, “!”, “@”, with0
signifying a dot and1
indicating a dash.# Morse code representation for characters MORSECODE = { 'A': '01', 'B': '1000', 'C': '1010', 'D': '100', 'E': '0', 'F': '0010', 'G': '110', 'H': '0000', 'I': '00', 'J': '0111', 'K': '101', 'L': '0100', 'M': '11', 'N': '10', 'O': '111', 'P': '0110', 'Q': '1101', 'R': '010', 'S': '000', 'T': '1', 'U': '001', 'V': '0001', 'W': '011', 'X': '1001', 'Y': '1011', 'Z': '1100', '1': '01111', '2': '00111', '3': '00011', '4': '00001', '5': '00000', '6': '10000', '7': '11000', '8': '11100', '9': '11110', '0': '11111', '?': '001100', '/': '10010', ',': '110011', '.': '010101', ';': '101010', '!': '101011', '@': '011010', ':': '111000', }
The function
on()
starts the buzzer and the LED. The functionoff()
is used to turn off the buzzer and the LED.def on(): """ Turn on the buzzer and LED. """ BeepPin.on() ALedPin.on() def off(): """ Turn off the buzzer and LED. """ BeepPin.off() ALedPin.off()
Define a function
beep()
to make the buzzer and the LED emit sounds and blink in a certain interval ofdt
.def beep(dt): # dt for delay time. """ Produce a beep sound and LED flash for the specified duration. :param dt: Duration for the beep and flash. """ on() time.sleep(dt) off() time.sleep(dt)
The function
morsecode()
is used to process the Morse code of input characters by making the “1” of the code keep emitting sounds or lights and the “0”shortly emit sounds or lights, ex., input “SOS”, and there will be a signal containing three short three long and then three short segments “ · · · - - - · · · ”.def morsecode(code): """ Convert the input code into Morse code and signal it using the buzzer and LED. :param code: The text to be converted to Morse code. """ pause = 0.25 for letter in code: for tap in MORSECODE[letter]: if tap == '0': beep(pause / 2) # Short beep for dot if tap == '1': beep(pause) # Long beep for dash time.sleep(pause) # Pause between letters
Defines a function named
destroy
that turns off both the buzzer and the LED. This function is intended to be called when the script is terminated to ensure that the GPIO pins are not left in an active state.def destroy(): """ Clean up resources on script termination. """ print("") BeepPin.off() ALedPin.off()
When you type the relevant characters with the keyboard,
upper()
will convert the input letters to their capital form.printf()
then prints the clear text on the computer screen, and themorsecod()
function causes the buzzer and the LED to emit Morse code.try: while True: code = input("Please input the messenger:") code = code.upper() # Convert to uppercase for Morse code lookup print(code) morsecode(code) except KeyboardInterrupt: destroy()