Friday, December 19, 2014

Connecting the Temperature Sensor DS18B20 to an Arduino

Searching on the internet how to connect the temperature sensor DS18B20 to an Arduino, returns multiple ways. This way seems to work best for me.

This page Arduino 1-Wire Address Finder explains more about the sensor and the fact that it is a 1-Wire device. 
I create a C++ class named DS18B20TemperatureSensor.cpp (.h) that expose the following methods:
class DS18B20TemperatureSensor {


        DS18B20TemperatureSensor(OneWire * oneWire);
        int GetSensorId();
        char * GetSensorName();
        double GetTemperature();
        double CelsiusToFahrenheit(double celsius);
        String GetSensorUniqueIdsOn1Wire();

The class can detect the 2 sensors DS18S20 and DS18B20 (Comparing the DS18S20 and the DS18B20).

The class can also get the Sensor Unique Ids if you have more than one on the same wire.
Though for now the method GetTemperature() will only return the temperature of the first one.


* Temperature Sensor DS18B20                                   *
Sensor ID:40, Name:DS18B20
Sensor Unique Ids:{ 40, 25, 144, 170, 05, 00, 00, 227 }
[00:00:01, mem:1421] 20.31C - 68.56F
[00:00:08, mem:1421] 20.38C - 68.68F
[00:00:10, mem:1421] 20.38C - 68.68F
[00:00:12, mem:1421] 20.75C - 69.35F
[00:00:13, mem:1421] 21.63C - 70.93F
[00:00:14, mem:1421] 23.63C - 74.52F
[00:00:15, mem:1421] 24.31C - 75.76F
[00:00:17, mem:1421] 24.94C - 76.89F
[00:00:18, mem:1421] 25.13C - 77.22F
[00:00:19, mem:1421] 25.06C - 77.11F
[00:00:21, mem:1421] 24.63C - 76.32F
[00:00:22, mem:1421] 24.50C - 76.10F
[00:00:24, mem:1421] 24.38C - 75.88F
[00:00:26, mem:1421] 24.19C - 75.54F
[00:00:28, mem:1421] 24.06C - 75.31F
[00:00:30, mem:1421] 23.94C - 75.09F

#include "OneWire.h"
#include "fArduino.h"
#include "DS18B20TemperatureSensor.h"

#define ONE_WIRE_BUS 2

OneWire                  _oneWire(ONE_WIRE_BUS);
DS18B20TemperatureSensor _DS18B20(&_oneWire);
TimeOut                  _oneSecondTimeOut(1000);

void setup() {

    Board.InitializeComputerCommunication(9600, "Initializing...");
    Board.TraceHeader("Temperature Sensor DS18B20");
    Board.Trace(Board.Format("Sensor ID:%d, Name:%s",
                _DS18B20.GetSensorId(), _DS18B20.GetSensorName()));

    Board.Trace(Board.Format("Sensor Ids:%s", 

void loop() {

    static double previousCelsius;

    if (_oneSecondTimeOut.IsTimeOut()) {

        double celsius = _DS18B20.GetTemperature();

        if (previousCelsius != celsius || _oneSecondTimeOut.EveryCalls(10)) {
            double fahrenheit = _DS18B20.CelsiusToFahrenheit(celsius);
            Board.Trace(Board.Format("[%s, mem:%d] %fC - %fF",  
                        Board.GetTime(), Board.GetFreeMemory(), celsius, fahrenheit));
            previousCelsius = celsius;

Source Code

All the source code is available on Github TempCensorDS18B20

Other Wiring

With this simpler wiring you can also get the temperature, see page Arduino - One Wire Digital Temperature Sensor - DS18B20, but it will not work in One Write mode (I Think). With this wiring the function GetSensorUniqueIdsOn1Wire() does not work.

No comments:

Post a Comment