Circuitpython Uart Serial - Adafruit Feather M4 Express Manual

Table of Contents

Advertisement

CircuitPython UART Serial

In addition to the USB-serial connection you use for the REPL, there is also a hardware UART you can use. This is
handy to talk to UART devices like GPSs, some sensors, or other microcontrollers!
This quick-start example shows how you can create a UART device for communicating with hardware serial devices.
To use this example, you'll need something to generate the UART data. We've used a GPS! Note that the GPS will give
you UART data without getting a fix on your location. You can use this example right from your desk! You'll have data
to read, it simply won't include your actual location.
You'll need the adafruit_bus_device library folder if you don't already have it in your /lib folder! You can get it from the
CircuitPython Library Bundle
Libraries page
(https://adafru.it/ABU).
Copy and paste the code into code.py using your favorite editor, and save the file.
# CircuitPython Demo - USB/Serial echo
import board
import busio
import digitalio
led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT
uart = busio.UART(board.TX, board.RX, baudrate=9600)
while True:
data = uart.read(32)
# print(data)
# this is a bytearray type
if data is not None:
led.value = True
# convert bytearray to string
data_string = ''.join([chr(b) for b in data])
print(data_string, end="")
led.value = False
The Code
First we create the UART object. We provide the pins we'd like to use,
. While these pins are labeled on most of the boards, be aware that RX and TX are not labeled on
baudrate=9600
Gemma, and are labeled on the bottom of Trinket. See the diagrams below for help with finding the correct pins on
your board.
Once the object is created you read data in with
will return a byte array type object if anything was received already. Note it will always return immediately because
there is an internal buffer! So read as much data as you can 'digest'.
If there is no data available,
© Adafruit Industries
(https://adafru.it/y8E). If you need help installing the library, check out the
# read up to 32 bytes
read( numbytes )
will return
, so check for that before continuing.
read()
None
https://learn.adafruit.com/adafruit-feather-m4-express-atsamd51
and
board.TX
board.RX
where you can specify the max number of bytes. It
CircuitPython
, and we set the
Page 145 of 183

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the Feather M4 Express and is the answer not in the manual?

Related Products for Adafruit Feather M4 Express

Table of Contents

Save PDF