Please update OneWire page for DS18B20

Hi,

I recently acquired a couple of DS18B20 temperature sensor chips from Sparkfun, in the hope of building a simple temperature logger. In getting them to work on my Arduino, I found that the sample code on the OneWire wiki page fails for this chip, because the family code is this chip is 0x28, rather than 0x10. Apart from this, the old sample code seems to work fine for the new chip.

Also, the link to the data sheet for the DS1820 on that page is broken, because the DS1820 has been completely replaced by the DS18S20 chip, now. The URL for the DS18S20 datasheet is now http://datasheets.maxim-ic.com/en/ds/DS18S20.pdf The data sheet for the DS18B20 chip available from Sparkfun is http://datasheets.maxim-ic.com/en/ds/DS18B20.pdf.

I wanted to ask that you update the wiki page to reflect these changes. Thanks!

The wiki is publicly editable, so feel free to create an account and update the pages yourself. If you don't want to change the code, you could always just add a note about working with these newer chips.

The wiki is publicly editable, so feel free to create an account and update the pages yourself.

Thanks, I will. (I'm not sure why I had the mistaken impression that editing privileges were restricted...)

A few days ago I got an Arduino BT and a DS18B20 Thermometer. I used Jim Studt's code with TomP's insight that the family name is 0x28 to get the thermometer working (Thanks guys!). The BT code controls the thermometer and records the temperature and transmits it to the computer via bluetooth. The bluetooth dongle on my computer shows up as com port 10. The results show up as a scrolling display in the Arduino Alpha software. However, I would like to save the results in Microsoft Access or Excel. How can I configure those applications to see and record the data stream coming into the bluetooth com port?

Thank you,

David

1-Wire fans:

There's more for you at...

http://www.phanderson.com/arduino/ds18b20_1.html
(Accessing 1-Wire chips with Arduino)

and

(Stuff about 1-Wire in general... no Arduino SPECIFIC stuff... but specific Arduino usable info abounds)

Acknowledgements:

The first I have no association with, I just know the source is reliable.

The second is mine.

So I am very excited because I plugged a DS18B20 into my arduino and it worked. It is happily spitting out these values.

R=28 9E 91 8C 1 0 0 A6 P=1 A2 1 4B 46 7F FF E 10 CRC=D8

I know next to nothing about programming and got this to work by simply copying and pasting the code from this page Arduino Playground - OneWire and changing the family code to 0X28 and adding the one wire library to the arduino programming environment. The next thing I would like to know is how to write the code to have the arduino convert the hex data above to a readable temperature format (i.e. 35 degrees C or something.....better yet....a farenheit conversion)

So I am very excited because I plugged a DS18B20 into my arduino and it worked. It is happily spitting out these values.

R=28 9E 91 8C 1 0 0 A6 P=1 A2 1 4B 46 7F FF E 10 CRC=D8

I know next to nothing about programming and got this to work by simply copying and pasting the code from this page Arduino Playground - OneWire and changing the family code to 0X28 and adding the one wire library to the arduino programming environment. The next thing I would like to know is how to write the code to have the arduino convert the hex data above to a readable temperature format (i.e. 35 degrees C or something.....better yet....a farenheit conversion)

Okay I'm starting to figure out the format of the data. The first two HEX numbers after the P=1 is the LSByte and MSbyte respectively. So that gives us 1A2 or 418 and converting to Celsius means 418*.0625 = 26.125 degrees celsius. So the question is how do I write the C code to first pull out those two bytes, switch them around from LSB MSB to MSB LSB and then multiply that number by .0625?

So the question is how do I write the C code to first pull out those two bytes, switch them around from LSB MSB to MSB LSB and then multiply that number by .0625?

In AVR C, an 'int' is a two-byte integer, and so you can combine the MSB and LSB of the temperature value into an 'int' value by saying

int rawtemp = (data[1] << 8) + data[0];

That's it - 'rawtemp' is now the signed temperature, multiplied by 16 (for the DS18B20). To get floating point fahrenheit and centigrade temperatures, you would say

double tempc, tempf;
tempc = (double)rawtemp / 16.0;
tempf = (tempc * 1.8) + 32.0;

Excellent. That worked great. I did run into the problem of Serial.print dropping off everything after the decimal. Is there a command similar to Serial.print that doesn't truncate the number? I put in a few extra lines of code to increase the precision of the number I was seeing but of course there is no decimal.

double tempc, tempf;
tempc = (double)rawtemp / 16;
tempc = tempc * 1000;
tempf = (((tempc / 1000) * 1.8) + 32.0) * 1000;
Serial.print("tempc=");
Serial.print(tempc,DEC);
Serial.print(" tempf=");
Serial.print(tempf,DEC);

Now this is the output of my two DS18B20's

tempc=22875 tempf=73175 R=28 9E 91 8C 1 0 0 A6 P=1 6E 1 4B 46 7F FF 2 10 CRC=71

tempc=22875 tempf=73175 R=28 36 71 8C 1 0 0 81 P=1 6E 1 4B 46 7F FF 2 10 CRC=71

Is there a command similar to Serial.print that doesn't truncate the number?

Not that I'm aware of, but I'm not an expert. What I did when I first wanted to get nicely formatted temperatures output to the console was the following...

  int temp_raw = (data[1] << 8) + data[0];
  if (temp_raw > 0) {
    temp_int = temp_raw >> 4;
    temp_frac = temp_raw & 0x0F;
  }
  else {
    int temp_abs = -temp_raw;
    temp_int = - (temp_abs >> 4);
    temp_frac = temp_raw & 0x0F;
  }
  tempc = (double)temp_raw / 16.0;
  tempf = (tempc * 1.8) + 32.0;

  int tempf_int, tempf_tenths;
  tempf_int = int(tempf);
  if (tempf > 0) {
    tempf_tenths = tempf*10 - tempf_int*10;
  } 
  else {
    tempf_tenths = tempf*10 + tempf_int*10;
  }

  Serial.print(count);
  Serial.print("  Temperature = ");
  Serial.print(temp_int);
  Serial.print(" ");
  Serial.print(temp_frac);
  Serial.print("/16 C (");

  Serial.print(tempf_int);
  Serial.print(".");
  Serial.print(tempf_tenths);
  Serial.println(" F)");

It's not elegant, but it gets the job done. For my temperature logger, I leave the temperature as a scaled integer on the Arduino and convert it to deg Fahrenheit only after it's been uploaded to my laptop.

Okay I think I understand what most of those lines of code do but can you explain what this line is saying?

temp_frac = temp_raw & 0x0F;

You also have this line but I don't see an integer called "count".

Serial.print(count);

Does this have something to do with the fact that you are using it as a temperature logger? As in you have an integer called count somewhere in there that increments +1 every iteration of the program to keep track of the temperatures?

Sorry for all of the questions. I am very green when it comes to any kind of programming but I am catching on slowly.

Okay I think I understand what most of those lines of code do but can you explain what this line is saying?

temp_frac = temp_raw & 0x0F;

This copies the least-significant four bits of temp_raw to temp_frac. The '&' operator does a bitwise AND between its two arguments, and 0x0F is the hexadecimal for 00001111.

You also have this line but I don't see an integer called "count".

Serial.print(count);

Does this have something to do with the fact that you are using it as a temperature logger? As in you have an integer called count somewhere in there that increments +1 every iteration of the program to keep track of the temperatures?

Sorry about that! I didn't notice I had left that in there. Yes, this is the count of the number of temperature readings I've taken.

Sorry for all of the questions. I am very green when it comes to any kind of programming but I am catching on slowly.

You never need to apologize for asking questions. The only way to learn is to try stuff out and ask questions when you run into problems.

temp_frac = temp_raw & 0x0F;

I must say.....that is a really slick way to pull out just the bits that you want and throw out the rest. I appreciate this.

Having fun learning to use the DS18B20. It looks like it can take a while to read the temperature via the library. A second or more and the Arduino can't do anything else during this time. I guess the library calls delay().

I think I'll see if there is a way to read the data in a non-blocking manner. I think most of the time is just holding the line high to provide power in parasitic mode, other things could be done during that time.

I'd like to read the temperature fairly often, but still service 5 buttons.

It looks like it can take a while to read the temperature via the library. A second or more and the Arduino can't do anything else during this time. I guess the library calls delay().

The reason it takes so long is that you need to give the DS18B20 750ms to take a temperature reading. The commands to start that process and to actually read the temperature from the device are fairly quick. Luckily for you, this delay is executed in user code (not in the OneWire library functions), so it should be easy for you to do other things in that period; just be sure not to try to read the temperature before the chip has been given enough time to record it.

If you have any more detailed questions about how to do this, just post your code here and I'll try to help.

Hello, im new here! thanks for all the inforrmation. can anyone please help me make the VB6 code for the pc thermometer ds1820. Thank you very much for your reply..

Hi i did a web server showing all the DS18B20 attach and printint the temperature on a web page.
If anyone wants it!
Actually i just grab some code here and there (on this site) and match every thing so it work for my application

/*
 * Multiport Web Server
 *
 * A simple web server that shows
 * the value of the analog and digital input pins.
 *
 * port 80: human readable page
 * port 8080: machine readable page (csv format)
 */
#include <OneWire.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 123 };
//byte gateway[] = { 192, 168, 0, 254 };
//byte subnet[] = { 255, 255, 255, 0 };

Server server1(80); // server for human beings
Server server2(8080); // server for machines
// DS18S20 Temperature chip i/o

OneWire ds(9);  // on pin 10

void setup()
{
  for (int i = 2 ; i < 8; i++){
    pinMode(i, INPUT);
    //digitalWrite(i, HIGH);
  }
  //Ethernet.begin(mac, ip, gateway, subnet);
  Ethernet.begin(mac, ip);
  server1.begin();
  server2.begin();
}

void loop()
{
  byte i;
  byte present = 0;
  byte data[12];
  byte addr[8];


  Client client1 = server1.available();
  Client client2 = server2.available();
  if (client1) {
    boolean current_line_is_blank = true;
    while (client1.connected()) {
      if (client1.available()) {
        char c = client1.read();
        if (c == '\n' && current_line_is_blank) {
          client1.println("HTTP/1.1 200 OK");
          client1.println("Content-Type: text/html");
          client1.println();

          
//            if ( !ds.search(addr)) {
//              // No more adresses
//              ds.reset_search();
//            }
            
            //else {
            while (ds.search(addr)) {
              client1.print("A=");
              for( i = 0; i < 8; i++) {
                client1.print(addr[i], HEX);
                client1.print(" ");
              }

              if ( OneWire::crc8( addr, 7) != addr[7]) {
                client1.print("CRC is not valid!\n");
                client1.println("
");

                //      return;
              }

              if ( addr[0] != 0x28) {
                client1.print("Device is not a DS18S20 family device.\n");
                client1.println("
");

                //      return;
              }

              ds.reset();
              ds.select(addr);
              ds.write(0x44,1);         // start conversion, with parasite power on at the end

              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[i] = ds.read();
              }
              int rawtemp = (data[1] << 8) + data[0];
              int tempca = rawtemp / 16.0;
              int tempcb = rawtemp % 16;

              client1.print(" Celcius = ");
              client1.print(tempca);
              client1.print(".");
              client1.print(tempcb);
              client1.println("
");
            }
            // No more device then reset the search
            ds.reset_search();




/*
          for (int i = 0; i < 6; i++) {
            client1.print("analog input ");
            client1.print(i);
            client1.print(" is ");
            client1.print(analogRead(i));
            client1.println("
");
          }
          for (int i = 2; i < 8; i++) {
            client1.print("digital input ");
            client1.print(i);
            client1.print(" is ");
            if (digitalRead(i)) {
              client1.print("HIGH");
            } else {
              client1.print("LOW");
            }
            client1.println("
");
          }
*/
          break;
        }
        if (c == '\n') {
          current_line_is_blank = true;
        } else if (c != '\r') {
          current_line_is_blank = false;
        }

      }
    }
  
    delay(10);
    client1.stop();
  }
  
  if (client2) {
    boolean current_line_is_blank = true;
    while (client2.connected()) {
      if (client2.available()) {
        char c = client2.read();
        if (c == '\n' && current_line_is_blank) {
          client2.println("HTTP/1.1 200 OK");
          client2.println("Content-Type: text/html");
          client2.println();
          for (int i = 0; i < 6; i++) {
            if (i != 0)
              client2.print(",");
            client2.print(analogRead(i));
          }
          for (int i = 2; i < 8; i++) {
            client2.print(",");
            client2.print(digitalRead(i));
          }
          break;
        }
        if (c == '\n') {
          current_line_is_blank = true;
        } else if (c != '\r') {
          current_line_is_blank = false;
        }
      }
    }
    delay(10);
    client2.stop();
  }

}

Can you get this to work in Flash??

where do i get the onewire.h?

Onewire.h is included with the ide.

Just use:
#include <OneWire.h>

As per Patgadget's example.

Gordon