Please update OneWire page for DS18B20

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

You need to download and install the OneWire library - it's not included as part of the Arduino distribution. You should download the most recent version of the library from
this page

http://homepage.mac.com/wtpollard/Software/FileSharing7.html

unzip it, and copy the OneWire directory into your Arduino hardware/libraries folder. There's a page on the Arduino playground with some examples of how to use the library.

http://www.arduino.cc/playground/Learning/OneWire

Hi all,

I have a project that is way beyond my capabilites (because they are non-exisitant at this point) using the Adruino with a C#
I'm trying to do something similar to this

but, using 2 to 4 DS18B20s and C# instead of processing.

I got the Arduino talking to C# via serial but I have no idea how to format the data into something usable.

Thanks for any help.

-Wes

Hi,

has anyone run into a problem using both the DS1307 (sparkfun rtc module) with the DS18B20 temp sensor? Im prototyping a data logger that writes temp, light, wind speed and voltage values to an sd card running on an atmega 168. Seems to be some interference between the wire.h and the OneWire.h libraries since I can get either to work without the other. Is it an I2C communication problem and/or ram?
I am using the ds18b30 code from the playground with modifications and the ds1307 from: Hobby Robotics » Page not found.

at the moment the temp is reading and only part of the time/date working, but after much trial and error.

any suggestions would be super helpful.
thanks in advance!

first thank you guys for the above info. what really helped me was tkbyd's first link: http://www.phanderson.com/arduino/ds18b20_1.html. its much simpler IMO than the OneWire library, however its made to talk to only one sensor per input pin... but thats cool with me.

the only thing i'm wondering about (in hope to speed up the sensor read time) is why the DS18B20 has to wait a full second when pulling the bus high after the 0x44 command and then why you need to do a reset followed by a 0xCC BEFORE you send the read command 0xBE. can anyone explain this?

the only thing i'm wondering about (in hope to speed up the sensor read time) is why the DS18B20 has to wait a full second when pulling the bus high after the 0x44 command and then why you need to do a reset followed by a 0xCC BEFORE you send the read command 0xBE. can anyone explain this?

Actually, you only need to wait a maximum of 750-milliseconds for
the 18B20 to perform the digitization of the current temperature.
This is the maximum time needed for all 12-bits of precision BUT
if you can get by with 9-bit precision (+/- 0.5 degree C and I don't
see why you couldn't) then the wait is at most 93.75-milliseconds.
These numbers come from the 18B20 spec sheet. Keep in mind
that you need to tell the 18B20 that you want only 9-bits of precision
by appropriately setting its configuration register in the scratchpad.

Why do you need to issue a 0xCC after the reset but before the 0xBE?
After a 1-wire bus reset all attached devices are listening to hear which
one gets selected with a SELECT command (0x55). This presumes
multilple devices on the bus and will determine which of the devices on
the bus the ReadScratchPad command (0xBE) will apply to so you can
read the temperature. Since you've only got one sensor on each pin you
don't need to select which sensor to read so the SkipROM command
(0xCC) is issued to tell the lone sensor that the next set of commands
are for you. If you don't do this your 18B20 will merrily sit there waiting
to be selected.

How's that?

I've tested the sample code of converting HEX to Temperature
http://www.arduino.cc/playground/Learning/OneWire

and found a slip of the pen

 LowByte = data[0];
  HighByte = data[1];

should be

 LowByte = data[0];
  HighByte = present;

otherwise got a wrong temperature like the following

R=28 90 9 60 0 0 0 CC P=1 CD 1 4B 46 7F FF 3 10 4A CRC=4A
28.81
R=28 CC FA 5F 0 0 0 A5 P=1 D3 1 4B 46 7F FF D 10 7B CRC=7B
29.18
R=28 2 ED 5F 0 0 0 4 P=1 E5 5 FF FF 7F FF B 10 2D CRC=2D
94.31
No more addresses.

0x01E5=30.31/6.25 0x05E5=94.31/6.25

After reading through the whole datasheet, I found the 3rd DS18B20 of mine is wrong and the sample code is right actually. :cry:

Hi, I'm new to arduino, in fact I only got mine today! :smiley:

I finally got the temperature probe running nicely using snipets of code :slight_smile:

I'd thought I would share it:

#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];

  if ( !ds.search(addr)) {
      Serial.print("\n");
      ds.reset_search();
      return;
  }

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

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

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

  delay(2000);     // 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]; 
  
double tempc, tempk;
tempc = (double)rawtemp / 16;
tempc = tempc;
tempk = (double)rawtemp / 16 + 273.15;
tempk = tempk;
   Serial.print("Temperature = ");
   Serial.print(tempc,4);
   Serial.println(" 'C");
   Serial.print("Temperature = ");
   Serial.print(tempk,4);
   Serial.println(" K");

}

How would it be possible to turn on a set of led's (red, yellow and green) based on the temperature?
For instance about 20'c green, between 19 and 5 yellow and below 5 red?

Thanks in advance,
Ollie

Forgot to paste the other code, use this for fahrenheit:

double tempc, tempk, tempf;
tempc = (double)rawtemp / 16;
tempc = tempc;
tempf = (1.8*(tempc)+32);
tempf = tempf;
tempk = (double)rawtemp / 16 + 273.15;
tempk = tempk;
   Serial.print("Temperature = ");
   Serial.print(tempc,4);
   Serial.println(" 'C");
   Serial.print("Temperature = ");
   Serial.print(tempf,4);
   Serial.println(" 'F");
   Serial.print("Temperature = ");
   Serial.print(tempk,4);
   Serial.println(" K");

Ollie

How would it be possible to turn on a set of led's (red, yellow and green) based on the temperature?
For instance about 20'c green, between 19 and 5 yellow and below 5 red?

if(tempX > 20)
{
   digitalWrite(greenPin, HIGH);
   digitalWrite(yellowPin, LOW);
   digitalWrite(redPin, LOW);
}
else if(tempX > 5)
{
   // Turn the yellow light on
}
else
{
  // Man, it's cold out here
}

You need to assign appropriate values to greepPin, yellowPin, and redPin, of course. You need to decide whether tempX is tempk, tempf, or tempc, too.

And, of course, you need to choose and connect the LEDs and resistors correctly.

Consider using the Dallas temperature library for the DS18B20 and DS18S20 as it has all the needed code in it. - MilesBurton.com

OK it may be not as much fun, but it saves a lot of time.