Friday, March 7, 2014

DS18B20 Digital Temperature Sensor

DS18B20 Digital Temperature Sensor


I drove to Sparkfun, up there north of Boulder to pick up my order of parts, including this very
handy digital temperature sensor.

In the last post I provided links to two different items, but on further inspection they are the same basic component, sold both as an individual TO-92 package and also as a waterproof sensor assembly with about 3 feet of plastic-jacketed three conductor cable.

I had read the comments at Sparkfun about this assembly, and several people were "in the dark" about identifying these leads. One person actually managed to burn one up, and then cracked it open to visually identify which wires were attached to which pins.  

The datasheet link for this assembly points to the same datasheet as for the assembled unit, so there was NO official data on the wiring of the extension cable.

I found a black lead, a red lead and a white lead, and I went with the configuration mentioned most in the comments. 

Red Wire - Vdd      +3.3 or + 5 Volts 

Black Wire - GND

White Wire -DQ      data in/out -- requires 4.7K pull-up resistor

I searched online for some Arduino code and found a good article on bldr.org

One Wire Digital Temperature. DS18B20 + Arduino

This digital device employs "1-Wire Bus", which is to my mind a catchy but misleading name.

There are two methods of hooking up, a "parasitic" mode that requires only TWO wires, VDD and DQ.
While you can save money on wire this way, there are some limitations, such as a smaller number of sensors on the bus, and the requirement that the GND pin of the sensor be connected to "ground"

For the electronic hobbyist, it's not going to be just "1 Wire"...you'll need to provide a common ground/signal return CONDUCTOR...and it's probably going to be a wire. 

The other, more costly in terms of conductor number and length, is to use a three conductor cable running the full length of the run from the sensor to your measuring device. The data sheet describes this in detail

Each device has a serial number burned into it, and it sends that serial number along with the encoded temperature data.

The idea is that you could have several of these on the same "1-Wire" bus and get data from all of them, or choose one specific sensor to read. It seems that a multi-point temperature datalogger could be cobbled together this way.

OK, I got it working on my Arduino and then modified my own space heater thermostat sketch to use this sensor as feedback for temperature control.

It's basically on/off control, but when it is ON it is sending PWM (Pulse Width Modulation) to the SSR (Solid State Relay) that sends 110VAC to the heater coild.

ON/OFF control is the easiest to implement. it's basically what controls your home furnace.
I was concerned about the space heater getting too hot inside my little greenhouse on a table, made of wood and plastic sheeting. 

Areas of Concern


First, I didn't want the mechanical OTC (Over Temerature Cutout) on the heater coil assembly being tripped. It's a safety device - the last resort - and I didn't want to wear it out.

Second or perhaps coequally, I didn't want to melt the plastic sheeting or start a fire. "Bad form" as the Brits say.

Third, I didn't want the heating coils cycling too fast, similar to an automobile, racing from stoplight to stoplight at full throttle. That constant heatup/cooldown causes thermal expansion/contraction of the coils, which shortens their potential service life.

And Fourth, or perhaps coequally with Third, I didn't want my heater to consume more electric power than necessary. That stuff costs money! 

Off Topic: Did you know that most forms of generating electric power are very inefficient? The MOST efficient commercial plants, excluding solar and wind power, have a thermal efficiency of less than 50%. That's right, coal burners, natural gas turbines, even nuclear plants. 

That means that for every megawatt of electric power produced, AT LEAST one megawatt of  "waste heat" is being dumped into either the air or the water near the plant. 

With a PWM signal from the Arduino, it's possible to "duty cycle" the coils. I started out with a 100 millisecond cycle for simplicity. For a 25% duty cycle pulse train, simply have the PWM output pin HIGH for 25 milliseconds of every 100 milliseconds.

With the RTD and the thermistor sensors, the input to the Arduino was bouncing around a lot.
I was adjusting both the high and low limits, the "deadband" of this electronic thermostat.

And with the low resolution, I had to have the high and low limits only one digit apart...a deadband of about 4 degrees C.

I also adjusted the duty cycle. The SSR datasheet states it cannot "change state" faster than 10 milliseconds, so when I got to 10/100 I began increasing the denominator, the duty cycle length.
At one point I had it at 500 ms with an on time of 25 ms. 

It really didn't work very well, and changes in sunlight (through the window) would cause the temperature to climb or dive inside the greenhouse. That kind of trial and error method is time consuming, especially with temperature control. I spent a lot of time babysitting my greenhouse, reading up on available alternatives.

This new sensor was a welcome change of pace!

When "read", the DS18B20 outputs a 9 byte string of binary digits that contains its individual serial number and temperature (with two decimal places) in degrees C. That made it easy to set high and low limits in my sketch. 

It can be read many times a second, but there is a pronounced lag time between applying heat to the waterproof plastic casing around the sensor and the sensor inside seeing it. For the purposes of my greenhouse, that's okay. The temperature of the entire greenhouse is what I want to control, and it probably can't change faster than the sensor can sense it. 

I should mention additionally that the setting for the heater is about 25 degrees warmer than the temperature I want for my plants. The instructions for tomatillos recommend soil temperature of 80 degrees F for good germination. The greenhouse loseses heat to the cooler surroundings of the room. One of my many "next steps" is to insulate the greenhouse more fully.
It works like a dream, how gratifying! Finally it was functional enough to fine tune the limits and the PWM duty cycle. It ran overnight, and today I'm hoping for more sun to see how the system responds to changes in solar heat input.

Here's a link to today's latest version of the sketch:  Arduino_Rd_Sensor_DrivePWM 

I downloaded Fritzing today. I'll work on getting the setup fritzed and showing it to you soon.

















No comments:

Post a Comment