Code Explanation
This project requires two code files, boot.py and code.py.
boot.py
Inside boot.py the USB microphone USB endpoint is enabled and audio settings are configured.
# SPDX-FileCopyrightText: 2026 Tim Cocks for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import usb_audio
usb_audio.enable(
sample_rate=16000, channel_count=1, bits_per_sample=16, microphone=True
)
Configuration
The first section contains a handful of configuration variables. The pre-loaded message strings BTN_A_MESSAGE and BTN_B_MESSAGE are used for the built-in A and B button hotkey messages, change them to use customized messages.
UNIT_SECONDS is the length in seconds of a single unit in the Morse playback. In standard Morse code, the dot beep lasts one unit of time. Increase the number to make the automatic messages play slower. This value has no effect on live keyed Morse code.
The remaining duration and gap variables are all set relative to UNIT_SECONDS. They use multiples of UNIT_SECONDS based on the standards of International Morse code. Typically you don't want to change them directly, instead just change UNIT_SECONDS. But, you can experiment with different values for them if you don't mind breaking from the protocol specification.
This section contains a Python dictionary that maps letters, numbers, and characters to their Morse code counterparts with dot and dash syntax that uses period and hyphen characters. The same mapping that can be found in Morse code tables.
The dot and dash representation string is then used with synthio to play the appropriate length tone.
Setup
The setup section initializes synthio and usb_microphone and sets synthio to play into the microphone audio stream. Two helper functions are defined:
-
play_tone()plays a tone for a specified duration and then stops -
play_morse()accepts a string of text, automatically converts it to Morse code, and plays it into the USB audio stream.
The buttons are initialized using the keypad module. One instance is of keypad.Keys is used for the built-in A and B buttons, and another instance for the Morse key IO pin.
Page last edited June 29, 2026
Text editor powered by tinymce.