Pages: [1]   Go Down
Author Topic: DS18B20 temperature sensor  (Read 1433 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys, I'm looking for some help with a DS18B20 temp sensor I have.

First of, I'm pretty new to arduino so appologies if my questions are daft   smiley-kitty

I can get the sensor to accurately display the temp using the one wire library. What I want to do however is get the sensor displaying the temperature without needing to use a library (I'm going to be running a course with kids and need to be able to fully explain every line of code to them - they have absolutely no programming experience).

I can get a value from the middle pin and am looking for a formula to convert this to an accurate temperature reading. I "think" I read somewhere that you needed to know the power to the sensor but can't find where I thought I seen this.

Can anyone supply (and explain  smiley ) a simple one line formula which will achieve this please.

Any help gratefully appreciated.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8821
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The value in the first two bytes of the scratchpad are the temperature in 16ths of a degree Centigrade.  Divide by 16 to get degrees centigrade.

Code:
float readThermometer(byte addr[8])
{
  byte data[9];
  float temperature_C;

  DS18S20.reset();
  DS18S20.select(addr);   
  DS18S20.write(0xBE);         // Read Scratchpad

  for (int i = 0; i < 9; i++)
    data[i] = DS18S20.read();

  if (data[5] != 0xFF || data[7] != 0x10) {
    return 999.999;  // Invalid response
  }

  if ( OneWire::crc8(data, 8) == data[8]) {
    // CRC is valid
    int temperature = (data[1] << 8) + data[0];  // Two byte binary temperature in °C/16
    temperature_C = temperature / 16.0;
  }
  else {
    return 999.999;  // CRC doesn't match
  }
  return temperature_C;
}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

DE
Offline Offline
God Member
*****
Karma: 21
Posts: 707
| Germany | NRW |
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Is there a way that this works also for native DS1820 ? (no DS18x20, they works fine).
I still have a bunch of DS1820 and want to use them...with the normal libs it seems to be impossible - if i use parasit power or not.
Logged

Dum spiro, spero - if you find my posting helpful, please click my Karma smiley
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Do you know A.R.T

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 47
Posts: 2263
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What I want to do however is get the sensor displaying the temperature without needing to use a library


Like this

http://sheepdogguides.com/arduino/ar3ne1tt.htm.

Or you might consider using a thermistor instead of the DS18B20.

Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8821
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is there a way that this works also for native DS1820 ? (no DS18x20, they works fine).
I still have a bunch of DS1820 and want to use them...with the normal libs it seems to be impossible - if i use parasit power or not.

The old DS1820 has been replaced by the DS18S20. "The primary specification difference between the two parts is the temperature conversion time: DS1820 = 500ms (max) and DS18S20 = 750ms (max)."

The steps on the DS18S20 (and by extension on the DS1820) is 1/2 Degree Centigrade.  Take the code above and instead of 16 use 2.

If you need more resolution there is a formula in the datasheet for taking Byte 0 (Temperature LSB), Byte 1 (Temperature MSB), Byte 6 (COUNT REMAIN) and Byte 7 (COUNT PER °C) and calculating temperature to the nearest 1/16th degree.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That looks kind of like what I'm after John, although there are a few bits of the code I don't understand (as I said, I'm new to C and arduino programming).

The following are the main bits I'm unsure about -

DS18S20.reset();
DS18S20.select(addr);   
DS18S20.write(0xBE);         // Read Scratchpad

and

DS18S20.read();

DS18S20 - is that a variable which needs to be declared somewhere? I get an error with it when I try to compile code with it.

The reset, select, write and read properties - do I need a library to use them or should they be recognised?

addr and 0xBE - I have no idea what these represent  smiley-confuse

I understand what the code is trying to do overall, it's just the syntax I'm struggling with.


Nick - that looks even more complex  smiley-eek
Logged

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 47
Posts: 2263
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Nick - that looks even more complex  smiley-eek

Definitely not. So much not, indeed, that I haven't the faintest idea of what your discussion with Wasser is about, but I used the sheepdog stuff when a total newbie.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8821
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, I thought you were already familiar with the OneWire library.

You will have to call StartThermometers() to tell all DS18S20 chips on the OneWire bus to start reading a temperature.  750 milliseconds later you can read the 'scratchpad' of each chip and use the data you read to calculate the temperature.

Code:
#include <OneWire.h>

// DS18S20 Temperature chip I/O
const int ONEWIRE_PIN = 9;
OneWire DS18S20(ONEWIRE_PIN);

void startThermometers()
{
  DS18S20.reset();
  DS18S20.write(0xCC);      // Address all thermometers
  DS18S20.write(0x44);         // start conversion, no parasite power on at the end
}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm getting there  smiley

The code I have so far (I would like to keep the code as simple as possible - no error checking etc)

Code:
#include <OneWire.h>   

OneWire tempSensor(2); 

void setup(void) {
  Serial.begin(9600);
}

void loop(void) {
  byte data[9];
  int temperature;
  float temperature_C;

  tempSensor.reset();
  tempSensor.write(0xCC);     
  tempSensor.write(0xBE);         
 
  delay(10000);
 
  for (int i = 0; i < 9; i++) {
    data[i] = tempSensor.read();
  }
 
  temperature = (data[1] << 8) + data[0]; 
  temperature_C = temperature / 16.0;

  Serial.print(temperature_C);
  Serial.println();
}

This code doesnt work correctly. I get a temperature reading when I open up the serial window, but it doesnt change.

The other thing I dont understand are the values such as 0xCC and 0xBE

I believe they are hex numbers - does the 0x part signify this?

If so, the CC and BE values, what do they represent, or where can I find out?
Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 216
Posts: 13670
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

0x is indeed the prefix for hexadecimal numbers   

0xCC and 0xBE are commands for the chip, you can find these and much more in the datasheet

- http://datasheets.maximintegrated.com/en/ds/DS18B20.pdf -
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8821
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

0xCC is the command that tells ALL devices on the OneWire bus to listen.  You can use this in place of .select(address) if you are only sending or you expect an answer (Read Scratchpad) and you only have one device on the bus.

0x44 is the command to calculate the temperature and save it in the scratchpad.  This takes up to 750 milliseconds.

0xBE is the command to send the contents of of the scratchpad.

What you are sending is "Everybody: Send your Scratchpad".  This only works if you only have one device.  Since you don't send any commands to take a new reading you get the same reading every time.

Try:
Code:
  tempSensor.reset();
  tempSensor.write(0xCC);  // All devices:
  tempSensor.write(0x44);  // Start a temperature reading
 
  delay(10000);  // Reading takes 750 milliseconds.  Waiting longer is OK.
 
  tempSensor.reset();
  tempSensor.write(0xCC); // All devices:   (must be only one device)
  tempSensor.write(0xBE); //  Send the content of your scratchpad.
   
  for (int i = 0; i < 9; i++) {
    data[i] = tempSensor.read();
  }
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Brilliant, I've got it now  smiley

Thanks so much everyone for your input - the hex numbers were really throwing me, now I see where they are coming from. It was them being listed in code as things like 0xCC but in the datasheet as CCh.

Thanks again guys, much appreciated  smiley
Logged

Pages: [1]   Go Up
Jump to: