Overview

This guide explains serial connections between the Circuit Playground Express and other devices. This is very useful for making connections to other microcontrollers or single board computers like the Raspberry Pi. And some sensors require serial communications instead of other protocols like I2C or SPI.

An outbound serial connection can be used for applications like CNC, data logging, debugging code, and many other things.

You can program serial communications with Microsoft MakeCode and CircuitPython - examples will be shown in each.

Parts List

Circuit Playground Express

PRODUCT ID: 3333
Circuit Playground Express is the next step towards a perfect introduction to electronics and programming. We've taken the original Circuit Playground Classic and...
$24.95
IN STOCK

USB to TTL Serial Cable - Debug / Console Cable for Raspberry Pi

PRODUCT ID: 954
The cable is easiest way ever to connect to your microcontroller/Raspberry Pi/WiFi router serial console port. Inside the big USB plug is a USB<->Serial conversion chip and at...
$9.95
IN STOCK

Adafruit CP2104 Friend - USB to Serial Converter

PRODUCT ID: 3309
Long gone are the days of parallel ports and serial ports. Now the USB port reigns supreme! But USB is hard, and you just want to transfer your every-day serial data from a...
$5.95
IN STOCK

Small Alligator Clip to Male Jumper Wire Bundle - 6 Pieces

PRODUCT ID: 3448
When working with unusual non-header-friendly surfaces, these handy cables will be your best friends! No longer will you have long, cumbersome strands of alligator clips. These...
$3.95
IN STOCK

What is Serial Communications?

In simple terms, serial data is sending data over few wires in a timed sequence. This is the opposite of parallel in which data may simultaneously be transmitted over many wires.

To take advantage of the benefits of serial communications, standards have been devised:

First was RS-232, a standard that has evolved over time and used as a basis for the TTL Serial interface, lower level connectivity between two electronics boards.

Later was the Universal Serial Bus (USB), devised to be better than RS-232. With refinements and operating system innovations like "Plug and Play", USB has dominated modern electronics device connectivity.

Serial Hardware

The Circuit Playground Express has two serial interfaces baked in:

  • The USB connector for high level serial
  • Pads RX/A6 and TX/A7 (along with an electrical ground GND) provide a 3.3 volt TTL serial interface

There also is the capability to work with TTL serial over any two set of data pins. This is called software serial as there is no dedicated hardware on the Circuit Playground Express' microcontroller chip to assist in providing a serial interface, the data serialization is done in software instead of hardware. See the CircuitPython Essentials Guide for more in-depth information on selecting serial pins.

USB to TTL Serial Adapters

It is very useful to have devices that can convert from a low level TTL serial interface to USB. Likewise it can be useful to have a USB capable computer talk low level TTL serial. 

Adafruit and other companies sell USB to serial adapters with various capabilities for providing this type of conversion and interface.

 

The USB to TTL Serial Cable provides serial conversion both ways: TTL to USB and USB to TTL serial. It makes connections very easy.

 

The Adafruit CP2104 Friend - USB to Serial Converter Board adds additional serial connection signals needed by some projects. It is very reasonable price-wise and comes with transmit and receive LED indicators. Some soldering might be needed for connections.

Serial Ports on Computers

Our computers (be they a PC, Mac, Linux box or a mobile device) all have serial communications baked-in. How this is usually done is via a number of USB ports. 

USB ports show up on your computer in operating system-dependent ways:

  • Windows: Serial ports are called COM ports and are typically numbered. For example COM1, COM2,.., COM23, .. COM33, etc. Use the Device Manager to find your COM port name
  • Mac: Serial ports follow a Linux/Unix like convention, with device names like /dev/tty.usbmodem and a suffix like a number. In Terminal, type ls /dev/tty* to list all serial ports.
  • Linux: Serial ports are also in /dev and start with tty but may vary in their naming conventions, an example may be /dev/ttyUSB0. In the command line type ls /dev/tty* to list all serial ports

Serial Communications Software

Serial Terminal Emulation Use

Going back to the serial RS-232 standard, there are a number of configurations for how many serial bits are sent at once. But with TTL serial, it is pretty standard that software is set for 8 bits, no parity bit, one stop bit, abbreviated 8,N,1 or 8, 1, N.

