<?xml version="1.0" encoding="UTF-8" ?><oembed><version>1.0</version><provider_name>Projects</provider_name><provider_url>https://projects.schneidr.de</provider_url><author_name>Gerald</author_name><author_url>https://projects.schneidr.de/author/admin/</author_url><title>Arduino and the DS18B20 temperature sensor</title><html>So, this is the reason I started with an Arduino in the first place. In summer 2010 I ordered an Arduino and a DS18B20 temperature sensor because I wanted to monitor the water temperature in my aquarium in the summer and have the Arduino start fans if needed. My flat is directly below the roof and gets quite hot.

But I never figured out how to read the temperature from the sensor, every tutorial I followed, every code snippet I found showed either nothing at all or ridiculously wrong temperatures. Nevertheless, I just decided to try it again, wired it up and the third code snippet I tried worked. Of course, it&#039;s the sample code right on the &lt;a href=&quot;http://www.arduino.cc/playground/Learning/OneWire&quot;&gt;Arduino Playground&lt;/a&gt;...

[caption id=&quot;attachment_281&quot; align=&quot;aligncenter&quot; width=&quot;584&quot; caption=&quot;A DS18B20 temperature sensor wired up to an Arduino Duemilanove&quot;]&lt;a href=&quot;http://projects.schneidr.de/files/2011/11/P1080471.jpg&quot;&gt;&lt;img class=&quot;size-large wp-image-281&quot; title=&quot;P1080471&quot; src=&quot;http://projects.schneidr.de/files/2011/11/P1080471-600x450.jpg&quot; alt=&quot;&quot; width=&quot;584&quot; height=&quot;438&quot; /&gt;&lt;/a&gt;[/caption]

&lt;!--more--&gt;It&#039;s not perfect, it shows a little higher temperature than my regular room thermometer (20.6°C vs 23.5°C) and on random occasions it reads 85°C, but that should be easy to normalize.

So, here is the code, straight from the Arduino playground:
&lt;pre lang=&quot;arduino&quot;&gt;#include &lt;OneWire.h&gt;

OneWire ds(3); // on pin 3
void setup(void) {
  Serial.begin(9600);
}

void loop(void) {
  byte i;
  byte present = 0;
  byte data[12];
  byte addr[8];
  int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;
  if ( !ds.search(addr)) {
      ds.reset_search();
      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 &lt; 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
  }

  LowByte = data[0];
  HighByte = data[1];
  TReading = (HighByte &lt;&lt; 8) + LowByte;
  SignBit = TReading &amp; 0x8000;  // test most sig bit
  if (SignBit) // negative
  {
    TReading = (TReading ^ 0xffff) + 1; // 2&#039;s comp
  }
  Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25

  Whole = Tc_100 / 100;  // separate off the whole and fractional portions
  Fract = Tc_100 % 100;

  if (SignBit) // If its negative
  {
     Serial.print(&quot;-&quot;);
  }
  Serial.print(Whole);
  Serial.print(&quot;.&quot;);
  if (Fract &lt; 10)
  {
     Serial.print(&quot;0&quot;);
  }
  Serial.print(Fract);

  Serial.print(&quot;\n&quot;);
}&lt;/pre&gt;
&lt;p lang=&quot;arduino&quot;&gt;I removed some debug output so it shows only the temperature, and beware, this code only seems to work with the DS18B20, not with the DS18S20 and other variants of this sensor.&lt;/p&gt;
&lt;p lang=&quot;arduino&quot;&gt;This is a schematic of the wiring:&lt;/p&gt;
&lt;p lang=&quot;arduino&quot;&gt;&lt;img class=&quot;aligncenter size-full wp-image-286&quot; title=&quot;DS18B20 Wiring&quot; src=&quot;http://projects.schneidr.de/files/2011/11/DS18B20-Wiring.png&quot; alt=&quot;&quot; width=&quot;195&quot; height=&quot;185&quot; /&gt;The three pins on the bottom are, in order, Ground, +5V, and a digital input pin (in my case pin3)&lt;/p&gt;</html><type>rich</type></oembed>