How STM32 interacts with serial port screens?

ampheoampheo
3 min read

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 PinScreen Pin
TX (e.g., USART1_TX)RX (on the screen)
RX (e.g., USART1_RX)TX (on the screen)
GNDGND
5V/3.3VVCC (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:

  1. Enable USART1 (or any USART).

  2. Set:

    • Baud Rate (e.g., 9600 or 115200)

    • 8 data bits, no parity, 1 stop bit (default)

  3. Generate code.

  4. 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.


Display TypeDescription
NextionSmart UART TFT display, programmable via GUI, easy command system
ST7920 UART adapterUsed with monochrome displays (requires byte-level commands)
TJC ScreensChinese versions of Nextion
Custom microcontroller-based displaysAccept 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.

0
Subscribe to my newsletter

Read articles from ampheo directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

ampheo
ampheo