Skip to content

STM32 Nextion Screen Led Toggle

In this tutorial we will learn how does Nextion HMI Screen communicates with STM32 series boards. We will use STM32cubeIDE, stm32f10c8 board called blue pill, and nextion 2.4-inch basic screen.

I will use UART2 on Stm32f103c8. Let configure UART2 for Nextion.Github page about that aplication

 You can use any STM32 series board with the UART line on it. Don’t forget to change the connection schematic according to the board you have.

This tutorial we will make a basic application. Open and close a led on the board using a button on the screen.

We will not use any library to communicate nextion HMI. We will use nextion communication protocol. You can access instruction created by Itead with that link. All instructions are end with three-byte “0xff 0xff 0xff”. That means if you sent or receive you have to add 3 times 0xff end of the data

I will use UART2 on Stm32f103c8. Let configure UART2 for Nextion. Don’t forget the current bound rate of the nextion is 9600.Set mode asynchronous to use Usart as UART .

If you don’t know how you can create a new project on STM32CubeIDE you can see how can you do that on here.

When your configuration over, you can see which pins are using on microcontroller UART. For stm32f10c8  A3 and A2 pins are using as A3 -> USART2_Tx and A2 -> USART2_Rx . 

 After Uart configuration make set PC13 output to use led on the stm32f10c board(bluepill) if you use another board set the pin output  which is connected to led on your board.

After generate codes we are ready to programming. First we will add buffer to store received data.

uint8_t rx_buff[7];      //7 is max data length.

Configuration in main function :

//Enable Read data register not empty
// led state default off
//Receives an amount of data in non blocking mode.

If you want to know more about hal library Uart function you can see all in here. More about HAL_UART_Recieve_IT on documentation as given below ;

One another and last function we will use is HAL_UART_RxCpltCallback function. This function run every Rx Transfer completed so it is Rx Transfer completed callbacks function. 

More about that function in hal library user manual pdf as given bellow.

After all information we will add HAL_UART_Recive_IT() function into the HAL_UART_RxCpltCallback() function. With this way we will receive data continuously when data sends.

HAL_UART_RxCpltCallback(UART_handleTypeDef *huart){      

Everything’s ready to receive data, now we will check what should we receive. When we create a button on Nextion Editor and simulate it, we can see what will Nextion HMI Screen transmit.

We can see when  press the button Nextion HMI Screen sent us 0x65 0x00 0x01 0x01 0xFF 0xFF 0xFF.

We know the last 3 0xFF  are ending bytes. 0x65 says us that the data packet is touch event return data. This data packet format looks like under bellow.

0X65+Page ID+Component ID+TouchEvent+End

If we compare this package format with ours, we can see Page ID= 0, Component ID=1, TouchEvent=1. TouchEvent 1 means button pressed. When the button is released, it becomes zero.

Now all we have to do is check the buffer equals to that sending data packet in a while loop.

if(rx_buff[0] == 0x65 && rx_buff[4] == 0xFF &&
rx_buff[5] == 0xFF && rx_buff[6] == 0xFF){


rx_buff[0]=0x00;//clear buff

That’s all I have to say about led toggling with STM32 using Nextion. It was a little bit long but I wanted to be clear on this subject. Next We will create a nextion library to use. You can access all in my GitHub account from the link below.

Github page about this

References :

Published inSTM32 examples

One Comment

  1. Architect The Coder Architect The Coder

    Explained well. Thank you for sharing.

Leave a Reply

Your email address will not be published. Required fields are marked *