Temperature Sensor

created by hek , updated

For temperature measurements we've selected the standard Dallas DS18B20.

It's accurate, inexpensive and you can connect many of them in parallel without using more than one digital input on the Arduino board.

They come in several variants: probe, individual, brick and a waterproof version.

You will also need a small pull-up resistor to make the circuit work. It is usually around 4,7kOhm.

Wiring Things Up

Start by connecting the radio module.

Sensor Arduino Comment
GND (left pin) GND Marked black
VCC (right pin) VCC (3.0-5.5V) Marked red
DQ (middle pin) Digital pin 3 Marked green

Mount a 4.7kOhm (4k7) resistor between VCC and Digital pin 3


This example uses the external DallasTemperature library found here. Please install it and restart the Arduino IDE before trying to compile.

Last updated by web-flow, 11 Aug 2016, "Move sensors.begin() from void setup() to void before() "

 * The MySensors Arduino library handles the wireless radio link and protocol
 * between your home built sensors/actuators and HA controller of choice.
 * The sensors forms a self healing radio network with optional repeaters. Each
 * repeater and gateway builds a routing tables in EEPROM which keeps track of the
 * network topology allowing messages to be routed to nodes.
 * Created by Henrik Ekblad <[email protected]>
 * Copyright (C) 2013-2015 Sensnology AB
 * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
 * Documentation: http://www.mysensors.org
 * Support Forum: http://forum.mysensors.org
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 * Example sketch showing how to send in DS1820B OneWire temperature readings back to the controller
 * http://www.mysensors.org/build/temp

// Enable debug prints to serial monitor
//#define MY_DEBUG 

// Enable and select radio type attached
#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69

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

#define COMPARE_TEMP 1 // Send temperature only if changed? 1 = Yes 0 = No

#define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
#define MAX_ATTACHED_DS18B20 16
unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature. 
float lastTemperature[MAX_ATTACHED_DS18B20];
int numSensors=0;
bool receivedConfig = false;
bool metric = true;
// Initialize temperature message
MyMessage msg(0,V_TEMP);

void before()
  // Startup up the OneWire library

void setup()  
  // requestTemperatures() will not block current thread

void presentation() {
  // Send the sketch version information to the gateway and Controller
  sendSketchInfo("Temperature Sensor", "1.1");

  // Fetch the number of attached temperature sensors  
  numSensors = sensors.getDeviceCount();

  // Present all sensors to controller
  for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {   
     present(i, S_TEMP);

void loop()     
  // Fetch temperatures from Dallas sensors

  // query conversion time and sleep until conversion completed
  int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution());
  // sleep() call can be replaced by wait() call if node need to process incoming messages (or if node is repeater)

  // Read temperatures and send them to controller 
  for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {

    // Fetch and round temperature to one decimal
    float temperature = static_cast<float>(static_cast<int>((getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;

    // Only send data if temperature has changed and no error
    #if COMPARE_TEMP == 1
    if (lastTemperature[i] != temperature && temperature != -127.00 && temperature != 85.00) {
    if (temperature != -127.00 && temperature != 85.00) {

      // Send in the new temperature
      // Save new temperatures for next compare


NameSize# Downloads
DS18B20.pdf333.51 kB184

Shopping Guide

100pcs 4k7 Pull-up Resistors
Used for Dallas temperature sensors.
196 available - $3.98   Buy
In stock - $7.29   Buy
10pcs DS18B20 Dallas Temperature Sensors
Use this for measuring room temperature. You'll also need a pull up resistor
306 available - $6.27   Buy
In stock - $8.55   Buy
DS18B20 Waterproof Dallas Temperature Sensors
About 100 cm cable length. Good when you want to measure the temperature in a freezer or on pipes. You'll also need a pull up resistor.
1755 available - $1.77   Buy
Stock unknown   Buy
DS18b20 Dallas Temperature Sensors Module
Easy to connect. Built in pull up resistor.
30 available - $4.10   Buy
In stock - $5.45   Buy