Go Down

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


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

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)) {
    return ;

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

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

  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.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("Teplota: ");
  if (Fract < 10)
  Serial.print(" Temp --> ");


Look at the Mile Burton stuff.

Saves a lot of time.
Just another Hacker


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 ]


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

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131