A key parameter for setting up serial communications is setting a baud rate. Best to think of it as how many bits a second are transmitted. Typical speeds follow old-time modem rates such as 1200, 9600, 19200, 57600, 115200. 256000, and 1 megabit. While setting non-standard rates may be possible with software serial, hardware may only accept fixed rates. For microcontrollers, 9600 is a typical slow rate and 115200 often a fast rate. Check your hardware specifications (spec) sheet for the baud rates the board supports.

When you run your terminal emulator, you'll need at least the baud rate, possibly the bit configuration also. Below is the configuration you might set up for Windows PuTTY:

Note the Serial line box has the COM port name, and Speed has the baud rate. We also have to click the Serial radio button to make sure we have a Serial connection

Software

While your final project may have two devices communicating without any visualization on what is being exchanged, viewing serial communications is very common. It was the primary way to interface with minicomputers before PCs came along.

Serial communications software has many names but they all basically do the same thing: display the outgoing data as characters and possibly allow the user to send characters back.

Mac and Linux

Linux and the Mac command line have the terminal capability to initiate serial communications. See our guide Advanced Serial Console for Mac and Linux for more information

Windows

Windows does not have a serial terminal standard but there are many third party terminal emulators including PuTTY and Tera Term among others. See our guide Advanced Serial Console on Windows for more information.

Your Windows COM port will be shown in the Control Panel -> Devices and Printers section, under Unspecified Devices and names Silicon Labs CP210x USB to UART Bridge.

Use that COM port and the baud rate in your code for your terminal software

External TTL Serial Connections

makecode_Serial-to-camera_bb2.png
The TTL camera operates on 5V DC but performs 3.3 volt TTL serial so it is Circuit Playground Express safe run via USB power.

Device Types

You will find a variety of devices that use TTL serial connections. This includes:

Connections

Check the data sheets for the serial devices you are looking to connect to - the Circuit Playground Express is a 3.3 volt device and may be damaged if connected to a 5 volt signal level TTL device

Check the data sheet for your device. If it is a 5 volt device, you may need a level shifting chip to interface between the TTL device and the Circuit Playground Express.

If your device is 3.3 volts safe, here are the connections you will want to make:

  • Connect the Circuit Playground Express TX/A7 pad to the device RX/Receive pin
  • Connect the Circuit Playground Express RX/A6 pad to the device TX/Transmit pin
  • Connect the Circuit Playground Express GND/Ground pad to the device GND/Ground

If the device will be powered by the Circuit Playground Express, you can connect the 3.3V pad to the 3.3 Volt input of the device. But be sure the device will not draw more than about 300 milliamps. You can draw up to 450 milliamps if you do not use the NeoPixels on the Circuit Playground Express. See this page for details.

You will note the TX on Circuit Playground Express connects to RX on your device, RX on the Circuit Playground Express connects to TX on your device. This is standard serial convention - if your project does not work, check to see that you did NOT connect RX to RX and TX to TX.

The Adafruit TTL serial camera is a good example board (see the Fritzing diagram at the top of the page). The TTL serial connections run at 3.3 volts so hooking TX to RX, RX to TX and ground to ground makes the communications work. The camera board must be powered by 5 volts. This is taken from the Vout pad which does have 5 volts when the Circuit Playground Express board is powered from a USB connection.

USB Converter Connections

Using the Adafruit USB Serial to USB Cable

makecode_Serial-to-cable-w-pwr.png
The red wire is not required and should not be used if you power the board from a battery or the native USB port.
Note you can use the +5 volts out on the external cable and board to provide power to the Circuit Playground Express on the Vout pad. The Circuit Playground Express should not draw more than 200 milliamps or so unless you use the NeoPixels where the current could rise above 450 milliamps which may be quite high. Check any device's specifications if you have custom needs or use an external power source and do not connect the converter's +5 out to the board.
Note the color connections: The White Alligator Clip on RX goes to the GREEN wire on the converter, the Green Alligator Clip to the WHITE wire on the converter. That is TX -> RX and RX -> TX for serial between devices.

If you are looking to establish a serial connection via the Adafruit USB console cable #954, the connections are shown above. This diagram shows using the USB cable power for the Circuit Playground Express (the red wire). If the console cable is plugged into a computer, the red wire has +5 volts and can power the Circuit Playground Express if connected to the Vout pad. If you plan to power the Circuit Playground Express via battery or USB power via the onboard USB connection, do not connect the USB serial converter cable red wire.

The signal wires RX and TX are 3.3 volt "TTL" levels so they are safe for Circuit Playground Express.

