A simple diode or the output of a transistor bias circuit can be used as a rough temperature sensor. For this purpose, a calibration procedure using a precise temperature sensor is recommended. This article shows how to calibrate such a temperature sensing circuit using an Arduino and the SHT15 Sensirion temperature/humidity sensor module.

## Temperature Sensors

"Every sensor is a temperature sensor; some measure other things as well"

Elecia White

This is one of my favorite quotes as it relates to circuit design. With electronics, most physical quantities vary to some degree with temperature, even if that was not your primary or intended measurement. Often, these thermal effects are a nuisance, but we can sometimes turn them into useful information. Take the circuit in Figure 1 for example. This is part of a project I'm currently working on (more on it soon). Normally, there's an AC component riding on the emitter voltage of the transistor amplifier along with a DC bias voltage. Now imagine the AC input to the amplifier is turned-off. What you are left with at the output VOUT is just the VE (Emitter) bias voltage *minus* the voltage drop through the Schottky diode. The primary use for the circuit is to measure the AC level, but if I turn the AC source off, I can also measure the DC component. Can we extract useful temperature information from this DC measurement? As we will see sortly, turns-out we can. This is a technique that I suspect one can apply to many circuits. Besides measuring whatever it is that you are measuring, you can also get a temperature measurement. Granted, this won't be a precision sensor but the information comes essentially "for free" and can often be useful. Let's see how this is done in practical terms.

Figure 1  - The Circuit

## Calibration

So the idea is simple: periodically turn off the AC component to the circuit and take a couple of measurements of VOUT (DC component only). We can then infer from VOUT what is the actual ambient temperature. (Note: this also assumes the current through the diode and the amplifier circuit is small to where self-heating effects can be neglected). In my application, VOUT is connected to an ADC input in the Arduino Nano, so this is easy to do in software. However, how do I know what temperature corresponds to a given voltage measurement? For that, we need a precise temperature sensor that one can compare against. A sort of reference/calibration tool.

As luck would have it, the good folks at Embedded Adventures shipped me a very nice Sensirion SHT15 temperature/ humidity sensor module for my projects (Disclosure: I'm not affiliated with Embedded Adventures though they did give me this module for free).  This is a very nice module, precise to about  +/-0.3 C typical with a 14-bit internal ADC resolution. The worst-case accuracy is a bit higher than typical (as expected) but still  below +/-1 C for the ambient temperature range I'm interested in. It's a good "tool" to have in the lab for accurate temperature measurements with Arduino prototypes. Alas, t<span >he module is very easy to connect to the Arduino. Figure 2 shows how I connected it to an Arduino Nano using only four wires GND, VCC, SCK, SD. There are some good web tutorials on how to use these Sensirion sensors with the Arduino. On the software front, I recommend Jonathan Oxer's excellent Arduino  library which makes reading temperature and humidity really easy (a simple function call).

Figure 2 - SHT15 Module Connected to Prototype Board

To collect my calibration data I used the following Arduino sketch which you can download from the link below. The software produces a measurement every minute for both Voltage (V measured) and the Sensirion provided temperature (T measured) and displays it in the serial console. I actually averaged the results over eight samples to reduce "noise" in the measurements. Notice that these modules take a maximum of about 340 ms to return a measurement, so averaging over many samples can take some time...

Next, I just simply let the program run over a 24 hour cycle and imported the results to a spreadsheet. Tip: use a serial terminal software such as "CoolTerm" that allows you to save directly to a text file. Then rename the file with a .csv extension (rather than .txt) and Excel will read it directly into two separate columns. A better approach of course would be to put the setup in a thermal chamber but I didn't have one handy. The backyard patio was my "thermal chamber":)

Figure 3 shows my overnight test setup. Notice I used an external supply connected to the Arduino Vin input rather than the USB supply. There are two reasons for this. The first one is that the final project will also be using an external supply through the internal regulator (connected to Vin), so whatever temperature variations produced by the regulator ought to be reflected in the data I collected. The second reason is that, with the Arduino Nano board, the USB supply goes through a Schottky diode in series which reduces the "5V" rail by about 0.3V and adds another temperature sensitive variation that will not be in my final project. For these reasons, it is better to calibrate using an external power supply feeding Vin.

Figure 3  - Calibration Test Setup

## Analyzing the results

After running the temperature cycle explained above, I ended-up with a long table with (V measured , T measured) pairs. See Figure 4 below. temperature is expressed in Celsius under column B:

Figure 4 - Imported Data (Left: Voltage; Right: Temperature)

The approach I used was to construct a linear regression model from this data. In other words, compute a formula in the form:

T estimate = m * Vmeasured + b

Where "m" and "b" are the linear regression parameters. Once these are computed (by the method of least squares), it's simply a matter of applying the equation above to a given Voltage measurement and "voila'" you have a Temperature estimate.

Computing m and b as it turns-out is the easy part. Excel can calculate this directly with built-in functions. Estimating how much error you have in that measurement is slightly more complex but we will also tackle that in Excel using some statistics functions. The error estimate for linear regressing given an input x (here the voltage measurement) is called a "Prediction Interval". Once you specify a confidencle level (say you want to be 99.9% confident in the Prediction Interval), the actual interval is calculated using the Student t distribution and the following formulas:

I won't bore readers with all the math, but for those interested, there's a very good article on this topic at the excellent "Real Statistics Using Excel" website. The spreadsheet I constructed calculates all these parameters using standard Excel functions (no VBA code required). See Figure 5. The user must first enter the measurement values in columns A and B and adjust the formulas if needed to capture the correct range. As depicted in Figure 5, the spreadsheet requires three main inputs. The "Measured V" is a given Voltage measurement from which we want to predict the temperature. The confidence level and Sensor Error are used only for the prediction error estimate as discussed above and are optional.

The results include the "m" and "b" regression parameters (which you can use directly in your software). The "Regression Error Prediction" is estimated and turned-out to be about +/-1.75 C. Now one has to account for the fact that the SHT15 itself has some error. Error variance tends to add as sum of squares (assuming a normal distribution which is a reasonable assumption) so the "Total Error" is calculated as:

Total Error = sqrt( Regression_Error^2 + Sensor_Error^2)

I call this the "Optimist Error". A more pessimistic error simply adds the two error sources. Either way, we are looking at a temperature estimate with about a +/-2.5C error which is not going to break any accuracy records but can be useful nonetheless.

The following chart shows the measurements along with the regression line (T estimate in orange). One interesting observation is that the measurements are clumped in vertical groups spaced about 5 mV apart. This is no coincidence since the Arduino ADC resolution is only about 5 mV (5V/1024). In Figure 5, I also show a field named "Voltage slope" which is the voltage change per degree C. This turned-out to be about 4.7 mV / C which makes sense considering that we essentially have two diodes in series: the Base-Emitter junction in the transistor (A BC 547B) and the BAT54 Schottky diode. Theory tells us a diode's forward voltage changes by about 2 mV / C (depending somewhat on the diode), so you would expect abut 4mV/C with two diodes in series.

Figure 6 - Plotting the Results

## Final Thoughts

There are some possible improvements that could result in better accuracy. You could approximate the values with a quadratic (rather than first-order) function for example. One could also build an automatic self-calibration routine in the Arduino that computes these values in the ATMega. For a rough estimate of the ambient (or operating) temperature, this can be a useful technique that I suspect can be used on a number of projects by simply adding some code to the existing hardware.

Comments, questions, suggestions? You can reach me at: contact (at sign) paulorenato (dot) com