Temperature sensing (Dallas DS18B20 et. al.)

Temperature can be sensed in a variety of ways, from a simple thermistor connected to the analogue pin of your micro-controller to fully digital versions that send their information over a bus like I²C. In this “Basics” article we will be using the digital serial bus kind. Pictured below is the device that we will be setting up.

It uses a Dallas semiconductor chip (DS18B20) inside its innards, although this guide will work with many of the Dallas Semiconductor chips, but this seems the most popular. This is a very widely sold sensor and the price at time of writing is less than $1US, 1euro or 1GBP.  It does not use the usual I²C or SPI interfaces that are built in as standard on many micro-controllers but instead opts for the “1 wire” interface. This means we can connect the sensor to any of the Arduino pins providing that we tell our “1 Wire” library we are using, which pin it should talk to in order to read the sensor. Another big advantage of these sensors on this 1 wire bus is that they all have a unique address, so you could easily connect many of them onto the same pin and read many temperature sensors from many locations if you so desired.

It has three wires not 1!
OK, it’s called a one wire bus but looking at the picture you may have spotted that it actually appears to need three wires. However, one is for +v, one for gnd and one for the data (the 1 wire bus). But… it gets even better, with this device you do not need the power wire (+v). The device can actually get its power from the data line. So why have the third power line at all? Well a micro-controller has a limit on how much current a data pin can supply and for the Arduino it’s typically around 20mA. So if you link up lots of sensors that all want power from the data pin you may run into problems, so at that point using a separate power rail to take the load off the Arduino data pin would be the way to go. For us though the sensor draws around 1mA from the Arduino, so well within limits even if we connect 10 of them up to the same pin. So our example will use just the two wires. If you do use two wires then both the Gnd and +v go to ground (Gnd).

Here is the wiring diagram and just after is the actual hardware itself wired up. Notice the 4k7 resistor connecting the data line to Vcc. This is a requirement of this chip and must be included. If you don’t then you will not a get a sensible reading back from the device.

The Software
Open up the Arduino IDE. We first need to install some libraries that will help us talk to the Dallas Temperature chip inside the probe. Typing in DS18B20 Github into a search engine should bring up he following git hub link:

https://github.com/milesburton/Arduino-Temperature-Control-Library

Click “Clone or Download”, then “Download Zip”. In your Arduino software go to
Sketch->Include Library->Add .Zip Library…
Navigate to your download location and add that zip file. That library gives us routines to talk to the sensor but not for communicating over the “1 wire” bus. For that we need another library, entering “1 wire github” into an internet search should bring up the following link:

https://github.com/PaulStoffregen/OneWire

Again download and install into your Arduino software in the same manner as the laast library. Next go to
Files->Examples->Dallas Temperature->Simple

and compile and upload to your device.

This example code should find your sensor and report the temperature back into the serial monitor. (launch serial monitor by going to Tools->Serial Monitor).

What about two sensors?

We said that all sensors have unique addresses on the 1 wire bus. So we should be able to add as many as we wish (or other 1 wire based devices for that matter). Look at the diagram  below to see the second identical sensor added.

It is a very simple matter of connecting the additional sensor in the exact same way as the first sensor. Note you do not add an additional resistor. I’ve altered the demo code we used in the first example to access multiple sensors, see below:

That should put you on the right track to adding sensors to your projects, obviously you’d want to add in a display device to make a project more stand alone. See this article on adding an OLED display to any project.