DS18x20_Temperature.ino 3.0 KB

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