user18720: I'm trying to measure the temperature using a DS18B20 sensor. I'm using the DallasTemperature and OneWire Libraries. The sensor is connect in Pin 10 with a 4k7 resistor between VDD and DQ (actually I'm using 4x 1k and 2x 330 ohms) as so:

Here's the code I'm using:
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 10

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

float celsius;

void setup(void) {
  Serial.begin(9600);

  sensors.setResolution(12);

  sensors.begin();
}


void loop(void) {
  sensors.requestTemperatures();
  celsius = sensors.getTempCByIndex(0);

  Serial.println(celsius);
}

The sensor is outputing values close to the actual temperature (measure with a mercury thermometer), but a bit off.
Example:
Sensor Temperaute: 34.44°C
Thermometer Temperature: ~32°C

Why is that? And how can I fix it?

user30198: When the wires to the DS18B20 are short, you can use a 10k resistor as well.
The DS18B20 is accurate. If you have connected it wrong and it became very hot, then it might be damaged. But normally it is always accurate.
Many report that the DS18B20 returns a high temperature, and the cause is often something nearby that makes the DS18B20 warmer. For example an Arduino board (with a hot voltage regulator) or a computer screen or a certain air flow of warm air or even very dim sunlight. A normal thermometer does not do that, but the DS18B20 is highly influenced by warm things nearby.
Some temperature sensors have a problem with self-heating. You could add a delay of 5 seconds in the loop function, to see if that makes a difference.
To be sure you can put it in distilled water at sea level at 0°C (water and ice cubes) and 100°C (boiling water).
The picture uses pin 2 and the sketch uses pin 10. I assume that is a mistake.

18720: <p>I'm trying to measure the temperature using a DS18B20 sensor. I'm using the DallasTemperature and OneWire Libraries. The sensor is connect in Pin 10 with a 4k7 resistor between VDD and DQ (actually I'm using 4x 1k and 2x 330 ohms) as so:</p>

<p><a href="https://i.stack.imgur.com/UhHft.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UhHft.jpg" alt="layout"></a></p>

<p>Here's the code I'm using:</p>

<pre><code>#include &lt;OneWire.h&gt;
#include &lt;DallasTemperature.h&gt;

#define ONE_WIRE_BUS 10

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&amp;oneWire);

float celsius;

void setup(void) {
  Serial.begin(9600);

  sensors.setResolution(12);

  sensors.begin();
}


void loop(void) {
  sensors.requestTemperatures();
  celsius = sensors.getTempCByIndex(0);

  Serial.println(celsius);
}
</code></pre>

<p>The sensor is outputing values close to the actual temperature (measure with a mercury thermometer), but a bit off.</p>

<p>Example:</p>

<pre><code>Sensor Temperaute: 34.44°C
Thermometer Temperature: ~32°C
</code></pre>

<p>Why is that? And how can I fix it?</p>
 30198: <p>When the wires to the DS18B20 are short, you can use a 10k resistor as well.</p>

<p>The DS18B20 is accurate. If you have connected it wrong and it became very hot, then it might be damaged. But normally it is always accurate.</p>

<p>Many report that the DS18B20 returns a high temperature, and the cause is often something nearby that makes the DS18B20 warmer. For example an Arduino board (with a hot voltage regulator) or a computer screen or a certain air flow of warm air or even very dim sunlight. A normal thermometer does not do that, but the DS18B20 is highly influenced by warm things nearby.</p>

<p>Some temperature sensors have a problem with self-heating. You could add a delay of 5 seconds in the loop function, to see if that makes a difference.</p>

<p>To be sure you can put it in distilled water at sea level at 0°C (water and ice cubes) and 100°C (boiling water).</p>

<p>The picture uses pin 2 and the sketch uses pin 10. I assume that is a mistake.</p>
