In this part we will see how to to display analog values on a wave on the Nextion TFT display. This is a very simpel example. As before, we read the potentiometer value and send the value to the wave in the TFT interface. You have the schematic, code and tft file below.
Ok, first of all, for this first example we need the schematic below. But only make the connections after we uplaod the code since the display uses the RX and TX pins and if something is connected to those pins, we can't uplaod the code. But we need the Arduino code part but also the Nextion TFT interface. For that we will use Nextion editor and create our interface.
Downlaod from below the Nextion editor software and install it. Then run the aplication and create a new file. Once again select the type of display and the horizonal mode. Then add a wave to the screen and remember the id of this label, in my case is id = 1. That's important sice we need this id later in the code.
Ok, so I've placed the wave on the screen and made it 240x240 and fixed trhe id as id=1. To uplaod the TFT file we have to compile the project. If we have no errors in the console, we go to File and select open build folder anc copy the tft file. Insert a micro SD card into your PC and copy the tft file there. The SD card must be empty and formated to a fat32 format.
Now that the tft file is on the SD card make sure the Nextion display is not powered on. Insert the SD card into the card slot. Now power up the display with 5V. You will see on the screen that the new tft interface is being uploaded. Once you see 100%, power off the display and remove the SD card. Now power back the display and connect the TX and RX pins as in the schematic and add a potentiometer to pin A0.
Copy the code from below or just download it. Upload the code to the arduino before you connect the TX and RX pins. After you uplaod the code, connect the UART TX and RX pins and power up the Arduino and display. Add the potentiometer. As you can see in the code we read the analog value and send the "add " text. Then se send the id of the wave, in this case id=1, then the channel, in this case channel=0 and then the analog value. That will refresh the value on the display wave.
int potentiometer_pin = A0; //Define the analog input pin
void setup() {
Serial.begin(9600); //Default baud rate of the Nextion TFT is 9600
pinMode(potentiometer_pin,INPUT); //Define pin as input
}
void loop() {
int Value = map(analogRead(potentiometer_pin),0,1024,0,255); //Read the pot value ann map it to 0.255 (max value of waveform=255)
String Tosend = "add "; //We send the string "add "
Tosend += 1; //send the id of the block you want to add the value to
Tosend += ",";
Tosend += 0; //Channel of taht id, in this case channel 0 of the waveform
Tosend += ",";
Tosend += Value; //Send the value and 3 full bytes
Serial.print(Tosend);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
Once again, as you can see, after each data send, we also send 3 full bytes (0xFF) in hexadecimal. So we send the "add 1,0," and the voltage analog value. That will update the val value of the wave on the screen and by that graph the value. We need to map the anlog read, since the wave will only accept values from 0 to 255 and the digital read is from 0 to 1024.