Go Down

### Topic: ds18b20 (Read 1 time)previous topic - next topic

#### kosmart2

##### Mar 15, 2011, 08:36 pm
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];
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.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();

for ( i = 0; i < 9; i++) {                  // we need 9 bytes
}

LowByte = data[0];
HighByte = data[1];
TReading = (HighByte << + 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] << + 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");
}

#### WillR

#1
##### Mar 15, 2011, 09:20 pm
Look at the Mile Burton stuff.

Saves a lot of time.

#### MarkT

#2
##### Mar 16, 2011, 12:07 am
Mecury thermometers can be wrong!  Measure the temperature of melting ice if you want to callibrate it.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

#### kosmart2

#3
##### Mar 16, 2011, 02:14 pm
Thanks for your advice. It measures correctly...thank you;-)

Go Up