Open form

Arduino w połączeniu z czujnikiem temperatury DS18B20

Poradnik przedstawia sposób obsługi czujnika temperatury za pośrednictwem płytki Arduino.

 

W przykładzie wykorzystano następujące elementy:

 

Podłączenie

Na początku bibliotekę należy dodać do środowiska Arduino (Szkic -> Include Library -> Add .ZIP Library...). Następnie łączymy piny czujnika według poniższej tabeli:

 

Pin Arduino Pin Czujnika
GND GND
Pin 10 DQ
5 V Vdd

 

 

Dodatkowo czujnik wymaga rezystora podciągającego 4,7 kΩ między DQ a zasilaniem 5 V - nie dotyczy to modułów, które mają już ten rezystor wbudowany.

 

 

 

 Schemat podłączenia czujnika temperatury.

 

Obsługa

W celu uzyskania temperatury z czujnika wykorzystamy przykładowy program DS18x20_Temperature pochodzący z dołączonej biblioteki (Plik -> Przykłady -> OneWire -> DS18x20_Temperature).

 

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

 

Efekty działania programu możemy zobaczyć na poniższym screenie.

 

 

Zrzut ekranu szeregowego monitora.

zapraszamy do współpracy!