PDA

View Full Version : LM35DZ and 18F2455 settings



Mark J
- 31st August 2007, 15:34
Hi everyone,

I have been scanning through this forum for quite a while and upto now never really needed to make my own post as after a good search, ive usually found what ive been after thanks to the helpfull posts on here etc.

Im currently making an Aquarium temperature sensor project that will use 2 sensors and also turn on and off multiple PC fans to help keep things cool etc.

I have seen a few posts regarding the temp sensors, but none with this sensor and also with the 18F2455 PIC.

Ive got the majority of the code working, and sort of got the temp sensor working right with the PIC chip BUT,
If I use the VDD as the AD reference, and use the DEC3 Temp to display it on the LCD it only shows it as for example '023' which is pretty much what I would expect due to the temp in the living room where I am etc.
The thing I cant suss with the VDD reference is to get it so I can get the fractional degrees as well.

If I use the Vref+ pin and set the input reference voltage to 2.5V I do get the fractional degrees but the temp just seems way off which leads me to think ive got something wrong in my ADCON settings etc.

The more I look at the ADCON0, ADCON1 & ADCON2 the more I get confused and just cant see the wood for the trees so to speak.

I have tried numerous settings but I just cant seem to understand the datasheet things for them as I am pretty much a newbie at all this stuff and only really played with a 12F675 (think its those ones off the top of my head)

I have stripped everything unrelated out of the code so all it does is display the temp from a single probe so that I can post it here and not have a huge amount of code of which 99% would be irrelevant etc.

I have seen the thread by Savnik and used the loops etc to get a means adjusted temp etc, but I am not getting the correct temps which leads me to believe it is to do with the ADCON's etc.
Savniks thread: http://www.picbasic.co.uk/forum/showthread.php?t=5826&highlight=LM35DZ

I have put my meter on the AD pin and ground and read the results and looking at the readout on there it shows what I believe are the correct temp values as when I blow on or hold the probe in my fingers, I can see the values going up and down etc.



@WDT_ON
@PWRT_ON
@MCLR_OFF
@BOD_ON
@LVP_OFF
@CPD_OFF
@PROTECT_OFF
@ errorlevel -202

'************************************************* ***************

' LCD should be connected as follows:
' LCD PIC
' DB4 PortB.0
' DB5 PortB.1
' DB6 PortB.2
' DB7 PortB.3
' RS PortB.4 (add 4.7K pullup resistor to 5 volts)
' E PortB.5
' RW Ground
' Vdd 5 volts
' Vss Ground
' Vo Ground

' Setup the ports for the LCD (as not set to the default layout as set in PBP)
DEFINE LCD_DREG PORTB
' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_DBIT 0
' Set LCD Register Select port
DEFINE LCD_RSREG PORTB
' Set LCD Register Select bit
DEFINE LCD_RSBIT 4
' Set LCD Enable port
DEFINE LCD_EREG PORTB
' Set LCD Enable bit
DEFINE LCD_EBIT 5
' Set LCD bus size (4 or 8 bits)
DEFINE LCD_BITS 4
' Set number of lines on LCD
DEFINE LCD_LINES 2
' Set command delay time in us
DEFINE LCD_COMMANDUS 2000
' Set data delay time in us
DEFINE LCD_DATAUS 50

'************************************************* ***************

DEFINE ADC_BITS 10 ' Set ADC to 10 bit

'************************************************* ***************

' 76543210
ADCON0 = %00000011 'Set ADCON0
ADCON1 = %00001101 'Set AN0 & AN1 for A/D input & Vref to VDD
ADCON2 = %10101111 'Set ADC results to right justified
TRISA = %00001011 'set PORTA as all output except RA0,RA1 &RA3
TRISB = %00000000 'set PORTB as all output
TRISC = %00000111 'set PORTC RC0,RC1 & RC2 as inputs for the buttons


'************************************************* ***************

Temp1_C VAR word 'Store for the Final Temperature in Celcius for probe 1

'************************************************* ***************

code_start:

pause 100
LCDOUT $FE,1

main:
lcdout $FE,$80,"Temp:",DEC3 Temp1_C
gosub Read_temp
goto main

Read_temp:
i var byte
samples var word
VAL var word

samples = 0
Temp1_C = 0
for i=1 to 10
ADCIN 0,VAL 'Read in sensor temp
pause 50
samples = samples + VAL
next i
Temp1_C = samples/10
Temp1_C=(Temp1_C*/500)>>2
return


