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.
/*
My big problem before. The datasheet I was using before didn't say anything about 10 ^ x..
I downloaded a new one from mouser's website(place sf bought sensors) and they used these CONSTs.
I tried em out and what do you know? It works!
*/
#define AP 3.354016 * pow(10, -3)
#define BP 2.569355 * pow(10, -4)
#define CP 2.626311 * pow(10, -6)
#define DP 0.675278 * pow(10, -7)
#define RREF 10
float vadc, r, rrf, arg1, arg2, arg3, kel, cel;
vadc = (5 * (float)temp) / 1024.0; //convert analog to voltage
r = ((4700.0 * (5.0 - vadc)) / vadc)/1000.0; //get my resistance in kO
rrf = r/RREF; // get var i use 3 times
//calculate the arguments for my equation to make it look neater
arg1 = BP * log(rrf); //Turns out log() is ln()... they lied!!
arg2 = CP * (pow(log(rrf),2.0));
arg3 = DP * (pow(log(rrf),3.0));
kel = 1.0/(AP + arg1 + arg2 + arg3); // get the kelvin temperature reading
cel = kel - 273.15; //convert to celsius.
Written in C. Works just well!
My room is 25.1503c and my soldering iron’s tip is 110.3406c =D
The calculation time doesn’t take very long so its all good. I did order a couple of those “small” linear sensors from microchip. Should have them by Thursday I think.
Till then, these will do just fine!
I am a bit tired right now, so in the morning I will modify my code to take 100 samples and use the average analog value to get a better estimate of what the actual temp is. I see it deviating around a bit. Taking many samples helps even it all out But I am happy with the results!
Thanks for all your help guys Expect a topic up in 1-2 days on fan control using a MOSFET. I may have some equations with that later.
OK, just to throw a bucket of water on your fire (just kidding!):
If you have a 10 bit A/D, then you don’t have enough bits for 7.4 precision. I think your precision will be closer to 4.1, so your measurement above would be perhaps 25.15, plus/minus .02 degrees, or something like that.
Also, 110 degrees is really cold for a soldering iron - it’s barely hot enough to boil water. A hot iron is going to be between 300 and 400 degrees C.
But note that most types of sensors can’t handle that type of heat. If you want to do that, you could use an old-fashioned thermocouple instead.
I believe so yes, the sensor is ± like .05c or something like that. To be honest, this is pretty good for what I need. (If I wanted more accurate readings, I would of gone with an IR temp. sensor!) The calculations are like .001 away from what the temp should be (based on the table in datasheet). There is always some sort of error in everything. So its not that big of a deal. It will be a big deal when the reading is 60c when its really 100 or vice versa. Being off by .5-.005 isnt much…
For my purposes, I will be rounding to 2 decimal places and taking 100 samples. This will ensure a better reading.
As for the iron… Do you REALLY expect a $8 radioshack iron to get to 400C? Seriously!! I wouldn’t! I am surprised it can get THAT hot.
When I start some real solder work (SMD) I will invest in a good quality $60-$70 iron with digital display and heat control Till then, I can solder a few wires perfectly well with this iron.
EDIT:
Small question. The datasheet of this sensor says:
Response time 1.2 s
Thermal time constant τ(for information only) 15 s
Does that mean it takes 1.2s before the sensor can notice a change in temperature? :s What delay should I add between samples?
My point on the resolution is that you cannot get any more resolution out of a system than the lowest-resolution thing in the system, which is your 10-bit A/D.
As an example:
Suppose that your sensor works from 0 degrees to 1023 degrees. That would mean that best case your resolution is ± 1 degree.
A more realistic example:
Suppose your sensor is accurate from -40 to +85 degrees C. That’s a range of 125 degrees. 125 / 1024 = 0.12 . So your accuracy will be no better than ± 0.12 degrees, and that’s the ideal case, which assumes that you have the sensor’s output scaled to exactly fit in the voltage range of your A/D, which is probably not the case.
In the real world, you need a very expensive ‘laboratory’ sensor to get that kind of accuracy and/or precision. Otherwise, you’ll be doing very well if you get within 1 or 2 percent.
A side-note on ‘accuracy’ vs ‘precision’. Accuracy is an indication of how close you are to the “real” value. Precision is a measure of how repeatable your measurement is. So accuracy can be stated with a single measurement, but precision requires a series of measurements.
Note that it’s possible to have high precision but poor accuracy. For example, if your measurement is always high by 10.0%, then you have good precision but lousy accuracy.
Lecture over