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.
# SPDX-FileCopyrightText: 2018 Anne Barela for Adafruit Industries # # SPDX-License-Identifier: MIT 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.
Page last edited January 22, 2025
Text editor powered by tinymce.