Printing D18B20 output to lcd to 2 decimal places

I've done a fair bit of searching but can't find a decent method of taking the output from a onewire temp sensor (device says D18B20) and printing it to 2 decimal places on an LCD I currently read the sensor as an integer and use itoa to give me a char to print:

  sensors.requestTemperatures();
  temp4 = sensors.getTempCByIndex(0);
  itoa(temp4, temp, 10);
  lcd.writeString(55, 0, temp, MENU_NORMAL);

any ideas?

Code snippets lead to lousy answers, generally.

What is the type for temp4? If it is int, you can't expect to print an integer to 2 decimal places. If it is NOT int, why are you using itoa (integer to ascii) to convert it to a string?

Sorry just thought i'd post the relevant section.

It is an int currently, which is then being converted and displayed correctly on my lcd.

However i know the sensor is capable of better resolution, i just need to get it to my display somehow

However i know the sensor is capable of better resolution, i just need to get it to my display somehow

You need to match variable types with function output. If sensors.getTempCByIndex() returns an int, you won't get two decimal places of accuracy. If sensors.getTempCByIndex() returns a float, you won't get anywhere truncating that value by storing it in an int.

There are several ways to convert a float to an array of characters. Whether that is even necessary depends on what the lcd object is. If it (correctly) derives (directly or indirectly) from the Print class, it is capable of printing ints and floats directly.

Without seeing more of you code, all we can offer is encouragement. Good luck.

The code that I am using does, I think, treat the value as a ‘float’ ?

// This Arduino sketch reads DS18B20 "1-Wire" digital
// temperature sensors.
// Tutorial:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html

#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

DeviceAddress insideThermometer = { 0x28, 0xCC, 0xD1, 0xE1, 0x02, 0x00, 0x00, 0xE6 };
//DeviceAddress outsideThermometer = { 0x28, 0x6B, 0xDF, 0xDF, 0x02, 0x00, 0x00, 0xC0 };

void setup(void)
{
  // start serial port
  Serial.begin(9600);
  // Start up the library
  sensors.begin();
  // set the resolution to 10 bit (good enough?)
  sensors.setResolution(insideThermometer, 11);
//  sensors.setResolution(outsideThermometer, 10);
}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  } else {
    Serial.print("C: ");
    Serial.print(tempC);
    Serial.print(" F: ");
    Serial.print(DallasTemperature::toFahrenheit(tempC));
  }
}

void loop(void)
{ 
  delay(2000);
  Serial.print("Getting temperatures...\n\r");
  sensors.requestTemperatures();
  
  Serial.print("Inside temperature is: ");
  printTemperature(insideThermometer);
  Serial.print("\n\r");
}

Yes i was under the impression (i think i even tested it earlier) that i could use float instead of int. but it didn’t display any decimal places on my lcd (just 22.00, 23.00, 24.00…)

so more specifically i guess i need to know to display floats correctly on my particular lcd (the nuelectronics 3310 shield)

more searching needed…

more searching needed....

Or you could post your code...

Greenbeast: Yes i was under the impression (i think i even tested it earlier) that i could use float instead of int. but it didn't display any decimal places on my lcd (just 22.00, 23.00, 24.00...)

What resolution are you using ?

look for something like : sensors.setResolution ....

from the ds18b20 data sheet : The resolution of the temperature sensor is user-configurable to 9, 10, 11, or 12 bits, corresponding to increments of 0.5C, 0.25C, 0.125C, and 0.0625C, respectively.

PaulS:

more searching needed....

Or you could post your code...

Ok i will, just gonna try and tidy it up and have a tinker while i'm at it

DaveO:

Greenbeast: Yes i was under the impression (i think i even tested it earlier) that i could use float instead of int. but it didn't display any decimal places on my lcd (just 22.00, 23.00, 24.00...)

What resolution are you using ?

look for something like : sensors.setResolution ....

from the ds18b20 data sheet : The resolution of the temperature sensor is user-configurable to 9, 10, 11, or 12 bits, corresponding to increments of 0.5C, 0.25C, 0.125C, and 0.0625C, respectively.

not sure currently, i'll have a look while i'm doing the aforementioned tidy up

here’s the code:

#include <OneWire.h>

#include <DallasTemperature.h>


#include <nokia_3310_lcd.h>

// Temp/humidity display using nokia 3310 LCD display shield from nuelectronics.com

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

  int temp1, temp2, temp3, temp4, temp5;
  char temp[3];


Nokia_3310_lcd lcd=Nokia_3310_lcd();

void setup()
{
  sensors.begin();
  lcd.init();
  lcd.clear();
}

void loop() {
  // Display non changing text, there is a slight delay while first reading is taken


      
      //Request and display temperatures
      sensors.requestTemperatures();
      temp1 = sensors.getTempCByIndex(0);
     
      
      itoa(temp1, temp, 10);
      lcd.writeString(30, 0, temp, MENU_NORMAL);
}

 //Timer2 interrupt routine -
 //1/(160000000/256/(256-6)) = 4ms interval

ISR(TIMER2_OVF_vect) {  
  TCNT2  = 6;
  //update_adc_key();
}

Think i’ve sorted it using dtostrf

#include <OneWire.h>

#include <DallasTemperature.h>


#include <nokia_3310_lcd.h>

// Temp/humidity display using nokia 3310 LCD display shield from nuelectronics.com

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
  float temp1;
  int temp2, temp3, temp4, temp5;
  char temp[3];


Nokia_3310_lcd lcd=Nokia_3310_lcd();

void setup()
{
  sensors.begin();
  lcd.init();
  lcd.clear();
}

void loop() {
  // Display non changing text, there is a slight delay while first reading is taken

      byte precision = 1;
      char floatBuffer[20];
      
      //Request and display temperatures
      sensors.requestTemperatures();
      temp1 = sensors.getTempCByIndex(0);
     dtostrf(temp1, precision+3, precision, floatBuffer);
     lcd.writeString(30, 2, floatBuffer, MENU_NORMAL);
     
}

 //Timer2 interrupt routine -
 //1/(160000000/256/(256-6)) = 4ms interval

ISR(TIMER2_OVF_vect) {  
  TCNT2  = 6;
  //update_adc_key();
}

Thanks