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