Start a new topic

printing a number with nexText from Arduino

I know that this subject has already been discussed several times but I'm getting confused ( and a little bit frustrated after many hours without results) about the way to use a NexText object to print a float value.

I figured out that, using Arduino, I have to:

- convert the float to a string

- assign the string to a buffer

- put the buffer in the setText(buffer) function

If this is correct it's not clear to me how exactly to do it.

Thanks for any help

How does state you must convert float to ascii?

You just send it to print ;-)

Although the default is to 2 decimal places, you can increase if you add the second parameter in the print statement like so.... 

float a = 10.254;
float b = 235.6987;
float c = 0.001473;

void setup() {
Serial.print ("My float 'a' = ");
Serial.println (a, 3);
Serial.print ("My float 'b' = ");
Serial.println (b, 4);
Serial.print ("My float 'c' = ");
Serial.println (c, 6);

void loop() {

 Gives this in your Serial Monitor... 

My float 'a' = 10.254
My float 'b' = 235.6987
My float 'c' = 0.001473 


Two methods of formatting to print value 'c' to a Nextion text box 't0' are.... 

float c = 0.001473;
char ter [4] = {0xff,0xff,0xff,0x00};

void setup() {


Serial.print(c, 6);

Serial.printf("t0.txt=\"%.6f\"%s", c, ter);

void loop() {

 Will output....... 


 in both cases, and you don't need a library to do this ;-) 

Thanks for the methods indev2.

I just used what is in line 9,10,11 of the second one and it worked properly.

I'm wondering, just for curiosity, if there is a function in the library that does the same thing.

Just to keep the code in the same style.

In any case thanks a lot for your help.

Login or Signup to post a comment