ds18b20

Hi!
I am making a new thermometer with LCD display and communication to computer. And I was wandering if you can help me. I took some parts of the code from arduino pages, made some edits in it and uploaded on my processor (ATmega328). But there have to be some problem. It measures temperature but not correctly. It’s about 2 degree of celsius. Why it’s about 2 degrees? Because i measure temperature in my room. On arduino says 23,62 °C on mercury thermometer says about 21,5 °C let say 21,62. Can you please take a look on my code? I simply don’t know what am I doing wrong. I am using external powering of ds18b20, there is 4k7 ohm resistor on the bus. I know that I can simply take the temperature down by mathematics. But i want to understand the problem. Thanks everyone for your time.

There is my code:

#include <OneWire.h>

// DS18S20 Temperature chip i/o
OneWire ds(10); // on pin 10

void setup(void) {
// initialize inputs/outputs
// start serial port
Serial.begin(9600);
}

void loop(void) {
byte i;
byte present = 0;
byte data[12];
byte addr[8];
int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;
float temp;

if ( !ds.search(addr)) {
ds.reset_search();
return ;
}

for( i = 0; i < 8; i++) {
}

if ( OneWire::crc8( addr, 7) != addr[7]) {
return ;
}

ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion

delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.

present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad

for ( i = 0; i < 9; i++) { // we need 9 bytes
data = ds.read();

  • }*

  • LowByte = data[0];*

  • HighByte = data[1];*

  • TReading = (HighByte << 8) + LowByte;*

  • SignBit = TReading & 0x8000; // test most sig bit*

  • if (SignBit) // negative*

  • {*

  • TReading = (TReading ^ 0xffff) + 1; // 2’s comp*

  • }*
    Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25

  • Whole = Tc_100 / 100; // separate off the whole and fractional portions*

  • Fract = Tc_100 % 100;*
    _ temp = ((data[1] << 8) + data[0])*0.0625;_

  • if (SignBit) // If its negative*

  • {*

  • Serial.print("-");*

  • }*

  • Serial.print("Teplota: ");*

  • Serial.print(Whole);*

  • Serial.print(".");*

  • if (Fract < 10)*

  • {*

  • Serial.print(“0”);*

  • }*

  • Serial.print(Fract);*

  • Serial.print(" Temp → ");*

  • Serial.print(temp);*

  • Serial.print("\n");*
    }

Look at the Mile Burton stuff.
http://download.milesburton.com/Arduino/MaximTemperature/DallasTemperature_360.zip

Saves a lot of time.

Mecury thermometers can be wrong! Measure the temperature of melting ice if you want to callibrate it.

Thanks for your advice. It measures correctly...thank you;-)