I hope my codes not toooooo messy for anyone to understand lol! I just tend to block things in and then go in afterwards and smarten it up (I blame Malc-C lol!! as he has taught me the dark side!!!!! Just kidding m8y! hope to chat on Skype again soon!)

Many thanks in advance for any help in getting this working correctly.

mackrackit
- 31st August 2007, 15:53
I have not used the 18F2455 but when using a 18F4320 with a LM34 I have to put a 22uf cap from VREF to ground and a 22uf cap from the sensor output to ground to get things working accurately.

Mark J
- 31st August 2007, 15:59
I'll give that a try when I get a chance and see how it goes.
At the mo, the only reason I used this chip is because I have a couple laying around here, and also I thought that in the future when I see about adding other things to the project etc, I can then use the USB functionality of the PIc to communicate to the PC for things like datalogging etc (not for a long while yet though lol! as just getting my head around the ADCON settings is hurting my brain lol!)

Hopefully, if it is the ADCON's and we get it working right etc, at least this will hopefully help others out using these PIC's as there dosnt seem to be a lot of info from the searches ive done so far as with the 18's been a strange beast compared to the normal PIC's people use etc, so I presume it kind of keeps peopple away from trying to use them more etc.

mackrackit
- 31st August 2007, 16:41
I am starting to like the 18F4320, made my head hurt at first.
I am working on a pyrometer with a K type thermo-couple and a LM34 for a reference. Have not looked at the data sheet for the PIC you are using, may or may not be close to the 4320. If it is, here is the pyrometer code so far, might give you some ideas.


DEFINE OSC 8
OSCCON=%01111000

DEFINE LCD_DREG PORTC
define LCD_DBIT 0
DEFINE LCD_RSREG PORTD
DEFINE LCD_RSBIT 1
DEFINE LCD_EREG PORTD
DEFINE LCD_EBIT 0
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50

ADCON1= %00011011

temp1 var byte
temp2 var byte
T1 var byte
T2 var byte

TC VAR WORD
TCT VAR WORD
TX VAR WORD
TXI VAR WORD
TXIU VAR WORD

RUN:
HIGH PORTD.2
PAUSE 100
LOW PORTD.2
PAUSE 100
gosub getT1
GOSUB getT2
T1 = temp1
LCDOUT $FE,1,"LM34 ",#T1
lcdout $FE,$C0,"TC ",#TXIU
GOSUB D_INDEX
GOTO RUN

getT1:
ADCON0= %00000001
ADCON2.7 = 0
gosub getAD
temp1 = ADRESH
return

getT2:
ADCON0= %00000101
ADCON2.7 = 1
gosub getAD
TC.highbyte = ADRESH
TC.lowbyte = ADRESL
return

getAD:
pause 50
ADCON0.1 = 1
pause 50
return

END

D_INDEX:
TX = TC
'STEPS
LOOKDOWN2 TX,>[898,895,892,889,886,883,879,876,873,870,867,863,86 0,857,854,850,_
847,844,841,838,835,832,828,825,822,819,815,812,80 9,805,802,799,795,792,788,785,_
782,778,775,772,769,765,762,758,755,751,748,744,74 1,737,734,731,727,724,721,717,_
713,710,706,703,699,696,692,689,685,682,678,675,67 1,667,664,660,656,653,650,646,_
643,639,635,632,628,624,620,617,614,610,606,603,59 9,595,592,588,584,580,577,573,_
570,566,562,558,555,551,547,544,540,536,532,529,52 5,521,517,513,509,506,502,498,_
495,491,487,483,479,475,471,468,464,460,456,453,44 9,445,441,437,433,429,426,422,_
418,414,410,406,402,398,395,391,387,383,379,375,37 1,367,363,360,356,352,348,344,_
340,336,332,328,324,321,317,313,309,305,301,297,29 3,290,286,282,278,274,270,267,_
263,259,255,252,248,244,240,236,232,229,224,221,21 7,213,209,206,202,198,194,190,_
187,183,180,176,173],TXI

