# USB Morse Code Key with CircuitPython

## Overview

https://www.youtube.com/watch?v=8-Khb60jxns

The [TinyUSB](https://github.com/hathach/tinyusb) library underpins many of CircuitPython's USB capabilities. It's what lets CircuitPython act as a storage drive, CDC, HID, MIDI and more. Starting with&nbsp;[version 10.3.0-alpha.3](https://github.com/adafruit/circuitpython/releases/tag/10.3.0-alpha.3) CircuitPython has added support for USB audio types. This guide demonstrates how to use the USB microphone capability to generate tones and output them to a computer or other digital audio equipment. You can connect a basic key to tap in Morse code live, and use the generator function to automatically convert strings into beeps and boops.

## Parts
### Morse Code Key with 3.5mm Jack

Morse keys come in different shapes and styles, but all are electronically similar. They are a basic momentary switch that closes a circuit when you press the key, and opens the circuit when you release the key. They can be found easily in online shops and radio equipment providers, or DIY'd with a 3D printer, spring, and conductive contacts. I used [this one](https://www.amazon.com/dp/B0C4P5YVF5).&nbsp;

![Blue aluminum Morse code key with a 3.5mm jack connection](https://cdn-learn.adafruit.com/assets/assets/000/144/628/medium640/circuitpython_morse_key.png?1782397217)

Featured
### Circuit Playground Bluefruit - Bluetooth® Low Energy

[Circuit Playground Bluefruit - Bluetooth® Low Energy](https://www.adafruit.com/product/4333)
 **Circuit Playground Bluefruit** is our third board in the Circuit Playground series, another step towards a perfect introduction to electronics and programming. We've taken the popular Circuit Playground Express and made it even better! Now the main chip is an nRF52840...

Out of Stock
[Buy Now](https://www.adafruit.com/product/4333)
[Related Guides to the Product](https://learn.adafruit.com/products/4333/guides)
![shot of a Black woman's neon-green manicured hand holding up a Circuit Playground Bluefruit glowing rainbow LEDs.](https://cdn-shop.adafruit.com/640x480/4333-11.jpg)

Featured
### 3.5mm Mono Audio Plug to Alligator Clips

[3.5mm Mono Audio Plug to Alligator Clips](https://www.adafruit.com/product/4181)
This cable is easy to understand - you get a 3.5mm mono audio plug on one end, and two alligator clips on the other. It's also really easy to use, perfect for clipping onto a micro:bit or Circuit Playground! It's also good for use with AT (accessibility technology) buttons as they use...

In Stock
[Buy Now](https://www.adafruit.com/product/4181)
[Related Guides to the Product](https://learn.adafruit.com/products/4181/guides)
![3.5mm Mono Audio Plug to Alligator Clips with white and black clips](https://cdn-shop.adafruit.com/640x480/4181-02.jpg)

Featured
### USB cable - USB A to Micro-B

[USB cable - USB A to Micro-B](https://www.adafruit.com/product/592)
This here is your standard A to micro-B USB cable, for USB 1.1 or 2.0. Perfect for connecting a PC to your Metro, Feather, Raspberry Pi or other dev-board or microcontroller

Approximately 3 feet / 1 meter long

Out of Stock
[Buy Now](https://www.adafruit.com/product/592)
[Related Guides to the Product](https://learn.adafruit.com/products/592/guides)
![USB cable - USB A to Micro-B - 3 foot long](https://cdn-shop.adafruit.com/640x480/592-01.jpg)

# USB Morse Code Key with CircuitPython

## Install CircuitPython

# Install or Update CircuitPython

Follow this quick step-by-step to install or update CircuitPython on your Circuit Playground Bluefruit.

Warning: The `usb_audio` module was added to CircuitPython in version `10.3.0-alpha.3`. Running this project requires using the development release of CircuitPython until there is a 10.3.0 stable release. On the downloads page scroll down to the latest development release panel.

[Download the latest version of CircuitPython for this board via circuitpython.org](https://circuitpython.org/board/circuitplayground_bluefruit/)
 **Click the link above and download the latest UF2 file**

Download and save it to your Desktop (or wherever is handy)

![adafruit_products_CPB_Download_UF2.png](https://cdn-learn.adafruit.com/assets/assets/000/080/530/medium640/adafruit_products_CPB_Download_UF2.png?1567715178)

Plug your Circuit Playground Bluefruit into your computer using a known-good data-capable USB cable.

**A lot of people end up using charge-only USB cables and it is very frustrating! So make sure you have a USB cable you know is good for data sync.**

Double-click the small **Reset** button in the middle of the CPB (indicated by the red arrow in the image). The ten NeoPixel LEDs will all turn red, and then will all turn green. If they turn all red and stay red, check the USB cable, try another USB port, etc. The little red LED next to the USB connector will pulse red - this is ok!

If double-clicking doesn't work the first time, try again. Sometimes it can take a few tries to get the rhythm right!

(If double-clicking doesn't do it, try a single-click!)

![adafruit_products_CPB_Front_Reset_Button_Arrow.jpg](https://cdn-learn.adafruit.com/assets/assets/000/080/532/medium640/adafruit_products_CPB_Front_Reset_Button_Arrow.jpg?1567715535)

You will see a new disk drive appear called **CPLAYBTBOOT**.

&nbsp;

&nbsp;

&nbsp;

Drag the **adafruit\_circuitpython\_etc.uf2** file to **CPLAYBTBOOT.**

![adafruit_products_CPB_CPLAYBTBOOT.png](https://cdn-learn.adafruit.com/assets/assets/000/080/533/medium640/adafruit_products_CPB_CPLAYBTBOOT.png?1567715858)

![adafruit_products_CBP_drag_UF2.png](https://cdn-learn.adafruit.com/assets/assets/000/080/534/medium640/adafruit_products_CBP_drag_UF2.png?1567715871)

The LEDs will turn red. Then, the **CPLAYBTBOOT** drive will disappear and a new disk drive called **CIRCUITPY** will appear.

That's it, you're done! :)

![adafruit_products_CBP_CIRCUITPY.png](https://cdn-learn.adafruit.com/assets/assets/000/080/535/medium640/adafruit_products_CBP_CIRCUITPY.png?1567716034)

# USB Morse Code Key with CircuitPython

## Code

## Getting the Program's Files

To use the application, you need to obtain **code.py** with the program, and **boot.py** to place on the Bluefruit&nbsp; **CIRCUITPY** drive.

Thankfully, this can be done in one go. Click the **Download Project Bundle** button below to download the project's **code.py** and **boot.py** in a zip file.

Connect your board to your computer via a known good data+power USB cable. The board should show up in your File Explorer/Finder (depending on your operating system) as a flash drive named **CIRCUITPY**.

Extract the contents of the zip file. Copy the **code.py** file, as well as **boot.py** &nbsp;folder to your **CIRCUITPY** drive. You need to reset the device once after pasting the code files so that the USB configuration in **boot.py** will take effect. Use the reset button or unplug then re-connect it.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CPB_Morse_Code_Paddle/code.py

## Drive Structure

After copying the files, your **CIRCUITPY** drive should look like the listing below. It can contain other files as well, but must contain these at a minimum.

![CIRCUITPY drive screenshot showing required project files code.py and boot.py](https://adafruit.github.io/Adafruit_Learning_System_Guides/CPB_Morse_Code_Paddle.png )

# USB Morse Code Key with CircuitPython

## 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.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/CPB_Morse_Code_Paddle/boot.py

## **code.py**

The main project code file is split into the following sections.

### 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&nbsp;[International Morse code](https://en.wikipedia.org/wiki/Morse_code#Representation,_timing,_and_speeds). 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.

### Morse Code Table
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](https://en.wikipedia.org/wiki/Morse_code#/media/File:International_Morse_Code.svg).

The dot and dash representation string is then used with `synthio` to play the appropriate length tone.

![International Morse code table chart showing dot and dash representations of the alphabet and numbers](https://cdn-learn.adafruit.com/assets/assets/000/144/652/medium640/circuitpython_morse_table.png?1782410768)

## 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.

## Main Loop

Inside the main loop the code checks both `keypad.Keys` instances for events to handle. The built-in buttons call&nbsp;`play_morse()` with their respective string when pressed. The Morse key button plays a tone with `synthio` when pressed, and stops the tone when released.

# USB Morse Code Key with CircuitPython

## Use

## Wiring
![Morse code key connected via 3.5mm cable with aligator clips to the pins A1 and GND on the CircuitPlayground Bluefruit](https://cdn-learn.adafruit.com/assets/assets/000/144/630/medium800/circuitpython_morse_key_connected_to_cpb_2.png?1782397805 )

The only wiring required is to connect the 3.5mm cable plug into the Morse code key and connect the alligator clips on the other end of the 3.5mm cable to pins **A1** (white wire) and&nbsp; **GND** (black wire) on the Circuit Playground Bluefruit.

## Select Audio Device
With the project **boot.py** and **code.py** files running on the Circuit Playground Bluefruit, the board will appear to the computer as a USB microphone in addition to the usual&nbsp; **CIRCUITPY** drive and serial console.&nbsp;

The exact steps to select it for a given application will vary. Generally you'll need to either set the CPB as your system default mic in the system settings (Ubuntu Sound input settings screenshot shown here), or set the input within a specific application such as Audacity or Discord.&nbsp;

In Audacity, click **Audio Setup** -\> **Recording Devices** , then click on **Circuit Playground Bluefruit USB Audio** in the list of devices.

In Discord click the dropdown arrow on the **red microphone button** , then click **Input Devices** -\> **Circuit Playground Bluefruit Mono**

![Ubuntu system sound microphone input chooser with CircuitPlayground Bluefruit device highlighted](https://cdn-learn.adafruit.com/assets/assets/000/144/633/medium640/circuitpython_ubuntu_system_chooser.png?1782400937)

![Audacity microphone input chooser with CircuitPlayground Bluefruit device highlighted](https://cdn-learn.adafruit.com/assets/assets/000/144/634/medium640/circuitpython_audacity_chooser.png?1782400924)

![Discord microphone input chooser with CircuitPlayground Bluefruit device highlighted](https://cdn-learn.adafruit.com/assets/assets/000/144/632/medium640/circuitpython_discord_chooser.png?1782400888)

## Live Keying
Pressing the Morse code key will generate a tone into the audio stream via USB to the computer. Use a short press for a dot, and a 3x longer press for a dash. Refer to a [Morse code chart](https://en.wikipedia.org/wiki/Morse_code#/media/File:International_Morse_Code.svg) if you're new to Morse or need a refresher on the alphabet. See if you can decode the short Morse code message keyed with the device from the wave file recording below.

![International Morse code table chart showing dot and dash representations of the alphabet and numbers](https://cdn-learn.adafruit.com/assets/assets/000/144/653/medium640/circuitpython_morse_table.png?1782410917)

[live_key_demo.wav](https://cdn-learn.adafruit.com/assets/assets/000/144/651/original/live_key_demo.wav?1782410365)
## Preprogrammed Messages

The code allows you to have up to two preprogrammed messages. They are stored as string variables in **code.py** and can be sent with the built-in **A** and **B** buttons on the CPB. Change the value of the two variables near the top of the code.

```python
BTN_A_MESSAGE = "HELLO WORLD"
BTN_B_MESSAGE = "CIRCUITPYTHON"
```

Press the **A** or **B** button on the CPB to automatically convert the specified message to Morse code and play it into the USB audio stream.


## Guide Products

### Circuit Playground Bluefruit - Bluetooth® Low Energy

[Circuit Playground Bluefruit - Bluetooth® Low Energy](https://www.adafruit.com/product/4333)
 **Circuit Playground Bluefruit** is our third board in the Circuit Playground series, another step towards a perfect introduction to electronics and programming. We've taken the popular Circuit Playground Express and made it even better! Now the main chip is an nRF52840...

Out of Stock
[Buy Now](https://www.adafruit.com/product/4333)
[Related Guides to the Product](https://learn.adafruit.com/products/4333/guides)
### 3.5mm Mono Audio Plug to Alligator Clips

[3.5mm Mono Audio Plug to Alligator Clips](https://www.adafruit.com/product/4181)
This cable is easy to understand - you get a 3.5mm mono audio plug on one end, and two alligator clips on the other. It's also really easy to use, perfect for clipping onto a micro:bit or Circuit Playground! It's also good for use with AT (accessibility technology) buttons as they use...

In Stock
[Buy Now](https://www.adafruit.com/product/4181)
[Related Guides to the Product](https://learn.adafruit.com/products/4181/guides)
### USB cable - USB A to Micro-B

[USB cable - USB A to Micro-B](https://www.adafruit.com/product/592)
This here is your standard A to micro-B USB cable, for USB 1.1 or 2.0. Perfect for connecting a PC to your Metro, Feather, Raspberry Pi or other dev-board or microcontroller

Approximately 3 feet / 1 meter long

Out of Stock
[Buy Now](https://www.adafruit.com/product/592)
[Related Guides to the Product](https://learn.adafruit.com/products/592/guides)

## Related Guides

- [Adafruit Circuit Playground Bluefruit](https://learn.adafruit.com/adafruit-circuit-playground-bluefruit.md)
- [PyLeap CLUE Barometer](https://learn.adafruit.com/pyleap-clue-barometer.md)
- [CRICKIT Exhibit Demo Board](https://learn.adafruit.com/crickit-exhibit-demo-board.md)
- [Makecode para la Circuit Playground Express](https://learn.adafruit.com/makecode-es.md)
- [Infinity Mirror Valentine's Candy Box](https://learn.adafruit.com/infinity-mirror-candy-box.md)
- [Visual Studio Code for Education... and CircuitPython](https://learn.adafruit.com/visual-studio-code-for-education-and-circuitpython.md)
- [Glowing Bottle Castle and Capacitive Touch Piano](https://learn.adafruit.com/glowing-bottle-castle-and-capacitive-touch-piano.md)
- [Circuit Playground Bluefruit BLE Heart Rate Pendant with CircuitPython](https://learn.adafruit.com/ble-heart-rate-display-pendant.md)
- [Crawling Animatronic Hand](https://learn.adafruit.com/crawling-hand-with-cpx-and-makecode.md)
- [Bluefruit Luminary Lanterns with Capacitive Touch](https://learn.adafruit.com/bluefruit-luminary-lanterns-with-capacitive-touch.md)
- [Remote Control Candy Dispenser Ghost](https://learn.adafruit.com/ble-claw.md)
- [PyPortal Quarantine Clock](https://learn.adafruit.com/pyportal-quarantine-clock.md)
- [MicroBlocks Circuit Playground Express Ornament](https://learn.adafruit.com/microblocks-circuitplayground-express-ornament.md)
- [How to Make Games in MakeCode Arcade](https://learn.adafruit.com/how-to-make-games-on-makecode-arcade.md)
- [A Logger for CircuitPython](https://learn.adafruit.com/a-logger-for-circuitpython.md)
- [Ocean Epoxy Resin Lightbox with RGB LED Matrix Image Scroller](https://learn.adafruit.com/ocean-epoxy-resin-lightbox-with-rgb-led-matrix-image-scroller.md)
