Welcome to ESP32 Core Board repository
1. Serial UART Communication Experiment
Serial UART Communication Experiment
Serial UART Communication Experiment1. UART Communication Overview2. Core Concepts2.1 What is UART2.2 UART Parameter Selection2.3 Core API3. Hardware Dependencies4. Project Architecture and Flow5. Source Code Details5.1 UART Initialization5.2 Transmit and Receive5.3 Main Loop6. Operation Procedure6.1 Flashing and Running6.2 Serial Output Example6.3 Expected Behavior6.4 Verification Method7. Common Issues
1. UART Communication Overview
UART (Universal Asynchronous Receiver/Transmitter) is a serial communication protocol that only needs two wires, TX (transmit) and RX (receive), to achieve bidirectional data transfer.
In this experiment, machine.UART is used to configure UART1 of the ESP32-S3, continuously sending a string at 115200 baud and performing loopback reception.
Typical application scenarios:
| Application | Example |
|---|---|
| Debug output | Serial terminal, log printing |
| Module communication | GPS, Bluetooth module, 4G module |
| Device interconnection | Data exchange between MCUs |
| Sensor readings | Serial sensors (PM2.5, temperature/humidity, etc.) |
2. Core Concepts
2.1 What is UART
UART converts parallel data into a serial bit stream, transmitted bit by bit according to the frame format:
Start bit + Data bits (5~8) + Parity bit (optional) + Stop bit (1/1.5/2)│ │ │ │▼ ▼ ▼ ▼1bit 8bit (LSB) 0/1bit 1bit
The common format 8N1 = 8 data bits, no parity, 1 stop bit (MicroPython default).
2.2 UART Parameter Selection
| Parameter | Value in this experiment | Description |
|---|---|---|
| Port | UART1 | Avoids UART0 (occupied by the REPL system) |
| Baud rate | 115200 | Standard rate, compatible with most devices |
| TX pin | GPIO10 | Transmit data line |
| RX pin | GPIO11 | Receive data line |
| Data bits | 8 (default) | 8 data bits |
| Parity | None (default) | No parity |
ESP32-S3 has 3 UARTs: UART0 (occupied by REPL), UART1 and UART2 (available to users).
2.3 Core API
xfrom machine import UART, Pinuart = UART(1, baudrate=115200, tx=Pin(10), rx=Pin(11, Pin.IN, Pin.PULL_UP)) # Create UART, RX pull-upuart.write(b'hello\r\n') # Send byte stringbuf = uart.read(255) # Read up to 255 bytesdata = buf.decode('utf-8').strip() # Decode into a string3. Hardware Dependencies
| GPIO | Peripheral | Mode | Description |
|---|---|---|---|
| 10 | UART1 TX | UART | Transmit pin |
| 11 | UART1 RX | UART | Receive pin |
Loopback test: Short GPIO10 (TX) and GPIO11 (RX) with a Dupont wire to transmit and receive on its own.
Wiring diagram:

4. Project Architecture and Flow
xxxxxxxxxxScript execution (single-file .py)│├─ UART(1, baudrate=115200, tx=10, rx=11)│└─ while True:├─ print("Sent: ...") // Print sent data├─ uart.write(...) // Send├─ sleep_ms(50) // Wait for data to fully arrive├─ uart.read(255) → decode('utf-8') → print("Received:") // Receive and print└─ sleep_ms(2000) // 2s interval
5. Source Code Details
Complete source code: 5.Source Code → MicroPython → 3.Serial Communication and Storage System → 1. UART → 1. UART.py
5.1 UART Initialization
xxxxxxxxxxfrom machine import UART, Pin # UART and Pin classesimport time # delay# Initialize UART1 (avoid REPL UART0)# RX pull-up to avoid garbage when floatinguart = UART(1, baudrate=115200, tx=Pin(10), rx=Pin(11, Pin.IN, Pin.PULL_UP))5.2 Transmit and Receive
xxxxxxxxxx# TX functiondef uart_tx(data): uart.write(data)# RX functiondef uart_rx(): buf = uart.read(255) # Read serial buffer, max 255 bytes if buf: try: print("Received:", buf.decode('utf-8').strip()) # Normal text decode except UnicodeError: print("Received raw:", buf) # Print raw bytes on failure5.3 Main Loop
xxxxxxxxxx# Main logicprint("UART Started, TX=GPIO10, RX=GPIO11")while True: send_str = "GOOD GOOD STUDY,DAY DAY UP!!!!!!\r\n" print("Sent:", send_str.strip()) # Print what was sent uart_tx(send_str) time.sleep_ms(50) # Brief delay for RX to complete uart_rx() time.sleep_ms(2000) # 2s interval6. Operation Procedure
6.1 Flashing and Running
Thonny IDE operation steps: See MicroPython → 1. Before Development → 3.Thonny IDE.
- Loopback test: Short GPIO10 (TX) and GPIO11 (RX) with a Dupont wire
- Open
1. UART.pywith Thonny IDE and click Run
6.2 Serial Output Example
xxxxxxxxxxUART Started, TX=GPIO10, RX=GPIO11Sent: GOOD GOOD STUDY,DAY DAY UP!!!!!!Received: GOOD GOOD STUDY,DAY DAY UP!!!!!!Sent: GOOD GOOD STUDY,DAY DAY UP!!!!!!Received: GOOD GOOD STUDY,DAY DAY UP!!!!!!...

6.3 Expected Behavior
Prints the received string every 2 seconds. If TX/RX is shorted correctly, the Received content is identical to the sent content.
6.4 Verification Method
| Action | Expected result |
|---|---|
| Short GPIO10 - GPIO11 | Received: shows the complete sent content |
| Remove the shorting wire | No Received: output (no data received) |
Change baudrate=9600 | Garbled data (TX/RX baud rates do not match) |
Change uart.write("Hello") | Received content becomes "Hello" |
7. Common Issues
| Symptom | Cause | Solution |
|---|---|---|
| No Received output | TX/RX not shorted | Connect GPIO10 and GPIO11 with a Dupont wire |
| Received shows garbage or abnormal characters | Baud rate mismatch | Confirm baudrate=115200 is the same on both ends |
UART(0, ...) fails | UART0 is occupied by the system REPL | Use UART(1) or UART(2) instead |
| Received is empty after sending | Insufficient receive delay | Increase the sleep_ms() delay |