Skip to content

Components DS18B22 temperature sensor

Pekka Nikander edited this page Jun 4, 2013 · 2 revisions

At this stage it is unclear if the used Temperature sensor is DS1822 or DS18B20.

Both DS1822 and DS18B22 are digital 1-wire temperature sensors.

Arduino 1-wire library

DS1822 DS18B20

Working code

#include <OneWire.h>

// DS18S20 Temperature chip i/o
OneWire ds(8);  // on pin 8

void setup(void) {
  // start serial port
  Serial.begin(115200);
  pinMode(13, OUTPUT); 
}

void loop(void) {
  digitalWrite(13, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  delay(200);              // wait for a second
  digitalWrite(13, HIGH);   // set the LED on
  
  byte i;
  byte present = 0;
  byte type_s = 0;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;

  if (!ds.search(addr)) {
      Serial.print("No more 1-wire addresses.\n\n\n\n");
      ds.reset_search();
      return;
  }

  Serial.print("address = ");
  for( i = 0; i < 8; i++) {
    Serial.print(addr[i], HEX);
    Serial.print(" ");
  }

  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.print("CRC is not valid!\n");
      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();
  }

  unsigned int raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // count remain gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    if (cfg == 0x00) raw = raw << 3;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
    // default is 12 bit resolution, 750 ms conversion time
  }
  celsius = (float)raw / 16.0;
  Serial.print("  temp = ");
  Serial.print(celsius);
  Serial.println(" C ");
}

Clone this wiki locally