Another approach to this problem that avoids the non-linearity and such:
Just use a temperature sensor chip.
I’ve recently been using a Microchip TC1047A. It’s in an SOT23 package (looks like a small SMT transistor). It has 3 leads: power, ground, and output. The output is a voltage that is proportional to degrees C. At minus 40 degrees C, the output is 100 mV. The output goes up 10 mV for every degree, so at zero degrees, the output is 0.5V. I think it costs about 50 cents.
There are all sorts of other sensor chips, some of which are in packages like TO-92, which is easier to handle (but bigger).
Guess I will use the:
"Well the voltage divider general equation is VOUT = VIN * R2 / (R1+R2) and you have VIN = 5V and R2 = 4.7K. Solving for R1, R1 = (R2/Vout) * (Vin - V out). "
method.
I know there are lots of other sensors out there but I need one that is tiny as possible and flat as possible. Those sorts of packages won’t work for me since they are fairly large.
Well for narrow and flat you could always go with the Analog Devices AD590JF which is about 2.3 mm wide and 1.2 mm thick. It’s output is 1 uA per degree Kelvin and it operates from -55 C to 150 C. The output IS linear. They are not inexpensive though.
Are you aware of how small an SOT23 is?
It’s 1.0mm thick, 1.30mm wide, 2.92mm long. If you drop it on the floor, you may have trouble finding it…
The TC1047A works from -40 to +125 degrees C. It uses only 35 uA of power. Using a 10K thermistor with a 4.7K resistor, you’ll be using about 10 times as much power (although it’s still small).
No conversion tables required, no resistors required.
It costs 47 cents in singly-unit quantities. How can you go wrong?
Its just not the type of package I am looking for… I need something with two pins, both next to each other… I will be sticking these inside of a computer and I need them to be a certain way in order for them to fit properly.
I will be ordering another sample from microchip soon so I will get a few for testing purposes.
Alright, seems my PIC has a 10-bit ADC module. What does 10-bit mean first of all? 1024 values (0-1023)? Would this explain why I am getting reads above 255? (256 is 8-bit analog i am guessing?)
So lets see here… Lets say I have a reading of 322. That would mean:
(5) * ((322)) / (2^10)) = 5 * (322/1024) = 1.57226562v
(guess thats where the 1024 comes from! 10-bit = 0-1023 then )
My multimeter returns: 3.396v Interesting… In order for that to be true, the Vref should be 10.7997019v but I don’t have that being generated anywhere :S
Oh wait a second!!! Silly ol’ me! I am measuring the voltage across the sensor! If I put the multimeter from AN0 (analog pin I am using) to GND then I get 1.568v Hell, what do you know? Thats a good enough resolution for me!
So now lets see…
R = V/I
V = 1.568V
I = .330mA = .000330A (very tiny measuring from 5v to sensor)
R = 1.568/.000330 = 4751.51515ohms
Would that mean I measuring the resistance of R2 (4.7K resistor) and that I am measuring the resistance wrong?
you are not measuring anything wrong, just your interpretation of what you are measuring needs some help. for the record I did give you the equation to calculate the thermistor resistance from the adc input voltage a few posts back. anyway, the A/D is always measuring with reference to ground, so yes you are measuring the voltage drop across the 4.7K resistor. that voltage drop varies as the thermistor resistance varies. since you like ohm’s law we can look at it that way and calculate the current in the 4.7K resistor using the I = Vadc / R2 (which is 4.7K). Since the current in R2 must be the same as in R1 (the thermistor), and you know the voltage drop across the thermistor is Vref - Vadc, you can calculate the resistance of R1 using dV / I, which is (Vref - Vadc) / (Vadc / R2) which is (R2 * (Vref - Vadc)) / Vadc. Using your values of R2 = 4.7K, Vref = 5V, and Vin = 1.57V this gives a thermistor resistance of 10,268 ohms, which is slightly less than 25C.
it’s just algebra. you can take it another level and maybe this is faster to compute
R1 = ((R2 * Vref) / Vadc ) - R2
the beauty of which is R2 * Vref is a constant so you could call it K then
R1 = ( K / Vadc ) - R2
which is one division and one subtraction. now if you were using a table to look up the temperature from the resistance you don’t even need to subtract R2 because you could just offset the Resistance in the table by adding R2 (again making use of the fact the table is all constants.)
Really you might want to step back a bit and write the equations deriving R1 as a function of the A/D value directly because you may be able to simplfy it even further.
Where
Ap = Aprime (A sub 1) = 3.354016
Bp = Bprime (B sub 1) = 2.569355
Cp = Cprime (C sub 1) = 2.626311
Dp = Dprime (D sub 1) = 0.675278
T = temperature in kelvin (C = K - 273.15)
R = resistance of sensor (R1) (this is my guess since they didnt define R)
Rref = reference resistance at 25c (being 10kO or 10,000ohm)
A-Dp are constants based on the B25/85-VALUE which for my sensor is 3977. This is mentioned in the “ELECTRICAL DATA AND ORDERING INFORMATION” table next to the 10,000ohm version.
Turns out this is based on the Steinhart-Hart equation. Wiki: “The Steinhart-Hart equation is a model of the resistivity of a semiconductor at different temperatures.”
This sensor seems to be using a modified version of it.
I tried this out and got nothing… At least nothing that looks remotely like correct temperatures.
I then tried using the natural regression (ln regression) since the equation uses lots of ln functions (logarithm with base of “e”)… I got an r of .99 and the resids seem to show that the function is off at the very low sub-zero temperatures… -40 is -50 in the regression which won’t do for me. When I ran a simulation of the regression i got, it didn’t work for some reason :s
I may use two equations. One from 0-150c and the other from -40 to -1c… dont know yet. Gunna try skewing my points around to get the regression to fit better. I wish I still had SPSS
I tried doing both regressions in a php script. Maybe my syntax was wrong? PHP has been known to not have correct log functions .
This is precisely why I like the idea of the integrated smart temperature sensors. Sure they may be a bit larger and/or more expensive, but a simple I2C or serial interface is so much easier to deal with than all of the math that makes my brain go
I don’t mind the math. After all, I will have a 20MHz processor doing it all later anyway I rather go through all this math (which I enjoy btw), than having to deal with a larger sensor. A small sensor is very important for me. Plus, I only have like 1 or 2 I2C ports which I can use, and I will need them both (for LED Driver and external 1MB EEPROM memory (to save my fonts and graphics, and keep user data/settings))
UPDATE:
I got the ln regression working. Turns out my identity was wrong… Its
logbase(x) = log(x)/log(base)
I had it the other way around X_x’’
The regression is worse than I thought. The temperature is low on both sides. (150 shows as 130. not good ><’!)
Guess I will have to figure out that large equation they gave… I think my problem is that my R/Rref is wrong… I am doing it as R1/10 (R1 is in kO, not O. 15000/10000 is same as 15/10 )
Remember, I2C is a BUS. You can and should connect more than a single device to each I2C BUS. You just need to be sure each device on an I2C BUS has a unique address.
You can put both of your devices on a single I2C BUS.
10 bits with all bits on (1) is the number of steps you can have generated by the ADC for results. An 8 bit resolution ADC could generate 255 steps and a 10 bit ADC could generate 2047 steps for the result. The higher the resolution of the ADC (in bits), the more accurate your measurment will be.
Each step would be 1/2047th of the maximum being measured for a 10 bit ADC. If you are measuring a 5V signal then each step would be approximately 2.44mV (5V/2047).
Just take the next higher power of 2 and subtract 1 to see how many steps you have for an N bit ADC. N = 11 for a 10 bit ADC.
How come its 2047? Where did that number come from? 10-bit… isnt that from
0000000000 - 1111111111 which is 1024 :s
(btw, whoever said I can have multiple I2C devices on the same bus… won’t work for me. I will need 6 of these sensors They will all have the same address.)
I was off by a power of 2 in my original reply. The bits are numbered from 0, not 1, counting to the left.
10 bits is 1000000000 in binary (2^10), which is 512 decimal.
1023 is 1111111111 in binary (2^10 - 1), not 2047.
So, you would have 1023 (2^10 - 1) steps of accuracy for a 10 bit ADC, not 2047 (which would be 11 bits).
I said that.
You have to follow the procedure to change the I2C address for other like devices that will be on the same I2C BUS. Look at the data sheet or other documentation for your sensor and it should detail the procedure for changing the I2C address.
If there were no way to change the I2C address for a device, there would be no point in I2C being a BUS that could handle many like devices.