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  https://github.com/mertwhocodes/STM32-Nextion-Screen-Led-Toggle

 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 :

1
2
3
4
5
__HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);
//Enable Read data register not empty
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,0);  
// led state default off
HAL_UART_Recieve_IT(&huart2,(uint8_t*)rx_buff,7);
//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.

1
2
3
4
5
HAL_UART_RxCpltCallback(UART_handleTypeDef *huart){      
       
HAL_UART_Receive_IT(&huart2),(uint8_t*)rx_buff,7);  
 
}                    
 

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.

 
 
 
1
2
3
4
5
6
7
8
9
10
11
 
while(1){      
if(rx_buff[0] == 0x65 && rx_buff[4] == 0xFF &&
rx_buff[5] == 0xFF && rx_buff[6] == 0xFF){

HAL_GPIO_TogglePin(
GPIOC,GPIO_PIN_13);

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  https://github.com/mertwhocodes/STM32-Nextion-Screen-Led-Toggle

References :

https://www.st.com/resource/en/user_manual/dm00105879-description-of-stm32f4-hal-and-ll-drivers-stmicroelectronics.pdf

https://server4.eca.ir/eshop/000/HMI(3011002027-28)/Nextion%20Instruction%20Set%20-%20ITEAD%20Wiki.pdf

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 *