For computer drivers for using the USB port from the USB converter cable:

In Windows, you will see the following icon appear in the Control Panel -> Devices and Printers view:

Using the Adafruit CP2104 Serial Communications Board

Above shows the connections for the Adafruit CP2104 Serial board #3309. The headers are not soldered in to get a better view of the board markings. This diagram assumes you are using USB or battery power for the Circuit Playground Express. The signal wires RX and TX are 3.3 volt "TTL" levels so they are safe for Circuit Playground Express. The ground wire provides an electrical return path so is necessary.

For Windows, the USB driver is in Windows Update, Mac OS drivers are here, Linux does not need a driver.

What do these connections do?

These connections turn a serial connection between the Circuit Playground Express (via the hardware serial TX and RX pads) to the cable and board which have chips which convert the TTL serial into USB serial communications.

Why not just use the Circuit Playground Express USB port?

1) The software support for USB on the Circuit Playground Express was discussed earlier - there are some limitations on its use in various languages that may not be present with an external board.

2) Sometimes you need more than one serial connection.

Can I program the Circuit Playground Express via an external USB connection like the ones shown?

Nope! This is only for debugging output from MakeCode or CircuitPython

Serial Communications Code

The following pages have the explanation of how to use serial communications for MakeCode and CircuitPython.

For both of these examples, you'll need to have the wiring to your USB Serial cable or adapter as seen above!

MakeCode

MakeCode has a beta set of blocks for serial communications. You will need to use the web address https://makecode.adafruit.com/beta. Click New Project. Then Click the ADVANCED block group then the EXTENSIONS button under that. One of the experimental extensions is Serial. Click that and you should see back on the main block editing page a new block group SERIAL.

These are the blocks you can use to connect to external serial peripherals and serial to USB devices. 

Note there is no Serial Read function block. Currently the Beta code only allows writing to a serial port.

Here is a simple program to write the light level and temperature read by the Circuit Playground Express to a serial interface. The serial redirect block sets up the communication pads and the baud rate. The program below uses the pads marked TX and RX for transmit and receive. If you use other pads, they will work but you are not using the hardware serial on the board but a software serial code function which is fine for the low transmission rates allowed by the block (no greater than 115,200 baud).

The hardware setup is shown on the page USB Converter Connections.

If you open up a terminal program and set it for your communications port at 115,200 baud, you'll get the output similar to that below:

If you get garbled text, you don't have the right baud rate, close the terminal and rerun the terminal program with the correct baud rate (for the code shown, that is 115,200).

If you get nothing, check the following:

  1. Is the Power On green LED on Circuit Playground Express? If not, add the correct power supply.
  2. Did you load the MakeCode onto the Circuit Playground Express?
  3. Are your connections such that the Circuit Playground Express TX pad connects to the converter RX (white wire) and that the RX pad connects to the TX (green wire) on the converter? They must be crossed for serial connections between devices).
  4. Ensure your terminal emulator is set for 115200 baud. For PuTTY, this is in the opening screen, with the COM port number the one from the Control Panel -> Devices and Printers for the Silicon Labs CP210x USB to UART Bridge device.

CircuitPython

CircuitPython has support for serial communications through the busio library. The CircuitPython Essentials guide has a page on UART Serial for CircuitPython which is a good reference.

The following code opens serial communications via busio.UART on the Circuit Playground Express TX and RX pads defined in the board library. 

The code then goes into a loop, waiting for a character to be received (any character), sending back the time (relative), the light intensity (relative) and the temperature in Celsius. The sleep time between reads can be adjusted.

import time
import board
import busio
from adafruit_circuitplayground.express import cpx

uart = busio.UART(board.TX, board.RX, baudrate=115200)

while True:

    data = uart.read(1)  # read a byte

    if data is not None:  # Data was received

        output = "%0.1f\t%d\t%0.1f\r\n" % (time.monotonic(),
                                           cpx.light, cpx.temperature)
        uart.write(output)         # Print to serial

        time.sleep(1.0)

The output is shown below. The data will not be displayed until you type in a character (any one character), then the program returns the time, the light value, and the temperature in Celsius. The read is not required, you can remove that if you like and have the data be written on every pass of the loop delayed by the sleep time.

The documentation for busio.UART is on Read The Docs.

Also see the CircuitPython Essentials guide UART Communications page.

This guide was first published on Aug 14, 2018. It was last updated on Aug 14, 2018.