Pages: [1] 2 3   Go Down
Author Topic: Please update OneWire page for DS18B20  (Read 5440 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 39
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 11
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 39
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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...)
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Sussex UK / CT USA
Offline Offline
Edison Member
*
Karma: 0
Posts: 1028
Forums forever
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

1-Wire fans:

There's more for you at...

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

and
http://www.arunet.co.uk/tkboyd/e1didx.htm
(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.
Logged

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 289
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 http://www.arduino.cc/playground/Learning/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)
Logged

Arduino Uno;
Mega328

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 289
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 http://www.arduino.cc/playground/Learning/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?  
Logged

Arduino Uno;
Mega328

0
Offline Offline
Newbie
*
Karma: 0
Posts: 39
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
Code:
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
Code:
double tempc, tempf;
tempc = (double)rawtemp / 16.0;
tempf = (tempc * 1.8) + 32.0;
Logged

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 289
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.smiley-cool + 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
Logged

Arduino Uno;
Mega328

0
Offline Offline
Newbie
*
Karma: 0
Posts: 39
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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...
Code:
 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.
Logged

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 289
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: April 21, 2008, 09:07:43 pm by jerseyguy1996 » Logged

Arduino Uno;
Mega328

0
Offline Offline
Newbie
*
Karma: 0
Posts: 39
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Quote
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.

Quote
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.
Logged

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 289
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.  
« Last Edit: April 22, 2008, 03:40:13 pm by jerseyguy1996 » Logged

Arduino Uno;
Mega328

0
Offline Offline
Full Member
***
Karma: 0
Posts: 115
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 39
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Pages: [1] 2 3   Go Up
Jump to: