## Friday, July 13, 2007

### Temperature Logging Part I

There have been many times when I've wanted to log temperatures. No useful reasons, I just like collecting data. The problem is that temperature logging devices are like \$50 and up and of course they only do that one thing. After the orgy of temperature data collection is done, it just sits on the shelf.

But I'd been hearing about this Arduino dealie, which is an open-source microcontroller that only costs about \$35. I figured that I could put a thermistor on there and make my own logger, which would not only be cheaper but I'd also have a versatile device for other projects. I've never used a microcontroller before, plus electronics above the level of a flashlight circuit confuses me, so it's been an adventure. Fortunately the Arduino is supereasy.

For that matter, the electronics of this were prettyeasy. The hardest part was doing the math to convert a thermistor reading into a temperature one. I harvested a thermistor from a broken (except for the thermistor) electronic thermometer. (It was easy to identify, since it was sticking way up away from the rest of the circuit board and and had "therm" written next to it.)

A thermistor varies its resistance based on temperature. That means that if you apply a voltage, you'll get a varying amount of current out. But the Arduino input port measures voltage level, which means you need to turn a varying resistance into a varying voltage instead. That's easy with a voltage divider.

"Th" is the thermistor, and A1 is the steak sauce Arduino analog input port. The total voltage drop across both resistors is a constant 5V, but how much drops across each resistor depends on the ratio of their values. So when the thermistor changes, the voltage at A1 also changes.

Now you just need a way to convert a voltage reading to a temperature reading. I assumed it would be a linear relationship, but it isn't, it's exponential. When you buy a thermistor, apparently they give you two numbers. A baseline resistance (at 25°C,) and a number B. Apparently you are supposed to know this equation:

RT = RTzeB(1/T - 1/Tz)

Oh sure, THAT equation! RT is the resistance of the thermistor, which you can figure out based on the read voltage and known values of the circuit. RTz is the resistance at 25°C (or whatever baseline), which is Tz. T is the temperature you want and B is the magic number.

I didn't get my thermistor from any fancy-shmancy store so I didn't have the magic number. Instead, I used the thermistor to measure hot and cold water (calibrated by a kitchen thermometer). Plugging in those readings I was able to solve for B. Just using math got me within 2 or 3 degrees (over the range 0-100°C--I haven't tested outside that range). Then a little empirical fudging got me right on the money.

1. Me, checking in again! I needed this and looked it up on my own blog!

Va = V * (r2/(r1+r2))

Where Va is the voltage at A1, V is 5V, r1 is the top resistor and r2 is the resistor I'm trying to measure.

The Arduino is going to read the input port and tell me, in increments of 1024, how close it is to 5V. So ideally, I'd like the max value of r2 to render r2/(r1+r2) = 1, meaning r1 should be small. The min value of r2 should give a total of close to 0, meaning r1 should dominate for r2 on the small end.

For my current needs, the varying resistor (a photoresistor) registers around 25k in the dark and around 200 in the light. If I use a 1k resistor for r1, then Va should vary from...5 * 200/1200 = .8V to 5 * 25000/26000 = 4.8V. OK.

2. Further note to self: Measure the voltage across the photoresistor, not the resistance.

3. Heh, checking in again, almost exactly a year later. This blogonet is turning out pretty useful...