- Translator
- Raspberry Pi
Morse code with Raspberry Pi
On a Raspberry Pi, GPIO Zero's LED class sends Morse by switching a pin on and off for the standard lengths, and its Button class reads a key: time each press, call it a dot or a dash, and decode after a long enough silence. This guide has both scripts and the wiring.
In short: with the GPIO Zero library, an LED on one pin sends Morse by switching on and off for the standard lengths, and a Button on another pin reads a key: time each press, call it a dot or a dash, and decode when the silence is long enough.
Wire an LED and a key
GPIO Zero describes the LED circuit like this: the cathode, the short leg, goes to a ground pin; the anode, the long leg, goes to a limiting resistor, and the other side of the resistor goes to a GPIO pin (GPIO Zero, Output Devices). A button needs only two wires: one side to a ground pin and the other to any GPIO pin (GPIO Zero, Input Devices). A real Morse key is a switch, so it fits the same way; Morse code keys shows the kinds. Any push button will do for practice.
| Part | Connects to | Documented by |
|---|---|---|
| LED, cathode (short leg) | A ground pin | (GPIO Zero, Output Devices) |
| LED, anode (long leg) | A current-limiting resistor, whose other side goes to GPIO17 | (GPIO Zero, Output Devices) |
| Key or push button, one side | GPIO4 | (GPIO Zero, Input Devices) |
| Key or push button, other side | A ground pin | (GPIO Zero, Input Devices) |
GPIO Zero numbers pins the Broadcom way, so LED(17) means GPIO17, not physical pin 17: its docs give pin 11 as the physical pin for GPIO17 (GPIO Zero, Basic Recipes). On Raspberry Pi OS, the pinout command shows a map of physical positions, GPIO numbers and pin functions (Raspberry Pi Documentation).
Check the limits before you connect
This project is low voltage, and the key needs no power of its own: with GPIO Zero's default, the pin is pulled high inside the Pi and the key only connects it to ground (GPIO Zero, Input Devices). Keep it that way. Do not connect anything here to mains. Raspberry Pi's documentation lists the limits that matter:
- GPIO pins operate at 3.3 V and are not 5 V tolerant, and applying 5 V directly to a GPIO pin can cause permanent damage (Raspberry Pi Documentation).
- A single GPIO pin can supply up to 16 mA, and all pins together up to 50 mA (Raspberry Pi Documentation).
- For LEDs, use a suitable current-limiting resistor (Raspberry Pi Documentation).
Send: blink the LED
This script flashes HI PI on the LED. It reuses morse.py from Morse code in Python: put that file in the same folder. Its timing() function returns pairs of on-or-off and milliseconds, which fit GPIO Zero's led.on() and led.off(), and sleep() from Python's time module pauses for the given seconds (Python documentation, time).
from time import sleep
from gpiozero import LED
from morse import encode, timing
led = LED(17)
for is_on, ms in timing(encode("HI PI")):
if is_on:
led.on()
else:
led.off()
sleep(ms / 1000)
led.off()
Run it with python3. The script reuses the timing from the Python guide: at 12 words per minute a dot lasts 100 ms (ARRL, QEX, April 1990), and dashes and gaps keep the ITU proportions (ITU-R M.1677-1). The end of the script switches the LED off; GPIO Zero notes that when a script ends, the GPIOs may be reset (GPIO Zero, Basic Recipes).
Receive: read a key with a button
Reading Morse is mostly measuring. GPIO Zero's Button can pause the script until it is pressed or released, and an optional timeout limits the wait (GPIO Zero, Input Devices). That is enough: measure how long each press lasts, decide dot or dash, and treat a long silence as the end of a letter. Python's monotonic() clock is for exactly this, because only the difference between two readings is valid (Python documentation, time).
from time import monotonic
from gpiozero import Button
from morse import decode
WPM = 12
UNIT = 1.2 / WPM # one unit in seconds
DASH_FROM = 2 * UNIT # a press this long or longer is a dash
button = Button(4) # key between GPIO4 and a ground pin
button.wait_for_press()
while True:
letter = ""
while True:
start = monotonic()
button.wait_for_release()
letter += "-" if monotonic() - start >= DASH_FROM else "."
released = monotonic()
button.wait_for_press(timeout=2 * UNIT)
if not button.is_pressed:
break
print(decode(letter), end="", flush=True)
remaining = max(0, 5 * UNIT - (monotonic() - released))
button.wait_for_press(timeout=remaining)
if not button.is_pressed:
print(" ", end="", flush=True)
button.wait_for_press()
Run it, press the key, and the letters appear in the terminal. The word timeout is measured from the last key release, so printing a letter does not extend that threshold. The thresholds below are our choices, not a standard: each boundary sits halfway between two of the standard lengths: 2 units between a dot and a dash, 2 between a gap inside a letter and a gap between letters, 5 between a letter gap and a word gap.
| What happens | Length | Result |
|---|---|---|
| A press shorter than 200 ms | About 100 ms | A dot |
| A press of 200 ms or longer | About 300 ms | A dash |
| Silence of 200 ms or longer after a release | About 300 ms between letters | The letter ends |
| Silence of 500 ms or longer in all | About 700 ms between words | A space is printed |
Taps are rarely exact, which is why the script compares against a boundary and not against a single length. Your first letters may come out wrong. Slow down by lowering WPM for generous margins, and check your rhythm against How to tap Morse code and Morse code timing. A noisy button can register extra presses: the Button constructor has a bounce_time parameter that makes it ignore changes for that many seconds after the first (GPIO Zero, Input Devices).
Where it goes next
The software side lives in Morse code in Python, and a microcontroller version with an LED and a buzzer is in Morse code with Arduino. For the Morse code itself, the alphabet is the cheat sheet and the translator plays any text so you can compare it with what your Pi sends.
Sources
- Recommendation ITU-R M.1677-1, International Morse code. ITU Radiocommunication Sector, October 2009.
- A Standard for Morse Timing Using the Farnsworth Technique, Jon Bloom, KE3Z. ARRL, QEX, April 1990.
- API - Output Devices, GPIO Zero documentation. GPIO Zero, checked 7 November 2026.
- API - Input Devices, GPIO Zero documentation. GPIO Zero, checked 7 November 2026.
- Basic Recipes, GPIO Zero documentation. GPIO Zero, checked 7 November 2026.
- Raspberry Pi hardware, 40-pin GPIO header. Raspberry Pi Documentation, checked 7 November 2026.
- time: Time access and conversions, The Python Standard Library. Python Software Foundation, docs.python.org, checked 7 November 2026.