DS18B20 onewire temperature sensor

The DS18B20 temperature sensor is a digital temperature sensor using the 1-Wire protocol. In this post I will review the sensor, show how it can be used and give a final judgement on it.

What can the DS18B20 temperature sensor do?

The DSB18B20 temperature sensor is a digital temperature sensor that works using the 1-Wire Protocol. The sensor takes care of turning the analogue signal detected into a digital value in Celsius.

Some temperature sensors work by giving you the current resistance and expect you to work out the temperature based on the specific sensor. However this sensor does this for you and means you don’t need to calibrate the sensor to known temperatures and resistance values.

This is a big advantage for basic projects where you want to quickly want to know the temperature. The downside is that you need something that speaks the 1-Wire protocol. However there is an Arduino library to do this and means you don’t need to program it yourself.

Wiring it up to an Arduino

Using the one wire protocol there are two ways to power the device, parasitic and standard power mode.

In parasitic mode, only the data and GND lines need to be connected which removes the need for the VCC voltage input. To power the device the data line is used to charge an internal capacitor in the sensor. This is stored for periods when the data line is used to send data and no power is being sent. Depending on the sensor there will be delays while the device is “charged” during which no communication can take place on the data bus.

In standard power mode both data, GND and VCC lines are connected to the sensor. By having a permanent voltage line the need to pause communication is removed since power is always provided. Running in standard power mode means that the device can be polled for data at a much faster rate.

For my experiments I am going to power the sensor in standard power mode. This will mean I can poll the sensor for the temperature at a much faster rate, helpful for logging data rapidly.

To connect the sensor to the Arduino in standard power mode I am connecting GND to the Arduino GND and VCC to the 5 volt line on the Arduino. The data line has a pullup resistor between the 5 volts and the data line. It is recommended to use a 4.7 k resistor between these lines. The data line is also connected to pin 8 on the Arduino. This can be any digital pin on the Arduino so the 1Wire protocol can be used.

Programming the Arduino with the DS18B20 temperature sensor

To access the DS18B20 temperature sensor I am going to use the OneWire and DallasTemperature Arduino library. The OneWire library allows me to access the OneWire protocol without having to know the specifics of how it works.

In addition to this I am going to use the DallasTemperature library as a wrapper around the OneWire library. This works with the OneWire library to specifically work with temperature sensors.

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_PIN 8

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

void setup() {
     Serial.begin(115200);
}

void loop() {
    sensors.requestTemperatures();
    Serial.println(sensors.getTempCByIndex(0));
    delay(1000);
}

Above I create the OneWire object and pass it by reference (the ampersand, &) in the constructor of the DallasTemperature object. With this I am able to request the temperate of the sensor at any time.

In my loop I call requestTemperatures on the sensors object to get the temperatures of all sensors on the 1-Wire bus. This operation will detect the sensors on the bus and request the data from the sensors. Once this function call returns I use the sensors object again to get the temperature in celsius using getTempCByIndex.  Here I am asking for the first sensor on the bus (as there is only one) so giving it the parameter 0 (programmers count from 0).

Once I have the data I print it out to the serial so that I may see the data change as it continues.

Practical Measurements

Using python to collect the data from the Arduino I set the sensor up in an air-conditioned room. In this room the air conditioning was set to 23C.

During the period of the experiment, it was cold outside but was slowly warmed during the day by other people in the room. Here you can see that over the period of the day the temperate was slowly increasing. The DS18B20 temperature sensor has a high resolution and does not fluctuate between temperatures. This ensures that the readings while changing, were stable and slowly increasing.

Previous temperature sensors I have tested, DHT11, DHT22 has problems where the temperature would fluctuate between two values however this does not seem as apparent for this sensor.

The precision of the sensor shows a gradual increase in temperature which is good to see the consistency of the data. In the future, I will look to compare this data against other like for like sensors.

Final Review

One of the advantages of this sensor over many other temperature sensors is that it returns data in celsius by default. This removes the need for a sometimes complex calibration step before using it. The precision level is similar to the DHT22 tested previously.

One of the features that sets this apart is the 1-Wire protocol this operates on. This can reduce the need for many IO ports on the Arduino and may be helpful when running larger projects.

Overall this sensor is at a similar price point and feature level as the DHT22 temperature sensor.  However the unique addition of the 1-Wire bus protocol means this is certainly a useful sensor to have around.

Would buy again!

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.