Dallas DS18b20 Temperature Sensor

These were the sensors used in the remote temperature network project.

Hardware & Pinout

These nifty temperature sensors come in two form factors.

  • The TO-92 package (looks like a transistor)
  • Waterproof probe looking thing.

In both instances, a 4.7k Ohm resistor is needed to pull the data line high. There are screw block terminal ‘modules’ that can do this, too.

They’re also 5V tolerant and good to -55°C to +125°C.

Datasheet is here.

For use with most Arduinos, data in D2 seems to be the norm. Never tried any different.

I’ve had no success with an ESP32 when pissing about with the Weather Station Project. However, that’s not to say that there isn’t a solution.

Code

As always, here is the skeleton code needed to output the temperature to the serial monitor every second.

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

#define ONE_WIRE_BUS 2

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

void setup() {

  Serial.begin(9600);
  sensors.begin();
  sensors.setResolution(12);

}

void loop() {

  sensors.requestTemperatures();
  float temp = sensors.getTempCByIndex(0);
  Serial.println(temp);
  delay(1000);

}

Each sensor has it’s own 64 bit address. There are methods to obtain these addresses and then access each one individually.


Last updated: 21/10/2023