Go Down

Topic: DS18B20 temperature sensor (Read 1 time) previous topic - next topic

dmkelgin

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   ^_^

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  :) ) a simple one line formula which will achieve this please.

Any help gratefully appreciated.

johnwasser

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: [Select]

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;
}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

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.
Dum spiro, spero - if you find my posting helpful, please click my Karma :)<br />~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />Do you know A.R.T, the FREE Android Root Tool ?

Nick_Pyner


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.


johnwasser


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.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

dmkelgin

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  :~

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:

Nick_Pyner



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.

johnwasser

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: [Select]

#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
}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

dmkelgin

I'm getting there  :)

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

Code: [Select]
#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?

robtillaart

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 -
Rob Tillaart

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

johnwasser

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: [Select]

  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();
  }
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

dmkelgin

Brilliant, I've got it now  :)

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  :)

Go Up