'TEMPERATURES
LOOKUP2 TXI,[2500,2490,2480,2470,2460,2450,2440,2430,2420,2410, 2400,2390,2380,2370,_
2360,2350,2340,2330,2320,2310,2300,2290,2280,2270, 2260,2250,2240,2230,2220,2210,2200,_
2190,2180,2170,2160,2150,2140,2130,2120,2110,2100, 2090,2080,2070,2060,2050,2040,2030,_
2020,2010,2000,1990,1980,1970,1960,1950,1940,1930, 1920,1910,1900,1890,1880,1870,1860,_
1850,1840,1830,1820,1810,1800,1790,1780,1770,1760, 1750,1740,1730,1720,1710,1700,1690,_
1680,1670,1660,1650,1640,1630,1620,1610,1600,1590, 1580,1570,1560,1550,1540,1530,1520,_
1510,1500,1490,1480,1470,1460,1450,1440,1430,1420, 1410,1400,1390,1380,1370,1360,1350,_
1340,1330,1320,1310,1300,1290,1280,1270,1260,1250, 1240,1230,1220,1210,1200,1190,1180,_
1170,1160,1150,1140,1130,1120,1110,1100,1090,1080, 1070,1060,1050,1040,1030,1020,1010,_
1000,990,980,970,960,950,940,930,920,910,900,890,8 80,870,860,850,840,830,820,810,800,_
790,780,770,760,750,740,730,720,710,700,690,680,67 0,660,650,640,630,620,610,600,590,_
580,570,560,550,540,530,520,510,_
500],TXIU

RETURN

As you can see I do not use ADCIN. I am a control freak:)

Mark J
- 31st August 2007, 17:29
No luck,
Tried adding the caps and no difference, and also tried your way of getting the AD values but still off, so i'm at a loss at the mo!

will try another sensor JUST on the offchance that its that thats gone awry etc.

mackrackit
- 31st August 2007, 18:13
Maybe Darrel or Bruce will come to the rescue.

Mark J
- 31st August 2007, 18:32
Hope so, as im quite attached to my hair lol!!
Runs fine when using the VDD as the Vref source, just cant seem to get the fractional digit at all.
Been running it using the VDD for about 5 mins, and it sits at around 22C which at a guess is our room temp lol, and when I hold the sensor inbetween my fingers the temp rises nicely, and when put down again it drops and settles again at 22.

It is just when I try to use the Vref+ (AN3) it goes toes up (not the usual saying, but kiddy friendly)

For a generic temp reader, it would be fine as it is, but for a marine aquarium, I want to be able to get finer readings so that I can use the fans to reduce the temp if the water level rises say 0.5C as stability is the key in marine keeping etc.

mackrackit
- 31st August 2007, 19:01
Not sure if you can get the 0.5 with this. If you do you will have to do a floating point routine or some other trick. +- 1 degree for these LM34s is the rating(I think).

Have you seen this?
http://www.rentron.com/PicBasic/LM34.htm

Mark J
- 31st August 2007, 19:06
yeah I have read through that a few times, and it helped me in the first place with the sensor etc, also read the link above in my first post as Savnik was trying to do the same thing but with a 16F chip and a couple of guys helped him to sort it out, which is the base for mine, but obviously on that thread, they dont say how they are using it (ie VDD or a Vref pin)

ronjodu
- 31st August 2007, 19:52
I use two LM34s with a 16f876 and it's been rock solid with the following snippetts....


DEFINE LOADER_USED 1
DEFINE OSC 4
DEFINE ADC_BITS 8 ' Set A/D for 8-bit operation
DEFINE ADC_CLOCK 3 ' Set A/D clock
DEFINE ADC_SAMPLEUS 50 ' Set A/D sampling time @ 50 uS

'((((((((((((((((((((((((((((((((((((((()))))))))) )))))))))))))))))))))))))))))
'Subroutine to get room temperature
'((((((((((((((((((((((((((((((((((((((()))))))))) )))))))))))))))))))))))))))))


roomdegrees:

FOR sample = 1 TO 20 ' Take 20 samples
ADCIN 2, roomtemp ' Read channel 2 into temp variable
samples = samples + roomtemp ' Accumulate 20 samples
NEXT sample
roomtemp = samples/20
samples = 0 ' Clear old sample accumulator
SerOut2 PORTC.6,SPbaud, [$00, $80, $29,roomtemp] 'update siteplayer with room temperature
if r7sched = 0 then return 'manual control = 0, auto control = 1
if roomtemp >= roomspt + 1 then
SerOut2 PORTC.6,SPbaud, [$00, $80, $3C,1] 'update siteplayer with exhaust status on
checkrelays = 1
endif
if roomtemp <= roomspt - 1 then
SerOut2 PORTC.6,SPbaud, [$00, $80, $3C,0] 'update siteplayer with exhaust status off
checkrelays = 1
endif
return
'((((((((((((((((((((((((((((((((((((((()))))))))) )))))))))))))))))))))))))))))
'Subroutine to get tank temperature
'((((((((((((((((((((((((((((((((((((((()))))))))) )))))))))))))))))))))))))))))

