Build a Raspberry Pi Pico Morse code trainer that plays any text you type as dots and dashes on an LED and buzzer, and decodes the letters you tap on a push button key.
Need it ready-made? Buy Ready Project
This Raspberry Pi Pico Morse code trainer is a small, fun project that helps you learn Morse code by ear and by hand. In Send mode you type any word in the Thonny shell and the Pico plays it as dots and dashes on an LED and a buzzer. In Key mode you tap a push button like a telegraph key, and the Pico decodes your taps and prints the letters on your screen, so you can see straight away if your timing is right.
It needs only a Pico, one button, one LED, one buzzer and a resistor, so it is a great first MicroPython project. You will learn about digital input with pull-ups, debouncing, measuring press time with ticks_ms(), and reading text from the USB serial port without blocking the program.

How It Works
- Morse timing: everything is measured in units. A dot is 1 unit, a dash is 3 units, the gap inside a letter is 1 unit, between letters 3 units and between words 7 units. At 12 words per minute one unit is 100 ms.
- Send mode: the Pico checks the USB serial port with
selectso the loop never freezes. When you press Enter, each character is looked up in a dictionary of letters, numbers and common punctuation, and the LED and buzzer are switched on for each dot or dash. - Key mode: the button is on GP15 with the internal pull-up, so it reads 0 when pressed. The code measures how long you hold it: under 250 ms is a dot, longer is a dash. The buzzer and LED sound while you press, like a real sidetone.
- Decoding: when the button has been quiet for 600 ms the collected dots and dashes are looked up in a reverse dictionary and the letter is printed. After 1.5 seconds of silence a space is added between words. An unknown pattern prints a question mark.
- Switching modes: hold the button for 3 seconds. Three short beeps mean Send mode, two beeps mean Key mode.
Components Required
- Raspberry Pi Pico (or Pico W) with headers – 1
- Tactile push button 12 mm (or a real Morse key) – 1
- Active buzzer, 3.3V–5V type – 1
- Red 5 mm LED – 1
- 220 Ω resistor – 1
- Half-size breadboard – 1
- Male-to-male jumper wires – 6
- Micro-USB data cable – 1
Wiring Table
| Part / pin | Pico pin | Notes |
|---|---|---|
| Push button leg 1 | GP15 (pin 20) | Internal pull-up, no resistor needed |
| Push button leg 2 | GND (pin 18) | Use legs on opposite sides of the button |
| 220 Ω resistor | GP14 (pin 19) | Other end to the LED long leg (anode) |
| LED short leg (cathode) | GND | |
| Active buzzer + | GP13 (pin 17) | 3.3 V buzzer; a 5 V buzzer needs a BC547 driver |
| Active buzzer – | GND | |
| Micro-USB | Computer | Power and serial (Thonny shell) |
Connection Diagram
The illustration above shows the parts on the breadboard. It is a layout guide only, so follow the pin-accurate drawing below, which matches the wiring table.
Code
Flash the latest MicroPython UF2 for your board (hold BOOTSEL, plug in the Pico and drop the file on the RPI-RP2 drive). Open Thonny, select MicroPython (Raspberry Pi Pico), paste the code and save it to the Pico as main.py so it starts on power-up. No extra libraries are needed. On a Pico W change the onboard LED line to Pin("LED", Pin.OUT).
# Raspberry Pi Pico Morse Code Trainer (MicroPython)
# Arduino Project Hub - ardiunoprojecthub.online
#
# Two modes in one sketch:
# 1) SEND mode : type a word in the Thonny shell, the Pico plays it in
# Morse on the LED and buzzer.
# 2) KEY mode : tap the push button like a Morse key. Short press = dot,
# long press = dash. The Pico decodes your letters and
# prints them in the shell.
# Hold the button for 3 seconds at any time to switch modes.
#
# Wiring:
# GP15 -> push button -> GND (internal pull-up, pressed = 0)
# GP14 -> 220 ohm -> LED anode, LED cathode -> GND
# GP13 -> active buzzer +, buzzer - -> GND (3.3 V type, or via BC547)
# Onboard LED (GP25) blinks once at boot.
from machine import Pin
import time
import sys
import select
BUTTON = Pin(15, Pin.IN, Pin.PULL_UP)
LED = Pin(14, Pin.OUT)
BUZZER = Pin(13, Pin.OUT)
BOARD_LED = Pin(25, Pin.OUT) # use Pin("LED", Pin.OUT) on a Pico W
WPM = 12 # words per minute for SEND mode
UNIT = 1200 // WPM # length of one dot in milliseconds
MORSE = {
'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.',
'F': '..-.', 'G': '--.', 'H': '....', 'I': '..', 'J': '.---',
'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---',
'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-',
'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--',
'Z': '--..',
'0': '-----', '1': '.----', '2': '..---', '3': '...--',
'4': '....-', '5': '.....', '6': '-....', '7': '--...',
'8': '---..', '9': '----.',
'.': '.-.-.-', ',': '--..--', '?': '..--..', '/': '-..-.',
'=': '-...-', '@': '.--.-.'
}
DECODE = {v: k for k, v in MORSE.items()}
def tone(on):
LED.value(on)
BUZZER.value(on)
def play_symbol(sym):
tone(1)
time.sleep_ms(UNIT if sym == '.' else UNIT * 3)
tone(0)
time.sleep_ms(UNIT) # gap inside a letter
def play_text(text):
print("Playing:", text)
for ch in text.upper():
if ch == ' ':
time.sleep_ms(UNIT * 4) # word gap = 7 units in total
continue
code = MORSE.get(ch)
if code is None:
continue # skip characters we cannot send
print(ch, code)
for sym in code:
play_symbol(sym)
time.sleep_ms(UNIT * 2) # letter gap = 3 units in total
print("Done.\n")
def read_line_nonblocking(buf):
# Returns (line or None, buffer). Works in the Thonny / USB shell.
while select.select([sys.stdin], [], [], 0)[0]:
ch = sys.stdin.read(1)
if ch in ('\n', '\r'):
if buf:
return buf, ''
else:
buf += ch
return None, buf
def beep(times, ms=60):
for _ in range(times):
tone(1)
time.sleep_ms(ms)
tone(0)
time.sleep_ms(ms)
# Timing for KEY mode (milliseconds). Tune these to your own speed.
DOT_MAX = 250 # press shorter than this = dot, longer = dash
LETTER_GAP = 600 # silence longer than this ends a letter
WORD_GAP = 1500 # silence longer than this adds a space
MODE_HOLD = 3000 # hold this long to switch modes
mode = 'KEY'
BOARD_LED.value(1)
time.sleep_ms(300)
BOARD_LED.value(0)
print("Pico Morse Code Trainer ready.")
print("KEY mode: tap the button. Hold 3 s to switch to SEND mode.")
symbols = ''
last_release = time.ticks_ms()
space_added = True
line_buf = ''
while True:
# ----- button handling (both modes) -----
if BUTTON.value() == 0:
time.sleep_ms(15) # debounce
if BUTTON.value() == 0:
start = time.ticks_ms()
if mode == 'KEY':
tone(1) # sidetone while keying
while BUTTON.value() == 0:
if time.ticks_diff(time.ticks_ms(), start) > MODE_HOLD:
break
held = time.ticks_diff(time.ticks_ms(), start)
tone(0)
if held > MODE_HOLD:
mode = 'SEND' if mode == 'KEY' else 'KEY'
symbols = ''
beep(3 if mode == 'SEND' else 2)
print("\n== Switched to", mode, "mode ==")
if mode == 'SEND':
print("Type a word and press Enter.")
while BUTTON.value() == 0: # wait for release
time.sleep_ms(10)
elif mode == 'KEY':
symbols += '.' if held < DOT_MAX else '-'
space_added = False
last_release = time.ticks_ms()
# ----- KEY mode: decode after gaps -----
if mode == 'KEY':
quiet = time.ticks_diff(time.ticks_ms(), last_release)
if symbols and quiet > LETTER_GAP:
letter = DECODE.get(symbols, '?')
print(letter, end='')
symbols = ''
elif not symbols and not space_added and quiet > WORD_GAP:
print(' ', end='')
space_added = True
# ----- SEND mode: read text from the shell -----
else:
line, line_buf = read_line_nonblocking(line_buf)
if line:
play_text(line.strip())
print("Type another word and press Enter.")
time.sleep_ms(5)
Testing
- Run the script in Thonny. The onboard LED blinks once and the shell prints Pico Morse Code Trainer ready.
- In Key mode tap three short, three long, three short. The shell should print
SOS. - Hold the button for 3 seconds until you hear three beeps, then type
HELLOin the shell and press Enter. The LED and buzzer play the word and each letter’s code is printed. - Change
WPMto 5 for slow practice or 20 once you get faster.
Troubleshooting
| Problem | Fix |
|---|---|
| Every press prints E or T only | Each press is being decoded as a full letter. Tap faster or raise LETTER_GAP to 800 ms. |
| Dashes are read as dots | Raise DOT_MAX to 300 ms, or hold dashes a little longer. |
| Button does nothing | Use two legs on opposite sides of the button; legs on the same side are always connected. Check it goes to GP15 and GND. |
| Buzzer is silent but LED works | You may have a passive buzzer, which needs a tone. Use an active buzzer, or check its + leg goes to GP13. |
| Typing in the shell does nothing | You are still in Key mode. Hold the button 3 seconds until three beeps, and click inside the Thonny shell before typing. |
| Nothing happens after unplugging from the computer | Save the file to the Pico as main.py. Send mode needs the USB shell, but Key mode still sounds without it. |
Ideas to Extend
- Add a 0.96 inch I2C OLED to show the decoded text without a computer.
- Add a quiz mode that plays a random letter and waits for you to key it back.
- Use a Pico W to send your decoded messages to a phone over Wi-Fi.
1 thought on “Raspberry Pi Pico Morse Code Trainer with Buzzer & Button (MicroPython)”