Shop OBEX P1 Docs P2 Docs Learn Events
DS18S20 Temp Display — Parallax Forums

DS18S20 Temp Display

SG09SG09 Posts: 11
edited 2009-04-27 15:12 in BASIC Stamp
I am a complete beginner in both electronics and programming (please bear with me).· I am trying to log temperatures from several DS18S20s throughout a solar thermal heating system I am building.· To get my feet wet, I first went the route of building a simple serial COM port type device that I could just plug in to the back of a computer.·After I could not get it to work, I learned that the notebook I have does not put out enough voltage...· I then stumbled upon the Basic Stamp (BS2 Homework Board). What a great way to get started in this hobby!· I have had alot of fun assembling the simple circuits with my 12 year old son and we are learning a little more each time we sit down with it.· I also learned that you can't just plug in a sensor and go!

I then found Peter Anderson's parts kit (about $8) to enable the BS2 to talk with several DS18S20s (via a PIC 16F88) and have just finished it.· I took the example code from his site and ran it and it actually works, but it is not showing an accurate reading.· I have searched the archives but had no luck in figuring out what is wrong; it responds to changes in temperature, but it is way off.· My digital thermometer in my kitchen reads 23.3 C and the output from the BS2·is showing·2.87 ( I am assuming C).· If I put my finger on it, it will climb to around 3, or so.

Can anyone point me in the right direction?

Thanks!

Bill

Comments

  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-04-21 15:43
    In cases like this, it can help if you provide links, or attach code that you are using.

    Be sure to distinguish the DS18S20 from the DS18B20. They have quite different scale factors.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • SG09SG09 Posts: 11
    edited 2009-04-21 15:54
    Thanks Tracy,

    Here is the code I am using.· I thought I was using DS18S20s, as that is what I ordered; what I received was a bag of (10) sensors marked "DS1820/DS18S20".· On the particular chip I am using it says "DS1820".


    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ·················· BaudMode· CON· 84········· '9600 True
    ··· X VAR Word
    ··· Y VAR Word
    ··· SignBit VAR Byte
    ··· Whole VAR Byte
    ··· Fract VAR Byte
    ··· DIR7 = 0· ' serial input
    ··· DIR8 = 1· ' serial output
    ··· OUT8 = 0· ' be sure SerOut pin is stable at zero
    ··· PAUSE 1000
    · DO
    AGN:
    ··· SEROUT 8, BaudMode, 10, [noparse][[/noparse]"P0", "W0cc", "S044"]· 'perform temp meas
    ·········································· ' note strong pullup
    ··· PAUSE 1100······· ' wait for conversion to complete
    ··· SEROUT 8, BaudMode, 10, [noparse][[/noparse]"P0", "W0cc", "W0be"]·· ' send temperature data
    ··· SEROUT 8, BaudMode, 10, [noparse][[/noparse]"R0"]·· ' fetch data
    ··· SERIN 7, Baudmode, 1500, TimeOut, [noparse][[/noparse]DEC X.LOWBYTE]
    ··· PAUSE 100
    ··· SEROUT 8, BaudMode, 10, [noparse][[/noparse]"R0"]·· ' fetch data
    ··· SERIN 7, Baudmode, 1500, TimeOut, [noparse][[/noparse]DEC X.HIGHBYTE]
    ··· PAUSE 100
    ··· ' now do the calculations
    ··· SignBit = X / 256 / 128
    ··· IF SignBit = 1 THEN ' its negative
    ······ X = X ^ $ffff + 1 ' take the two's comp
    ··· ENDIF
    ··· ' multiply by 6.25 - This is 100 * Tc
    ··· Y = X / 4·· ' 0.25
    ··· X = X * 6 + Y
    ··· Whole = X / 100
    ··· Fract = X // 100
    ··· IF SignBit = 1 THEN
    ······ DEBUG "-", DEC Whole, ".", DEC2 Fract, CR
    ··· ELSE
    ······ DEBUG DEC Whole, ".", DEC2 Fract, CR
    ··· ENDIF
    ··· PAUSE 3000 ' wait 3 secs
    · LOOP
    TimeOut:
    ··· DEBUG "Timeout Error", CR
    ··· GOTO AGN



    ·
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-04-21 17:45
    Insert a debug statement:
    ' now do the calculations
    DEBUG SDEC x,CR ' signed integer

    That will show what is there before the calculation.

    It looks to me that this is code for the DS18B20, which at 12 bit resolution is 0.0625 degree Celsius per bit. That is the multiply time 6.25 in the calculation. The DS1820 and DS18S20 have 9 bit resolution, 0.5 degree Celsius per bit. Here is a calculation done is a more streamlined way for resolution of 0.5 per bit:

    ' now do the calculations
       DEBUG SDEC x,cr   ' raw datum
       x = x * 5   ' multiplication works okay in twos complement
        DEBUG REP "-"\x.bit15, DEC ABS x/10, ".", DEC1 ABS x ,CR ' display +-xx.x
       PAUSE 3000
       LOOP
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • SG09SG09 Posts: 11
    edited 2009-04-21 18:30
    It works!!·

    Someday, I will figure out exactly how!!

    Just two more quick things:

    · -First, instead of just showing for example "23.5", it is showing "47" and then "23.5" on the line below with every output.

    · -How do I change the display to Fahrenheit?

    Many Thanks,

    Bill
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-04-21 19:03
    47 is the result of the "DEBUG SDEC x". You can delete or comment out that line to get rid of it. Note that 47/2 = 23.5, and also that 47*5=235. The Stamp works with integers, so we get the digits by multiplying by 5. Then break that down to display it as 23.5, as follows:
    REP "-"\x.bit15 ' if x is negative, the sign bit (bit15 will be negative), and the REP modifier will display the - sign
    DEC ABS x/10 ' displays all but the ones digit of the absolute value of x
    "." ' decimal point
    DEC1 ABS x ' displays the ones digit

    To Fahrenheit...

    degF = x + 178 * 9 / 5
    DEBUG REP "-"\degF.bit15, DEC ABS degF/10, ".", DEC1 ABS degF

    That formula will work down to -17.8 degrees Celsius (=zero degF). If you use the standard formula,
    degF = x * 9/5 + 320 ' convert tenths Celsius to tenths Fahrenheit
    that will work down to 0 degrees Celsius, 32 Fahrenheit, but will fail below that because /5 does not work correctly on negative numbers on the Stamp.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • SG09SG09 Posts: 11
    edited 2009-04-21 21:54
    Yes!! It works perfectly.·

    A small start, but now I can't wait to·get·some more sensors set up and maybe delve into controlling pumps when I get some more experience.

    Thanks,

    Bill
  • diy.jackdiy.jack Posts: 1
    edited 2009-04-27 07:31
    Tracy Allen is right .Be sure to distinguish the DS18S20 from the DS18B20 . You can check the datasheet from the url.
  • Radridz3Radridz3 Posts: 49
    edited 2009-04-27 15:12
    I recently did this same project for a temp sensor system to lcd in by boat dash. I could never figure out how to get it into Farienhiet Thanks for the thread helpt out alot...>>....Now if I could only figure out how to get the LCD 117 Serial Backpack commands to show my debug data on the lcd instead.... Any Help On this would be appreciated. OH and the demo code you listed above is for the 18B20 ...I did try to contact peter anderson for help with a couple questions but he just blew me off....I hope you and your son have great fun with the hobby and just remember 1 day he will be coding everything for you.!> He'll probably laugh at you when you tell him you guys used to program Pic's HAHAHA ...Like us when we laugh at a 333's.....
Sign In or Register to comment.