tankdegrees:

FOR sample = 1 TO 20 ' Take 5 samples
ADCIN 4, watertemp ' Read channel 4 into watertemp variable
samples = samples + watertemp ' add 5 samples
NEXT sample
watertemp = samples/20
samples = 0 ' Clear old samples
SerOut2 PORTC.6,SPbaud, [$00, $80, $28,watertemp] 'update siteplayer with water temperature
if r8sched = 0 then return 'manual control = 0, auto control = 1
if watertemp > waterspt then
serOut2 PORTC.6,SPbaud, [$00, $80, $3D,0] 'update siteplayer with heater status off
checkrelays = 1
endif
if watertemp <= waterspt - 1 then
SerOut2 PORTC.6,SPbaud, [$00, $80, $3D,1] 'update siteplayer with heater status on
checkrelays = 1
endif
return

I know you're using a different micro but maybe this will help some.

Mark J
- 31st August 2007, 20:03
Thanks ronjodu,
Do you have it display with fractional degrees c? ie 25.4C or just straight values ie 25.
Also noticed that the pic chip your using and also the one on the link I have in my post both use 8 bit ADC_BITS, wheras the 18F's use 10, so not sure if that might have something to do with it also as it could be because im basing the reading/loop/convertion on an 8 bit value.

Mark J
- 31st August 2007, 20:49
Just done a couple of tests.

Set the Vref back to AN3 and just simply did ADCIN 0,VAL and then print it out to the LCD with a DEC5 just to see if I was missing anything without any fiddling with the ADCIN result.

the reading is just constantly at 00255 (once in a blue moon it shows 511 then goes back to 255) on the LCD whether I hold the sensor or not, so somehing is seriously wrong with the way im setting it up, but I just cant for the life of me see what it is!

Same test with VDD as ref, I get values around 00046 - 00050 and when I hold the sensor, it rises.

Hope that may give some ideas as im all out now and I dont think tears and electronics mix :( lol!

Mark J
- 2nd September 2007, 11:33
Boy do I feel a bit dumb lol!

Decided to have one more go this morning with the sensor and just displaying the result of all the samples added together with NO adjustments etc.

on the LCD it was showing something like 462 and putting the meter on the Gnd pin and the Vout pin on the sensor the meter was reading 231, so it then dawned on me that I dont need to do all the division stuff by the number of samples etc just simply divide by 2 to get the result!!!!!! DOH! and that way, I get the reading with the fractional degree's also!

Oh, and just using the VDD as the voltage reference.

One happy bunny!

Mark J
- 6th October 2007, 13:47
Well, I thought I had it working, but ive just had chance to work on the project again and found that when submersed in the aquarium (waterproofed obviously!) the thermomitor temp (tube type) shows a steady 25 C, yet the sensor on my project is all over the place (even have it in a bag in case of water seepage etc) it varies from between 22 to 28 degrees! and in a large body of water, there is no way the water temp can fluctuate so wildly (roughly 150 UK gallons)

So I am at a loss, as the code is based on other peoples posts and I thought it was working fine, but testing both sensors I still get the swings (Arghh!)

I am seriously getting annoyed with these sensors but with both swinging so much, it leads me to think its something in either my code, or getting some errenous readins through the breadboard etc.

Does anyone know if breadboards will cause these variations? as I have read somewhere that breadboards arnt exactly the greatest things etc.

Any ideas would greatly be appreciated!

mackrackit
- 6th October 2007, 17:44
Where the leads from the probe connect to the bread board. Place a 22uf , or something close, capacitor between the signal return and ground. This should settle things down. Long wires going to the probe will cause fluctuations like this.

Mark J
- 7th October 2007, 09:41
Cool thanks Dave,
It seems to have sorted the problem out.
As you can probably tell, the electronic side of things isnt my strong point lol! so many little things to think of etc.

mackrackit
- 7th October 2007, 14:26
Cool thanks Dave,
It seems to have sorted the problem out.
As you can probably tell, the electronic side of things isnt my strong point lol! so many little things to think of etc.
To be honest, I had the same problem the first time I used a LM34 at a distance. Learn as you go:)
The problem is not the bread board, it is long wires.

slimpeng
- 25th January 2008, 08:10
hello....if i using the 16f877A pic so the program need to change or not and the math is only use in LM35DZ or other elese...can use it.....!!

mister_e
- 25th January 2008, 08:31
The program could need some minor modifications for hardware (LCD, USART, ADCs, Comparators etc etc), but the maths have to be the same.