0

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:

layout

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?

asked Mar 2, 2017 at 20:00
1
  • 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? Commented Mar 2, 2017 at 20:48

1 Answer 1

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.

answered Mar 2, 2017 at 20:19
3
  • 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. 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. Commented Mar 9, 2017 at 21:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.