A Digital Temperature Sensor

There are digital temperature sensors we can use instead of the thermistors we have used up to now. Instead of reading a voltage with an analog pin, we read a serial stream of bits from the sensor on one of our digital pins.

One such sensor is the Dallas Semiconductor DS18B20. It uses what Dallas Semiconductor calls the OneWire protocol to communicate back and forth between the sensor and the tiny computer. It has three pins, one for power, one for ground, and one for the data link (it is this single data link line that gives the protocol its name).

The OneWire protocol requires a rather large amount of code to process. Thankfully, someone else has already written that code, and placed it in a library for us. Someone has also written code that uses the OneWire library to interface to the DS18B20. Rather than make you download and install both of those libraries, we have wrapped them (and some of our own code) into one library, so you only have to download and install our science library, science.zip.

Clicking on the science.zip link will start the download. To install it, pull down the Sketchmenu, expand the Include Library menu item, and select Add .ZIP Library:

In the dialog box that pops up, navigate to the science.zip file you just downloaded, and select it, then click on the Open button:

Our science library is now ready to use.

We connect the DS18B20 to +5 volts on the right pin, D2 on the middle pin, and ground on the left pin. A 10,000 ohm resistor is placed between the middle pin and the left pin (+5 volts) to pull the value on the middle pin up, so it sees a HIGH value instead of either LOW or an unstable shifting between the two values. The tiny computer has internal pullup resistors that could have been used instead, but the writers of the OneWire library chose not to use them for some reason.

Below is the program we will be using:

The file shown in the screenshot above can be found here.

Inside the science.h header file is the definition of a new class, called Temperature. It has the methods how_many_sensors()celsius(), and fahrenheit().

There can be several DS18B20 sensors connected to the same digital pin on the tiny computer. The how_many_sensors() method returns an integer telling us how many there are. For each one we can then call celsius(), or fahrenheit(), passing in the index of the sensor we want to read.

The output looks like this:

The digital temperature sensor has some advantages and disadvantages when compared to the thermocouple.

First, it is a lot slower to take a reading. The OneWire protocol needs to send many bits of data over one pin, and it doesn't do that very quickly. Second, the sensor is in a plastic transistor case, so it responds to temperature changes more slowly than a tiny thermistor would (although some waterproof thermistors are embedded in an even larger metal case that responds even more slowly). But the DS18B20 has an internal analog to digital converter that has 12 bits of resolution, compared to the 10 bits in the tiny computer. So the minimum temperature difference it can show us is four times more precise than the thermistor.

We might expect that the digital sensor would have a higher signal to noise ratio than the thermistor. To find out, we need to run a comparison. We can put both sensors on the same breadboard, and combine the two programs into this program.

We connect the sensors as we have previously:

The output looks like this:

Sometimes, the signal to noise ratios will be very similar. But if the temperature is changing a lot, the thermistor will appear to have a much lower signal to noise ratio. That is because it responds more quickly to temperature changes, so the standard deviation is higher: