Note

This is the documentation for the latest development branch and may refer to features that are not available in released versions. If you are looking for the documentation for a specific release, use the drop-down menu on the left and select the desired version.

UART Usage Tutorial#

What is UART?#

UART (Universal Asynchronous Receiver/Transmitter) is a common serial communication protocol used to send and receive data between devices in a serial manner. It is widely used for:

  • Serial terminal debugging

  • Communication with sensors or external modules (e.g., GPS, GSM modules)

  • Data exchange between master and slave devices

K230 UART Features#

Feature

Description

Number of supported channels

5 UARTs: UART0 ~ UART4

Available channel description

UART0 is occupied by system debugging, UART1, 2, 3, 4 are available

Configurable parameters

Baud rate, data bits, stop bits, parity bits, etc.

Pin multiplexing

Supports setting any IO to UART function via IOMUX

Data operation methods

Supports methods such as write(), read(), readline(), readinto()

Application Example: Serial Send/Receive Test#

The following example demonstrates how to initialize the UART1 interface and perform data sending and receiving operations.

Example Code#

from machine import UART

# ========== 初始化 UART1 ==========
u1 = UART(
    UART.UART1,           # UART1 通道
    baudrate=115200,      # 波特率 115200
    bits=UART.EIGHTBITS,  # 数据位 8 位
    parity=UART.PARITY_NONE,  # 无校验
    stop=UART.STOPBITS_ONE    # 停止位 1 位
)

# ========== 发送数据 ==========
u1.write("UART1 test")  # 发送字符串

# ========== 读取数据 ==========
r = u1.read()            # 读取接收到的字节数据(可能为 None)
r = u1.readline()        # 读取一整行数据(以换行符结尾)
b = bytearray(8)
u1.readinto(b)           # 将接收到的数据填充到 bytearray 中

# ========== 释放资源 ==========
u1.deinit()

Interface Description#

Method Name

Description

UART()

Create a UART object, specifying the channel number and communication parameters

write(data)

Write a string or byte data to the serial port

read()

Read all available data, return a bytes object or None

readline()

Read one line of data, terminated by a newline character \n

readinto(buf)

Read data into the specified buffer (such as bytearray)

deinit()

Stop the serial port and release resources

Application Scenario Examples#

  • Serial debug output: replace print(), making it easier for external devices to monitor logs

  • Communicate with serial modules: such as Bluetooth, LoRa, 4G modules

  • Multi-serial concurrent task processing: connect multiple peripherals through UART1/2/4

Troubleshooting#

Issue

Troubleshooting Suggestions

Unable to receive data

Check the baud rate settings and whether the IO mapping is correct

Received data is garbled

Confirm that the communication parameters on both ends are consistent (baud rate, data bits, etc.)

Read result is None

There is no readable data in the serial port buffer

UART0/UART3 cannot be used

These two channels are occupied by the system; it is recommended to use UART1/2/4

Further Reading#

Comments list
Comments
Log in