DS18x20_Temperature.ino


1
#include <OneWire.h>
2
3
// OneWire DS18S20, DS18B20, DS1822 Temperature Example
4
//
5
// http://www.pjrc.com/teensy/td_libs_OneWire.html
6
//
7
// The DallasTemperature library can do all this work for you!
8
// http://milesburton.com/Dallas_Temperature_Control_Library
9
10
OneWire  ds(10);  // on pin 10 (a 4.7K resistor is necessary)
11
12
void setup(void) {
13
  Serial.begin(9600);
14
}
15
16
void loop(void) {
17
  byte i;
18
  byte present = 0;
19
  byte type_s;
20
  byte data[12];
21
  byte addr[8];
22
  float celsius, fahrenheit;
23
  
24
  if ( !ds.search(addr)) {
25
    Serial.println("No more addresses.");
26
    Serial.println();
27
    ds.reset_search();
28
    delay(250);
29
    return;
30
  }
31
  
32
  Serial.print("ROM =");
33
  for( i = 0; i < 8; i++) {
34
    Serial.write(' ');
35
    Serial.print(addr[i], HEX);
36
  }
37
38
  if (OneWire::crc8(addr, 7) != addr[7]) {
39
      Serial.println("CRC is not valid!");
40
      return;
41
  }
42
  Serial.println();
43
 
44
  // the first ROM byte indicates which chip
45
  switch (addr[0]) {
46
    case 0x10:
47
      Serial.println("  Chip = DS18S20");  // or old DS1820
48
      type_s = 1;
49
      break;
50
    case 0x28:
51
      Serial.println("  Chip = DS18B20");
52
      type_s = 0;
53
      break;
54
    case 0x22:
55
      Serial.println("  Chip = DS1822");
56
      type_s = 0;
57
      break;
58
    default:
59
      Serial.println("Device is not a DS18x20 family device.");
60
      return;
61
  } 
62
63
  ds.reset();
64
  ds.select(addr);
65
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end
66
  
67
  delay(1000);     // maybe 750ms is enough, maybe not
68
  // we might do a ds.depower() here, but the reset will take care of it.
69
  
70
  present = ds.reset();
71
  ds.select(addr);    
72
  ds.write(0xBE);         // Read Scratchpad
73
74
  Serial.print("  Data = ");
75
  Serial.print(present, HEX);
76
  Serial.print(" ");
77
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
78
    data[i] = ds.read();
79
    Serial.print(data[i], HEX);
80
    Serial.print(" ");
81
  }
82
  Serial.print(" CRC=");
83
  Serial.print(OneWire::crc8(data, 8), HEX);
84
  Serial.println();
85
86
  // Convert the data to actual temperature
87
  // because the result is a 16 bit signed integer, it should
88
  // be stored to an "int16_t" type, which is always 16 bits
89
  // even when compiled on a 32 bit processor.
90
  int16_t raw = (data[1] << 8) | data[0];
91
  if (type_s) {
92
    raw = raw << 3; // 9 bit resolution default
93
    if (data[7] == 0x10) {
94
      // "count remain" gives full 12 bit resolution
95
      raw = (raw & 0xFFF0) + 12 - data[6];
96
    }
97
  } else {
98
    byte cfg = (data[4] & 0x60);
99
    // at lower res, the low bits are undefined, so let's zero them
100
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
101
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
102
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
103
    //// default is 12 bit resolution, 750 ms conversion time
104
  }
105
  celsius = (float)raw / 16.0;
106
  fahrenheit = celsius * 1.8 + 32.0;
107
  Serial.print("  Temperature = ");
108
  Serial.print(celsius);
109
  Serial.print(" Celsius, ");
110
  Serial.print(fahrenheit);
111
  Serial.println(" Fahrenheit");
112
}