After trying the simple example on the previous page, you can switch to this code to try out Bluetooth LE functionality using the Adafruit Bluefruit LE Connect app on your iOS or Android device.

See this guide to get the app installed and set up.

Installing Project Code

To use with CircuitPython, you need to first install a few libraries, into the lib folder on your CIRCUITPY drive. Then you need to update with the example script.

Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the file in a zip file. Extract the contents of the zip file, open the directory Baudot_tty/baudot_tty_ble/ and then click on the directory that matches the version of CircuitPython you're using and copy the contents of that directory to your CIRCUITPY drive.

Your CIRCUITPY drive should now look similar to the following image:

# SPDX-FileCopyrightText: 2020 John Park for Adafruit Industries
# SPDX-License-Identifier: MIT

### Baudot TTY Message Transmitter
### Bluefruit Connect UART mode to send messages to CLUE for audio
### tramsission to TTY machine.

### The 5-bit mode is defined in ANSI TIA/EIA-825 (2000)
### "A Frequency Shift Keyed Modem for use on the Public Switched Telephone Network"

import time
import math
import array
import board
import audiopwmio
from audiocore import RawSample
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from import UARTService

# BLE radio setup
ble = BLERadio()
uart_server = UARTService()
advertisement = ProvideServicesAdvertisement(uart_server) = "TTY_MACHINE"  # pylint: disable=protected-access

# constants for sine wave generation
SIN_LENGTH = 100  # more is less choppy
SIN_AMPLITUDE = 2 ** 12  # 0 (min) to 32768 (max)  8192 is nice
SIN_OFFSET = 32767.5  # for 16bit range, (2**16 - 1) / 2
DELTA_PI = 2 * math.pi / SIN_LENGTH  # happy little constant

sine_wave = [
    int(SIN_OFFSET + SIN_AMPLITUDE * math.sin(DELTA_PI * i)) for i in range(SIN_LENGTH)
tones = (
    RawSample(array.array("H", sine_wave), sample_rate=1800 * SIN_LENGTH),  # Bit 0
    RawSample(array.array("H", sine_wave), sample_rate=1400 * SIN_LENGTH),  # Bit 1

bit_0 = tones[0]
bit_1 = tones[1]
carrier = tones[1]

char_pause = 0.0  # pause time between chars, set to 0 for fastest rate possible

dac = audiopwmio.PWMAudioOut(
)  # the CLUE edge connector marked "#0" to STEMMA speaker
# The CLUE's on-board speaker works OK, not great, just crank amplitude to full before trying.
# dac = audiopwmio.PWMAudioOut(board.SPEAKER)

LTRS = (
    " ",

FIGS = (
    " ",

char_count = 0
current_mode = LTRS

#  The 5-bit Baudot text telephone (TTY) mode is a Frequency Shift Keyed modem
#  for use on the Public Switched Telephone network.
#   Definitions:
#       Carrier tone is a 1400Hz tone.
#       Binary 0 is an 1800Hz tone.
#       Binary 1 is a 1400Hz tone.
#       Bit duration is 20ms.
#       Two modes exist: Letters, aka LTRS, for alphabet characters
#       and Figures aka FIGS for numbers and symbols. These modes are switched by
#       sending the appropriate 5-bit LTRS or FIGS character.
#   Character transmission sequence:
#       Carrier tone transmits for 150ms before each character.
#       Start bit is a binary 0 (sounded for one bit duration of 20ms).
#       5-bit character code can be a combination of binary 0s and binary 1s.
#       Stop bit is a binary 1 with a minimum duration of 1-1/2 bits (30ms)

def baudot_bit(pitch=bit_1, duration=0.022):  # spec says 20ms, but adjusted as needed, loop=True)
    # dac.stop()

def baudot_carrier(duration=0.15):
    # Carrier is transmitted 150 ms before first character is sent
    baudot_bit(carrier, duration)

def baudot_start():

def baudot_stop():
    baudot_bit(bit_1, 0.04)  # minimum duration is 30ms

def send_character(value):
    baudot_carrier()  # send carrier tone
    baudot_start()  # send start bit tone
    for i in range(5):  # send each bit of the character
        bit = (value >> i) & 0x01  # bit shift and bit mask to get value of each bit
        baudot_bit(tones[bit])  # send each bit, either 0 or 1, of a character
    baudot_stop()  # send stop bit
    baudot_carrier()  # not to spec, but works better to extend carrier

def send_message(text):
    global char_count, current_mode  # pylint: disable=global-statement
    for char in text:
        if char not in LTRS and char not in FIGS:  # just skip unknown characters
            print("Unknown character:", char)

        if char not in current_mode:  # switch mode
            if current_mode == LTRS:
                print("Switching mode to FIGS")
                current_mode = FIGS
            elif current_mode == FIGS:
                print("Switching mode to LTRS")
                current_mode = LTRS
        # Send char mode at beginning of message and every 72 characters
        if char_count >= 72 or char_count == 0:
            print("Resending mode")
            if current_mode == LTRS:
            elif current_mode == FIGS:
            # reset counter
            char_count = 0
        # increment counter
        char_count += 1

while True:
    while not ble.connected:

    # Connected

    # Loop and read packets
    while ble.connected:
        if uart_server.in_waiting:
            raw_bytes =
            textmsg = raw_bytes.decode().strip()
            print("received text =", textmsg)

    # Disconnected

Once you've copied the code file to your CLUE, you can connect from the Bluefruit app to the CLUE device named TTY Machine and then use the UART function to send messages to the CLUE.

The letters will be received by the CLUE and then sent as audio to the TTY machine!

This guide was first published on May 05, 2020. It was last updated on May 05, 2020.

This page (Code the BLE TTY Transmitter) was last updated on Aug 16, 2022.

Text editor powered by tinymce.