View Tutorial Metadata Edit Content Revision History Add New Tutorial Add to Watchlist Working with Dallas DS18S20 and DS18B20 temperature sensors from Arduino

Working with Dallas DS18S20 and DS18B20 temperature sensors

DS18S20/DS18B20

The DS18S20 and DS18P20 are two temperature sensors from Dallas. They function on a one-wire bus which mean that for communication you need just a single wire.

Code to detect temperature sensors on a one-wire bus

The code for doing this :

#include <OneWire.h>
 
//init the one wire interface on pin 10
OneWire ow(10);
 
void setup(void) {
  Serial.begin(9600);
  lookUpSensors();
}
 
void lookUpSensors(){
  byte address[8];
  int i=0;
  byte ok = 0, tmp = 0;
  //start the search
  Serial.println("--Search started--");
  while (ow.search(address)){
    tmp = 0;
    //0x10 = DS18S20
    if (address[0] == 0x10){
      Serial.print("Device is a DS18S20 : ");
      tmp = 1;
    } else {
      //0x28 = DS18B20
      if (address[0] == 0x28){
        Serial.print("Device is a DS18B20 : ");
        tmp = 1;
      }
    }
    //display the address, if tmp is ok
    if (tmp == 1){
      if (OneWire::crc8(address, 7) != address[7]){
        Serial.println("but it doesn't have a valid CRC!");
      } else {
        //all is ok, display it
        for (i=0;i<8;i++){
          if (address[i] < 9){
            Serial.print("0");
          }
          Serial.print(address[i],HEX);
          if (i<7){
            Serial.print("-");
          }
        }
        Serial.println("");
        ok = 1;
      }
    }//end if tmp
  }//end while
  if (ok == 0){
    Serial.println("No devices were found");
  }
  Serial.println("--Search ended--");
}
 
void loop(void) {
  //do nothing :)
}

and an output sample :

--Search started--
Device is a DS18S20 : 10-1D-30-F9-01-08-00-51
--Search ended--

Read the temperature in Celsius and Fahrenheit degrees

The code for reading the temperature from the temperature sensors :

#include <OneWire.h>
 
//init the one wire interface on pin 10
OneWire  ow(10);
 
//write here the address you receive from the other program
byte sensor[8] = {0x10, 0x1D, 0x30, 0xF9, 0x01, 0x08, 0x00, 0x51};
 
void setup(void) {
  Serial.begin(9600);
}
 
void writeTimeToScratchpad(byte* address){
  //reset the bus
  ow.reset();
  //select our sensor
  ow.select(address);
  //CONVERT T function call (44h) which puts the temperature into the
scratchpad
  ow.write(0x44,1);
  //sleep a second for the write to take place
  delay(1000);
}
 
void readTimeFromScratchpad(byte* address, byte* data){
  //reset the bus
  ow.reset();
  //select our sensor
  ow.select(address);
  //read the scratchpad (BEh)
  ow.write(0xBE);
  for (byte i=0;i<9;i++){
    data[i] = ow.read();
  }
}
 
float getTemperature(byte* address){
  int tr;
  byte data[12];
 
  writeTimeToScratchpad(address);
 
  readTimeFromScratchpad(address,data);
 
  //put in temp all the 8 bits of LSB (least significant byte)
  tr = data[0];
 
  //check for negative temperature
  if (data[1] > 0x80){
    tr = !tr + 1; //two's complement adjustment
    tr = tr * -1; //flip value negative.
  }
 
  //COUNT PER Celsius degree (10h)
  int cpc = data[7];
  //COUNT REMAIN (0Ch)
  int cr = data[6];
 
  //drop bit 0
  tr = tr >> 1;
 
  //calculate the temperature based on this formula :
  //TEMPERATURE = TEMP READ - 0.25 + (COUNT PER Celsius Degree - COUNT REMAIN)
/ (COUNT PER Celsius Degree)
 
  return tr - (float)0.25 + (cpc - cr)/(float)cpc;
}
 
//fahrenheit to celsius conversion
float f2c(float val){
  float aux = val - 32;
  return (aux * 5 / 9);
}
 
//celsius to fahrenheit conversion
float c2f(float val){
  float aux = (val * 9 / 5);
  return (aux + 32);
}
 
void loop(void) {
  float temp;
  float tmp2;
  tmp2 = getTemperature(sensor);
  temp = c2f(tmp2);
 
  Serial.print("Temp = ");
  Serial.print(temp);
  Serial.print(" F or ");
  Serial.print(tmp2);
  Serial.println(" C");
  //wait 30 seconds
  delay(30000);
}

An image of the project

Here is an image of the project : http://stuff.nekhbet.ro/wp-content/uploads/2009/08/real-scheme-ds18s20.jpg

Talk icons discuss – in the article above, point your mouse over them to reply.
Can't find a relevant Talk icon for what you have to say? See how you can add one...
Note: All the talks and replies are also listed below.


Add a new discussion topic (Only if you don't find it above, duplicates will be removed)


Rating: (2+, 0-) In: Arduino