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) {



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


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


Sensor Temperaute: 34.44°C
Thermometer Temperature: ~32°C

Why is that? And how can I fix it?

  • 3
    if the error is consistent (stable) you can just correct it in software, but it should be within 1/2 deg C; is your thermometer calibrated?
    – dandavis
    Commented Mar 2, 2017 at 20:48

1 Answer 1


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.

  • Oh I see, maybe that's why, The sensor is in a protoboard in my desk, it has a bit of sunlight shining on it, should I cover the sensor? Doesn't it create the problem of airflow trapping hot air inside? I'll try the delay maybe that's why, I'm using no delay whatsoever. Commented Mar 2, 2017 at 20:31
  • Get it away from the sunlight, and, as mentioned, anything else that might be warm.
    – KC Tucker
    Commented Mar 2, 2017 at 21:06
  • As long as the thermometer is measuring the same exact conditions as the sensor, you should be OK. Put the bulb right against the sensor and wrap it in a rag or something. Both would be measuring the temperature in the center of the rag.
    – Dave X
    Commented Mar 9, 2017 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.