In the last issue we took a general look at Analog to Digital boards and an example project building an inexpensive multiple input switch. In this issue we will continue our review of A2D boards by looking at connecting temperature sensors and dealing with the resulting resolution issues.
Sensing temperature is a staple use of A2D boards. You can make a temperature sensor using a variety of readily available components. For example, you could use a thermal resistor (or thermistor). These devices are based on the fact that the resistance of most materials change with temperature. Thermistors are among the oldest temperature sensors know, first demonstrated my Michael Faraday in 1833. A thermistor changes its resistance according to its temperature. Unfortunately, they are not linear devices, and require compensation for both the non-linearity and other temperature sensitive components required to complete the sensor.
Another method of sensing temperature is by use of a thermocouple. A thermocouple generates an electrical potential from heat. They are made from two dissimilar metals and when the junction is heated an emf is created which is related to the temperature. This potential is small, with coefficients of about 50uV per degree C.
One of the easiest types of temperature sensing devices to use is an IC temperature sensor. These devices normally require a minimum of external circuitry and are fairly inexpensive. Two popular temperature IC’s are the Dallas Semiconductor DS1820 and the LM34/35 by National Semiconductor. The DS1820 is a complete digital thermometer complete with high and low temperature alarms. It utilizes the Dallas Semiconductor 1 wire digital interface for communications to the host hardware. This device does not require an analog to digital converter. It generates digital output directly. Its interface is also proprietary to Dallas Semiconductor and restrict you to using their devices.
The National Semiconductor LM34 (or LM35 for centigrade) generates an output voltage which is directly proportional to temperature. It is also a very popular IC and is the basis for many commercial devices. One example is the Model 1522 temperature sensor sold by SmartHome.Com which sells for about $20. The LM34 sensor alone sells for $1.75 for the 32 to 212 degree version or $4.95 for the -40 to 230 degree version at Jameco Electronics (www.jameco.com). The LM34 can be connected to an A2D board with no other parts if desired. Its cost, availability, and ease of connection make it ideal for our purposes. The rest of this article will focus on connecting the LM34 to an A2D board, and generating useful output from it.
The LM34/35 Temperature Sensor
The LM3x sensors produce 10 milli-volts of electricity per degree of temperature. It is a three pin device; one for supply voltage, one for ground, and one for output. Connections are as shown in the diagram at right. The three wire interface used to operate the LM34 can be run more than a hundred feet without significant signal degradation. It is a good idea to add a 0.1 uF capacitor connected at the A2D board input. This helps improve the stability of the measurement by reducing the effects of noise picked up on the Signal line.
Connections to your A2D board are very straight forward. Connect the +Vs pin to plus 5 volts and GND to ground. Vout or Signal is the output from the temperature sensor and represents the temperature at 10 mV per degree. For example, if it is 78 degrees (Fahrenheit) the output from the temperature sensor is 0.78 volts. An analog input of 0.78 volts corresponds to a digital value of 40 (0.78 / 0.0195) which is what you read from the board when the conversion completes.
To convert the digital value to a number which represents temperature you reverse the conversion process. In this case multiply by 1.95 (0.0195 * 100). The 0.0195 is the voltage per step we computed earlier. The 100 converts from the voltage domain to the temperature domain (10 mV or .01 V per degree). For details on the conversion process, see Analog to Digital Primer in last issue’s article.
If you look closely at the computations for conversion to and from the digital domain, you will notice that the temperature must change by 1.95 degrees in order for the A2D output to change. Although simple, this is not exactly an optimal solution. The problem lies in too much data being put into too small of a voltage range. Sometimes there is nothing you can do about it, but in this case we can improve things considerably.
Data Resolution and Signal Conditioning
An 8 bit A2D converter has 256 steps. If you take your 5 volt input range and divide it by 256 you find that each step corresponds to 0.0195 volts. If you relate this back to the LM34 sensor you find that each step represents 1.95 degrees. This is also known as the A2D’s resolution. The question is, how can we improve this result? You basically have two choices; either change the input to better match the A2D board or change the A2D board to match the input. Although some A2D boards allow you to alter in input voltage range this is not always possible or desirable and so we will choose to adjust the LM34’s output to better match the A2D and our requirements. These techniques can be used with equal effect on any analog signal you may wish to acquire.
To match a sensor’s output to an A2D’s input conversion range you can do two things; shift the sensor output to a new voltage range or multiple the voltage (possibly by less than 1) to expand (or compress) its dynamic range. The LM34C produces an output between -0.4 volts and +2.3 volts over its full temperature range. This corresponds to a maximum spread of 2.7 volts. Our A2D has an input range of 5 volts. If we matched the sensor’s output to the A2D’s input we would gain almost a factor of 2 in resolution. Lets ignore the negative values (temperatures) for the moment, giving us a sensor output range of 0 to 2.3 volts. This allows us to safely amplify the input signal by 2 which results in each degree now producing 20 mV instead of 10. A single step of the A2D still represents 0.0195 volts, but 0.0195 volts now represents only 0.975 degrees. This is a much better resolution for a temperature sensor, but we can do better!
For indoor temperature sensing a more realistic measurement range is 0 to 100 degrees. This reduced temperature range corresponds to a voltage range of only 1 volt. Now we can amplify the LM34’s output by 5, resulting in an A2D resolution of only 0.39 degrees! Not bad. In fact, given the accuracy of the A2D and the LM34, its probably as good as we are going to see anyway.
So, how do we amplify the sensor’s output? We build an amplifier. This is actually pretty easy for amplifying DC voltages. The circuit at right utilizes an LM10CL operational amplifier to increase the LM34’s signal by a factor of 4.7. The LM10 is also made by National Semiconductor. It is a single supply Op Amp that operates on a voltage of between 1.2 and 7 volts (no negative supply required!). The LM10 comes in a variety of packages. The CL designation indicates a maximum supply voltage of 7 volts and an operating temperature range of 0 to 70 degrees centigrade.
You can use this modified sensor indoors and outdoors as long as you don’t expect the temperature to drop below 0 degrees (Fahrenheit or Centigrade depending on the model). Even if it does go below 0, the output of the amplifier will just sit at 0 until it warms back up.
The other pins on the LM10 that are not shown in the circuit are the Reference Output, Reference Feedback, and Balance. These are not used and can be left unconnected. Also not shown are the V+ and V- connections. These connect to +5v and ground respectively. The choice of resistor values (10k and 2.7k) gives a little more room at the top of the temperature scale and uses standard resistor values. You can adjust these as necessary to get other gain factors.
Since this circuit consists of only three parts it is easily built on a piece of perf board. You can also place the LM34 on the same piece of perf board and make a complete sensor package (see picture at left). In any event, it is best if you can place the amplifier near the sensor. If not, you will amplify both the signal and any noise you pick up on the wires on the way back to the A2D board.
Now lets get back to negative temperatures. Our assumption for the temperature sensor outlined above was temperature sensing in the range of 0 to 100 degrees. If we want to observe temperatures below 0 we have two problems. The first is that the LM34 produces negative voltages (with respect to its ground) for negative temperatures. Since we are using +5 and Ground as the sensor’s references it can’t generate a negative voltage. The second problem is that even if the sensor could produce a negative output most A2D converters will not accept an negative input.
One way to correct both of these issues is to raise the sensor’s ground reference by about a half a volt. This has the effect of shifting the sensor’s output by that amount. Of course you lose this shift on the top end, but we have voltage to spare at the top. If we shift the output by 0.5 volts the LM34 processes negative temperatures by producing outputs starting at 0.5 volts and going down to 0 instead of starting at 0 and going down to -0.5. At the high end the voltage scale we lose 0.5 volts, but the LM34 does not produce outputs above 2.3 volts anyway.
The schematic above shows one way to achieve this result. The two 1N914 diodes create a ground reference for the LM34 of about 0.9 volts. This has the effect of shifting all the outputs of the LM34 by that amount. For example, if the temperature is 78 degrees, the sensor will output 1.73 volts instead of 0.78 volts. This allows temperatures all the way down to -50 degrees to create a positive signal. Signal1 is the shifted output from the sensor and Signal2 is the amount of the shift. Both signals must be connected to separate A2D inputs. To get the correct temperature, subtract Signal2 from Signal1 after the A2D conversions are complete. This allows for proper handling of drift in the LM3x ground reference. The 1N914 diodes’ voltage drop are very sensitive to temperature. It is also necessary to amplify both signals so that the subsequent signal subtraction makes sense. You can’t subtract the signals electrically before A2D conversion since this would result in a negative voltage input to the A2D.
Building the second circuit is a bit more complicated than the first, but it is still suitable for perf board construction. The LM10s should be kept indoors since their operating temperature range does not include anything below zero. They should also be kept as close to the sensor as possible to avoid amplifying noise picked up on the wires.
In order to convert the digital value received from the A2D board into a value meaningful for the sensor, you must reverse the conversion process. The inputs into this conversion process are:
Sensor Output: 10 mv per degree F.
A2D Input: 0 to 5 volts
A2D Output: 8-bit.
To convert digital A2D reading to degrees:
Resolution = A2D Input Range / Output Range or 5/256 = 0.0195 volts per bit.
Analog Voltage = Digital value * Resolution (0.0195)
Temperature = Analog voltage / Volts per degree (.01 V per degree)
– or –
Temperature = Digital Value * 0.0195 * 100
Temperature = Digital Value * 1.95
These are the nominal values. Some of the constants are not quite accurate and can add error into the computation. Specifically, the A2D range is actually between 0 and VRef+. If you measure VRef+ on your A2D board you will probably find it is a bit off from 5.00 volts. In my case it measured 4.92 volts (mostly due to voltage regulator tolerance). Substituting 4.92 gives a multiplier of 1.92 instead of 1.95. For a temperature of 76 degrees the error amounts to a computed temperature of 78 instead of 76 degrees (remember our resolution is only 1.9 degrees).
Computing the conversion multiplier for the amplified signal requires factoring in the gain of the amplifier. For a gain of 4.7:
Temperature = Digital Value * 1.95 / 4.7
Temperature = Digital Value * 0.4148.
The gain of 4.7 is computed. It should be measured for best accuracy. To do this, measure the output of the temperature sensor (pin 2) and the output of the LM10 (pin 6). To compute gain, divide the LM10’s output by the sensor’s output. For my system, the temperature sensor’s signal output was 0.72 volts and the LM10’s output was 3.39 volts for a gain of 4.708. This is pretty close to the predicted value, but it could be off by as much as 10%. This gives a conversion multiplier of:
Temperature = Digital Value * 1.92 / 4.708
Temperature = Digital Value * 0.4078.
Again, for a temperature of 76 degrees, the nominal values give a temperature of 77.15 degrees. The calibrated values give a temperature of 75.85.
It should be noted that the LM34 is tested to be accurate to within +- 1 degree. You will have some variation between sensors regardless of the accuracy of your conversion factor.
Adding temperature sensors to your home automation system is fun and the first step toward automating and taking control of your HVAC system. The techniques presented here can also be used with good effect for virtually any analog input. It is very unusual for a sensor to produce an optimal output without any conditioning at all.