![]() |
![]() |
![]() |
|
|
|||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
|
|
Thread Tools | Display Modes |
|
|
(permalink) |
|
Hi, I am working on a project and I am trying to use the lm335 temperature sensor to display the ambient temp on an lcd. I have the lcd working but the value I have for tempurature is 8128 with no calibration at room temp. Note that when I attach ground to the analog input on my pic, it reads 0 as it is suppose to. This is not the tempurature but just the value the pic is reading from the sensor I think. I am using a pic 16f88 that has a/d conversion(10bit if i remeber correctly) in it already. As of right now, I have the Porta.0 as the analog input into the pic. So to get to my question, I have researced everywhere, but can not seem to figure out how to calibrate the sensor and it is causing me much grief.
On my circuit I have 5v running through a 2.2k resistor that goes into the input pin(pin2) on the sensor. Pin 1 of the sensor is to ground and pin3, the adj pin is running from the sensor to my analog input pin(pinA.0) on my pic. I know the sensor is working for the output voltage at 25 degC is about 2.99v which falls within the range specified on the data sheet. Also When I hook up the multimeter to check the voltage of the pin3 on the sensor, the voltage goes up as it gets warmer and down when colder. The problem is is that my pic is not updating the temperature in a predictable manner on the lcd. The lm335 has a sensitivity of 10mv/K and this is where I am concerned. I think I may need to uses a difference op amp or the problem may lie in my code. If anyone would like to clarify this for me, it would be greatly appreciated. I do not think that these sesors should be to hard to figure out but for me, it has been aggravating lol. Attached is my code in picbasic pro, hopefully somebody could point me in the right direction as to calibrate the sensor or how to get it to work. Once again thanks for any insight on this. Jonathan |
|
|
|
|
|
|
(permalink) |
|
Whoops I forgot the code in previous post. Here it is
DEFINE OSC 8 'Sets to 8Mhz DEFINE ADC_CLOCK 64 'Set clock source (rc=64) DEFINE ADC_BITS 10 'Set number of bits in result DEFINE ADC_SAMPLEUS 50 'Sets sampling time to 50 microseconds OSCCON.4=1 OSCCON.5=1 OSCCON.6=1 cmcon.0=1 cmcon.1=1 cmcon.2=1 cvrcon.6=0 cvrcon.7=0 i VAR BYTE temp VAR WORD 'create temp to store result x VAR BYTE 'number of steps direction VAR BIT 'forward or reverse motion size VAR BIT 'sets steps as half steps index VAR BYTE tempy VAR word buzzer VAR PORTA.4 'renames PortA.4 as buzzer phi1 VAR PORTA.1 phi2 VAR PORTA.0 phi3 VAR PORTA.7 phi4 VAR PORTA.6 'line20 onswitch VAR PORTB.2 'renames the ports as switches stepswitch VAR PORTB.3 ansel= %0000001 'Turns on channel 0 TRISB=%10001100 'sets PortB.2,3,7 as inputs and rest outputs....40 TRISA=%00000001 'as of now setting PORTA.3 as input ADCON1=%00000001 'Sets PORTA.3 as analog input (right justified?) ADCON0=%11000001 ADCIN 0, temp 'Read channel 0 to temp Pause 500 'Wait .5 sec loop: ADCIN 0, temp Pause 100 If (temp>=0) Then tempy=15*(temp-8128)/192 'may be wrong calibration serout PORTA.2,2,[$FE, 1] 'clears screen line 49 serout PORTA.2,2,[" The Temp is"] 'sends the string The Temp is Pause 500 serout PORTA.2,2,[$FE, 1] serout PORTA.2,2,[ #tempy," Degrees F"] If (tempy<60) Then buzzer=0 Else buzzer=1 Pause 1000 buzzer=0 Endif Pause 500 Goto loop Endif End note the 8128 value corresponds to the variable temp. tempy is what I am trying to calibrate Once agan thanks for any help Jonathan |
|
|
|
|
|
|
(permalink) | |
|
Quote:
With your current setup, you are using the 5V rail as your reference and so 1 bit of the ADC = 5mV = 0.5C (Actually 5/1024). You could improve it with an opamp but as you sensor is only accurate to 1C it hardly seems worth it. Also, because your sensor is referenced to 0K and is just a slope output, if you calibrate it to give the correct reading on the LCD display at a known temperature then, it should be correct at all temperatures. You effectively set the slope to 9.76mV/K which is 2*5/1024. This will also correct for power supply errors. If your supply is 5.1V, this will be calibrated out by the above procedure. Just make sure your supply is stable. In you code above, temp/128 would be the temp in Kelvin and so temp/128-273 would be Celsius. You can do the Farenheit bit. HTH Mike. |
||
|
|
|
|
|
(permalink) |
|
Pin 2 is supposed to go to the ADC input, not the ADJ leg. Get a copy of the datasheet.
I don't even use the ADJ leg since the adjustment offset is constant. Mike |
|
|
|
|
|
|
(permalink) |
|
just a tip for you, one easy way to make the math for the temp sensor simpler is to use a resistor divider tree, with two potentiometers and 3 resistors, and use that to set the + and - Vrefs for the ADC input (to the two Vref pins on the PIC)
For mine, I set the - reference to the voltage representing 0 degrees C, and the + reference to the voltage representing 100 degrees C, if you want higher temp range adjust those voltages accordingly. then your ADC value will represent the full range of temperatures you have chosen, instead of having a huge offset of 2.7 or so volts for zero degrees and severely limiting your resolution (in mine, I have the ADC's 10 bits spaced across 1 volt, instead of 10 bits spaced across 5 volts) it also makes it easy to calibrate, because you can stick the sensor in ice water and adjust the lower pot so the PIC just reads zero, and then stick it in boiling water and adjust the upper pot until the PIC just reads maximum. (0x02FF or 0xFFC0 depending on your left/right justification)
__________________
EEgeek.net |
|
|
|
|
|
|
(permalink) |
|
Just a word of warning, check the PIC datasheet for details on possible loss of accuracy when using Vref far from 0V and 5V.
Secondly, don't set it for 0-100 degrees full scale, set it for 0-102.3 degrees, that way you don't have to do any maths to display it, it's already correct (except for manually adding the decimal point). This may be what evandude ment?, having to convert from 1023 to 1000 is rather pointless, and ruins the nice 0.1 degree display resolution. |
|
|
|
|
|
|
(permalink) |
|
Actually nigel I hadn't thought of that. Since the project I was using the sensors for was only to turn computer fans on and off, I wasn't very concerned with accuracy so I was only taking the most significant byte of the A2D result anyway... but doing zero to 102.3 is certainly a great idea when you have to display it!
__________________
EEgeek.net |
|
|
|
|
|
|
(permalink) | |
|
Quote:
|
||
|
|
|
|
|
(permalink) |
|
Thanks for all of the replies. However when I tried to set my code to be right justified and then connected my pin 2 on the sensor to my ad pin on my pic and it reads a constant 7 on the display. Am I on the right track? Thanks for any help
|
|
|
|
|
|
|
(permalink) |
|
Just to recap...You have 5V going through a resistor to pin 2 of the LM335. Pin 2 also goes to the ADC of the PIC. The minus leg of the sensor goes to ground (not sure if that's 1 or 3). Correct?
Mike |
|
|
|
|
|
|
(permalink) |
|
Yes that is correct. I have a 1k resistor connected to 5v running to pin 2 on the sensorHowever, I have looked at the data sheet for lm355 and for some wierd reason, when I connect either pin 1 or pin 3 to ground and leave the other one not connected to anything, the readout on my lcd does indeed increase or decrease depending on temperature. The only difference I have noticed is that I have to adjust my pot which is connected to pin 2 on the sensor to get the voltage to where I want it. Is this ok? It almost has me thinking they messed up the pinout on the datasheet, but I would have to say I am probably ncorrect. Once again thanks for the help.
Jonathan |
|
|
|
|
|
|
(permalink) |
|
Here is how I have mine connected: http://web.mit.edu/rec/www/workshop/lm335.html
I'd suggest getting this basic hookup working first, before you try calibration. I left calibration out of mine, because it's just a constant offset and the sensors I used were already within a degree of my digital sensor (DS18S20). Mike |
|
|
|
|