How STM32 interacts with serial port screens?


STM32 microcontrollers can communicate with serial port display screens (also called UART TFT screens) using standard UART (USART) interfaces. These screens often include a built-in microcontroller and use a command-based protocol over a serial connection (TX/RX).
1. Basic Hardware Connection
STM32 Pin | Screen Pin |
TX (e.g., USART1_TX) | RX (on the screen) |
RX (e.g., USART1_RX) | TX (on the screen) |
GND | GND |
5V/3.3V | VCC (depending on screen voltage) |
Voltage levels must match — if the screen is 5V and STM32 is 3.3V, use a level shifter.
2. Communication Protocol
Most UART screens (e.g., Nextion, ST7920 UART modules, or custom ones) follow a command format like:
css
Command bytes → [Header] + [Command ID] + [Data] + [Checksum/End bytes]
Or simply:
vbnet
"page 1\xff\xff\xff" → Change to page 1 (Nextion example)
You send these via HAL_UART_Transmit()
, e.g.:
c
char cmd[] = "page home\xff\xff\xff";
HAL_UART_Transmit(&huart1, (uint8_t*)cmd, strlen(cmd), HAL_MAX_DELAY);
Some screens also send responses back to STM32 (e.g., button presses).
3. STM32 HAL UART Setup
In STM32CubeMX / STM32CubeIDE:
Enable USART1 (or any USART).
Set:
Baud Rate (e.g., 9600 or 115200)
8 data bits, no parity, 1 stop bit (default)
Generate code.
Initialize the UART (HAL handles this in
MX_USARTx_UART_Init()
).
4. Interfacing in Code
Transmitting Data (STM32 → Screen):
c
char txt[] = "t0.txt=\"Hello\"\xff\xff\xff";
HAL_UART_Transmit(&huart1, (uint8_t*)txt, strlen(txt), HAL_MAX_DELAY);
Receiving Data (Screen → STM32):
You can use:
HAL_UART_Receive()
(blocking)HAL_UART_Receive_IT()
(interrupt)HAL_UART_Receive_DMA()
(non-blocking, fast)
Example:
c
uint8_t rxData[10];
HAL_UART_Receive_IT(&huart1, rxData, sizeof(rxData));
In USARTx_IRQHandler()
, handle parsing of response frames.
5. Popular UART Display Modules
Display Type | Description |
Nextion | Smart UART TFT display, programmable via GUI, easy command system |
ST7920 UART adapter | Used with monochrome displays (requires byte-level commands) |
TJC Screens | Chinese versions of Nextion |
Custom microcontroller-based displays | Accept custom command protocols |
Advantages of UART Screens
No need to handle raw LCD commands
Easy to interface: just send strings or frames
Offloads rendering work to the display
Common Pitfalls
Wrong baud rate or voltage level mismatch
Incomplete commands (missing end markers like
\xff\xff\xff
)Ignoring UART buffer overflows when receiving fast data
Bonus: Debugging Tips
Use a USB-to-Serial adapter and terminal (e.g., PuTTY) to test screen commands.
Connect logic analyzer (e.g., Saleae) to monitor TX/RX signals.
Add delays between commands if the screen is slow to respond.
Subscribe to my newsletter
Read articles from ampheo directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
