Nextion gauge and DS18B20 temperature sensor with Arduino mega

I want to show temperature value (celsius) in Nextion gauge (Temp value between 0-120 celsius).
Is it possible? because i have using DS18B20 Digital temperature sensor.If yes,how to map the gauge value to sensor value.

I have using Nextion advanced 2.8" display and Arduino mega.

Please advise..

Do you know how to display something on the display ?

Have you looked at the Arduino map() function ?
See map() - Arduino Reference

1 Like

I have make one program for showing the potentio meter value (pressure value in bar) in Nextion gauge as well as digital.its working.
But in case of temperature value using DS18B20 temperature sensor i am confused..

Start simple

What library are you using to read the DS18B20 temperature sensor ?

Dallas Temperature library

Can you print the temperature to the Serial monitor ?

If you edit the title, then you can change the section of this forum. I think it should be in the "Displays" section, unless it is just a question about map().

The Nextion Arduino library has many shortcomings. That is why there are so many alternatives, which also have shortcomings. The best option is to start here:

Then you have to find an example for a Gauge.
Perhaps it is something like this:

int t = 80;

Which values needs to be mapped to which values ?
The DallasTemperature library returns a floating point number, you can cast that to a integer and then use the map().

I have sending two data to Nextion Display one for Gauge (z0) and other one to digital (x0)...
digital value (x0) printing in Serial monitor as well as Nextion according to temperature variation.
But in case of Gauge value (map value) printing erratically in Nextion as well as serial monitor.

Please post your sketch, using code tags when you do

1 Like
#include <Nextion.h>
#include <OneWire.h>/////////////////////Temp sensor
#include <DallasTemperature.h>///////////Temp sensor
#define ONE_WIRE_BUS A0//////////////////Temp sensor
OneWire oneWire(ONE_WIRE_BUS);//////////Temp sensor
DallasTemperature sensors(&oneWire);////Temp sensor
float temp;

void setup()
void loop() 
  temp = sensors.getTempCByIndex(0);
  int sensor4 = map(temp, 0, 1023, 329, 569);///////////////////map the value for gauge //display
  sensor4 = constrain(sensor4, 329, 569);
  Serial.print("va0.val=");////////////////gauge display
  long xVal = temp * 100;/////////////digital display


Nextion Dusplay side code


![nextion display|690x332](upload://mBZ4yc2Easnu7BpUdvGVkIAdCup.jpeg)
![Nextn display|690x332](upload://iIJd2egDO2qGAgE6rLZs9yXCgWu.jpeg)

I need to map the sensor temperature input with my Nextion gauge value that is 329 to 569.

int sensor4 = map(temp, 0, 1023, 329, 569);
What range of temperatures are you likely to get ?
Is it really from 0 to 1023 ?

What do you see when you print the value of temp to the Serial monitor ?
Why do you need to map the temperature to between 329 and 569 ?

#i want to read temp range 40-120 degree Celsius
#In serial monitor shows the value of present temperature-temp-34.56.
#329-is initial position of needle in Nextion gauge and 569 is the last point of needle (max-range-120)

What do you want to display if the temperature is not in that range ?

Gauge needle start to move once temperature reaches 40 upto 120 degree Celsius ,if the temp is out of max range, gauge needle will stop on max reading.

why do you use A0?
Tempsensor is a digital thing

How about something like this

void showTemp()
  if (temp < 40)
    output = 329;
  else if (temp > 120)
    output = 569;
    output = map(temp, 40, 120, 329, 569);

NOTE that map() only works with integers

Thank you
Now its working