Arduino in combination with DS18B20 temperature sensor

This guide shows how to operate the temperature sensor via the Arduino board.

The following elements are used in the example:

Connection

At the beginning the library should be added to Arduino environment (Sketch -> Include Library -> Add .ZIP Library...). Next we connect sensor pins according to the table below:

Arduino pins Pin Sensor
GND GND
Pin 10 DQ
5 V Vdd

In addition, the sensor requires a 4.7 kΩ pull-up resistor between DQ and 5 V power supply - this does not apply to modules that already have this resistor built in.

Wiring diagram of the temperature sensor.

Operation

In order to obtain the temperature from the sensor we will use a sample program DS18x20_Temperature from the attached library (File -> Examples -> 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. I was and;
  16. byte present = 0;
  17. byte type_s;
  18. byte date[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. 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. 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. date[i] = ds.read();
  86. Serial.print(data[i], HEX);
  87. Serial.print(" ");
  88. }
  89. Serial.print(" CRC=");
  90. Serial.print(OneWire::crc8(date, 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 = (date[1] << 8) | date[0];
  98. if (type_s) {
  99. raw = raw << 3; // 9 bit resolution default
  100. if (date[7] == 0x10) {
  101. // count remain gives full 12 bit resolution
  102. raw = (raw & 0xFFF0) + 12 - date[6];
  103. }
  104. } else {
  105. byte cfg = (date[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. }

The effects of the program can be seen on the screen below.

Serial screenshot of the monitor.

Botland.store - shop for makers!