Using UART on the Pyboard

Contents

What is UART?

UART, or Universal Asynchronous Receiver/Transmitter, is a hardware communication protocol for serial data transmission between two devices.

It is asynchronous because each device must maintain its own clock and transmission from either device may start at any time. Start and stop bits are added to data frames to signal when transmission begins and ends.

There are only two pins/connectors (TX and RX) plus a common ground. A device uses its TX pin to transmit data. The RX pin is used to receive data. The TX pins and RX pins are cross-connected between devices. Thus the TX pin on one device is connected to the RX pin on the other device and vice versa.

Pyboard Hardware UARTs

The Pyboard V1 has five separate hardware UARTs, configured as shown in the following table:

UART Name TX Pin RX Pin
UART(1) X9 X10
UART(2) X3 X4
UART(3) Y9 Y10
UART(4) X1 X2
UART(6) Y1 Y2

NOTE: UART(5) is not available on the Pyboard.

MicroPython pyb.UART Class

All things UART on the Pyboard are configured through the pyb.UART class.

Initialising the UART Object

The first step is to declare and initialise an UART object.


Syntax:
<uart_variable> = pyb.UART(bus, baudrate)

Creates a UART object.

Where:
  bus : One of 1, 2, 3, 4, or 6 based on
  the above table.

  baudrate : Is the agreed data transmission
  speed between the two devices in units of
  bits per second.
  Common baud rates are 9600, 19200, 57600
  and 115200 bps.
            

Syntax:
pyb.UART.deinit()

De-initialise the UART making its pins available
for other uses.

Examples:
from pyb import UART

# Define UART(1), baud rate = 9600
uart1 = UART(1, 9600)

# Define UART(2), baud rate = 115200
uart2 = UART(2, 115200)
# De-initialise UART(1)
uart1.deinit()
            

This simple syntax initialises the chosen UART at the specified baud rate and using the most commonly used configuration: 8 data bits, no parity bit, 1 stop bit; usually abbreviated (8, N, 1)

In the rare event that these parameters need to be changed please refer to the official MicroPython documentation[1].

UART Methods

The UART object provides stream functions that are used as if writing to a file.


Syntax:
UART.write(buf)

Write the buffer of bytes or strings to the bus.

Where:
  buf : One or more bytes or strings written to the serial bus.
        
Returns: The number of bytes written.
            

Syntax:
UART.any()

Returns: Returns the number of bytes
         available to be read (may be 0).
            

Syntax:
UART.read([nbytes])

Where:
  nbytes : The number of bytes at most read.
           If absent all bytes available are read.

Returns: A bytes object containing the bytes read in.
            

Syntax:
UART.readline()

Returns: A line, ending in a newline character.
            

Reading Temperature from the US-100 Sensor

The US-100 is an ultrasonic sensor that measures distances out to a range of about four metres. It also has an onboard low resolution (8-bits) temperature sensor that's used by the main chip to apply temperature compensation to distance measurements.

The US-100 has an easy to use UART interface. This example uses the Pyboard to return the ambient air temperature via the UART interface. The algorithm is simple:

  1. Send the initiate temperature measurement command by transmitting 0x50 to the US-100.
  2. Wait 100ms for the sensor to process the command and prepare the data.
  3. If any bytes available, read one byte from the US-100.
  4. Calculate temperature:
    Temperature °C = Raw value - 45

The UART(1) will be used on the Pyboard. The hookup to the US-100 is simple:

Pyboard US-100
3.3V VCC
GND GND
Pin X9 Trig/Tx
Pin X10 Echo/Rx

Copy the following code to a suitable IDE such as Thonny and flash to the Pyboard.


Code:
from pyb import UART, delay

# Define UART(1), baud rate = 9600
uart1 = UART(1, 9600)

CMD = bytes([0x50]) # Initiate temperature reading command
degC = ''.join(['\u00B0', 'C']) # Degrees symbol

# Initiate the temperature measurement
uart1.write(CMD)
# Wait for measurement to complete
delay(100)
# Check if data is ready
if uart1.any() > 0:
    # Read raw temperature
    raw = uart1.read(1)
    # Convert to degC
    print(raw[0] - 45, degC)
else:
    print(None) # No data available           
          

Typical Output:
29 °C
          
Pyboard's UART(1) connected to UART interface of the US-100 sensor.
Fig 1 - Pyboard's UART(1) connected to the UART interface of the US-100 sensor