Turning a mlx90614 temp sensor value to a float

Hi ive been trying to get a value off of my temp sensor (mlx90614) and turn it into a float to display on a Nextion screen. I am a big noob with programming and after spending hours on forums and youtube I am still clueless as to how I can get it into a useable value.

with this code I can read the temp on the serial monitor but it has 3 question marks after the value. e.g. n0.val=14.85⸮⸮⸮
and nothing will change on my display it only shows the number I put in to start from on the nextion software.

#include <Wire.h>
#include <Adafruit_MLX90614.h>
Adafruit_MLX90614 mlx = Adafruit_MLX90614();






void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
  Serial.println("Adafruit MLX90614 test");  
  mlx.begin();  
  
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(1000);
 Serial.print("n0.val=");
 Serial.print(mlx.readObjectTempC());
 Serial.write(0xff);
 Serial.write(0xff);
 Serial.write(0xff);
 Serial.println();
  
}

The "reverse question marks" come from these lines. They make no sense, so why are they there?

 Serial.write(0xff);
 Serial.write(0xff);
 Serial.write(0xff);

This will work, but the "(float)" cast is not needed if mlx.readObjectTempC() returns a float value. Check the library docs to make sure.

 Serial.print("n0.val=");
 Serial.println( (float) mlx.readObjectTempC());

nothing will change on my display it only shows the number I put in to start from on the nextion software.

What is the "nextion software"? The program you posted just prints to the serial monitor.

I don’t know why the “serial.write(0xff)” is needed I was watching a tutorial and the bloke said you need to write it 3 times for it to show on the nextion screen.

The nextion screen is connected to the i2c ports the same as the sensor.

There is nothing in the posted code to deal with the nextion screen, so it is just an inactive bystander.

Here is a getting started guide for the nextion screen: Using Nextion displays with Arduino

According to that tutorial, the display is connected to serial TX and RX, not I2C.

Hello @smellypanda

Have you seen my tutorial: Using Nextion displays with Arduino ?
I suggest you start there.

The Nextion does not use I2C it uses serial. You have to connect to a serial port.

It tells the Nextion that the data you sent to it is complete and can be processed. Without it the Nextion will not do anything with the data you send to it.

Please work through my tutorial and come back if you have problems.

Ideally you need an Arduino with a spare serial port, for example a Nano Every or a Mega. Using the same port as is used for the serial monitor (for example a Uno) is possible but will make things more difficult than they need to be.

okay so my nextion is now plugged into tx and rx following your tutorial however now i am getting error code:
"avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x00"
when trying to upload and the nextion is plugged in... if I take it out then it uploads fine. but could this mean there is a problem with the communication between them?

also with regards to the serial print screen saying the 0xff as backwards question marks... will it always show as the question marks even when its working and communicating with the nextion?

i am on an uno at the minute because my mega is in a project that cant be removed right now is there something that needs to be done on the uno to allow it communicate as it uses pins 0 and 1.

Yes, there is procedure.

  1. Upload your program to the Uno.
  2. turn all power off to the Uno and disconnect ANY AND ALL connections to the PC, including the USB.
  3. connect up pins 0 and 1 as you wish.
  4. Power up the Uno and you are ready to use the serial port as you wish.

ahaa thank you. now to get the hmi to actually display the values :\

1 Like

The difficulties you are having with using the Uno are the reason I suggested using a board with a spare serial port. With a Uno you are trying to use its only serial port for 2 different things.

Alright then I’ll order a nano and try again next week. Thanks

NOT a Nano, a Nano is a Uno in a different form factor (shape), it does not have an additional serial port. A Nano Every is a very different board and has an additional serial port you can use.

Ah okay. So with the every what serial port do I use?
Would it be “Serial1.print()” like in your tutorial?

Yes, correct.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.