Shop OBEX P1 Docs P2 Docs Learn Events
TMP04 temperature sensor ? ? — Parallax Forums

TMP04 temperature sensor ? ?

Don PomplunDon Pomplun Posts: 116
edited 2006-03-21 06:46 in Learn with BlocklyProp
SEARCHing finds no hits for the AD TMP04 temperature sensor, so I'll ask if anyone has tried these with the Stamp. It outputs a TTL level pulse train at about 30 Hz whose duty cycle is linearly proportional to temperature. I'd like to incorporate temperature control as a lab project in my upcoming course, and this seems like a good candidate without having to add analog input circuitry.
TIA
Don

Comments

  • Tracy AllenTracy Allen Posts: 6,664
    edited 2006-03-17 02:16
    Try this from one of Prof. Peter Anderson's highly motivated students:
    www.phanderson.com/stamp/tmp04.html

    That shows how easy it is to read the high and low times with PULSIN. The math there can be greatly simplified.
    The calculation of temperature uses this formula:
    TC_CALC: ' 2350 - (4000 * T1)/T2
    and in the program the multiplication is done in a highly instructive manner as 4000 iterated additions and the diviision also as iterated subtractions.

    If you want to pursue it, I could hobble together something faster and shorter using the Stamp's ** operator and a quick binary long division.. Also instructive, in its own way.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Paul Sr.Paul Sr. Posts: 435
    edited 2006-03-17 13:28
    Tracy Allen said...
    Try this from one of Prof. Peter Anderson's highly motivated students:
    www.phanderson.com/stamp/tmp04.html

    That shows how easy it is to read the high and low times with PULSIN. The math there can be greatly simplified.
    The calculation of temperature uses this formula:
    TC_CALC: ' 2350 - (4000 * T1)/T2
    and in the program the multiplication is done in a highly instructive manner as 4000 iterated additions and the diviision also as iterated subtractions.

    If you want to pursue it, I could hobble together something faster and shorter using the Stamp's ** operator and a quick binary long division.. Also instructive, in its own way.

    Dr. Allen,

    I would definately like to see that if you have the time to put it together. I do have the components from Prof. Anderson..

    TIA,
    Paul
  • Don PomplunDon Pomplun Posts: 116
    edited 2006-03-17 16:32
    Good news -- looks like the TMP04 is a winner. There a few gotchas associated with the integer arithmetic & 16 bit word limitations. The device defines Farenheit temperature as 455 - (720 * Ton / Toff) . Numbers returned by pulsin are in the thousands, so you have to be careful of overflows. The code below outputs the temperature in 1/100ths of degrees, though no claim is made for that accuracy. Blasting the TO-92 device with a can of Freeze, or a hair dryer works well from temperatures down in the 20's, and up over 130. Still have to watch for numbers out of the 16 bit range, but it should work well for a temperature control lab. Somewhere on the Parallax site I saw an excellent writeup on PID, which I'll probably steal ;=)
    These devices are over $5 at DigiKey, even though the Analog Devices site shows them as half that in quantity. This might be a candidate for the Parallax store, eh?
    -- Don

    Ton VAR Word
    Toff VAR Word
    Temp VAR Word

    start: PULSIN 0,1,Ton
    PULSIN 0,0,Toff
    Toff=Toff/100
    Temp = 10*Ton/Toff*72
    Temp=45500-Temp
    DEBUG "Ton=", DEC Ton," Toff/100=",DEC Toff, " 100xTemp=", DEC Temp, CR
    PAUSE 1000
    GOTO start
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2006-03-17 19:11
    Hi Don,

    That calculation makes it easy! I looks to me like that would give a real resolution of about 1/3 degree Fahrenheit.

    The data sheet informs us that T1 (Ton) will be around 10 milliseconds, 12 maximum, and does not depend much on temperature, while T2 (Toff) will range from around 44 milliseconds at 125 degC down to around 15 milliseconds at -25 degC. On the Stamp, that is PULSIN values in the range of Toff = 7500 to 22000 (at 2 microseconds per count), and around 5000 for Ton.

    Don, are those values typical of what you are seeing?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Don PomplunDon Pomplun Posts: 116
    edited 2006-03-17 19:50
    Hi Tracy,
    I guess I hadn't read the (16 page) data sheet very carefully, especially the part about Ton being pretty constant. But, yes it does seem to follow that. It's a little tough to follow my quick temp changes, but Ton seems to hover around 8.3 - 8.4 ms over a temp range of 4 - 117 F (pulsin values of 4144 - 4226). Over that same range, the Toff variation is 13.4 - 18.1 ms (pulsin 6694 - 9055).
    What I tried may not be the optoimal approach for getting the best temperature reading. It was just the first thing that worked! It will start out being a good class discussion on the topic of overflow, and overcoming the limitations of integer arithmetic.
    It looks like it should give enough resolution to do a temperature control lab.
    -- Don
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2006-03-17 23:18
    Here is another version that gives better precision:

    ' {$STAMP BS2pe}
    ' {$PBASIC 2.5}
    ' TMP04.bse
    ' version author: Tracy Allen, EME Systems
    ' Code for TMP04 temperature sensor (Analog Devices)
    '  Temperature proportional to duty cycle
    ' degrees Fahrenheit = 455 - (720 *T1/T2)
    ' where T1 and T2 are as in data sheet, time high and time low
    ' program displays various intermediate values, including:
    ' --- compute1: easy calculation
    ' --- compute2: using binary long division to comute T1/T2
    
    t1 VAR Word
    t2 VAR Word
    degf VAR Word
    idx VAR Nib
    
    DO
      PULSIN 15,1,t1
      PULSIN 15,  0,t2
      DEBUG DEC t1,TAB,DEC t2,TAB
      GOSUB compute1
      DEBUG DEC degf/100,".",DEC2 degf,TAB
      GOSUB compute2
      DEBUG DEC degf/100,".",DEC2 degf,CR
      PAUSE 100
    LOOP
    
    compute1:
      degf = t2/100
      degf = 10*t1/degf*72
      degf  = 45500 - degf
      RETURN
    
    compute2:
      FOR idx=15 TO 0   ' binary long division
        t1 = t1 // t2 << 1
        degf.BIT0(idx) = t1/t2
      NEXT
      DEBUG TAB,DEC degf,TAB   ' intermediate result T1/T2 = degF/65536
      degf = 22750 - (36000**degf) * 2   ' values * 5 * 2
      RETURN
    



    I did have a TMP04 (out of a box of samples!) At room temperature, I was getting T1 = 4346 and T2 = 8177 from a BS2pe. Using the data sheet formula, the temperature is 455 - (720 * 4346/8177) = 72.326 degrees Fahrenheit.

    The easy compute1 formula loses precision in the T2/100 step, which is necessary to keep the computation in bounds, and it comes up with a reading of 69.08 degrees Fahrenheit, with a resolution of about 4.3 degrees. Did I get your formula right Don? As the sensor warmed up, the reading went from 69.08 to 73.4

    The compute2 routine first uses binary long division to compute T1/T2 , approximated by the fraction degf/65536, and it finishes the computation to give a reading of 72.34 degrees Fahrenheit, with a resolution of 0.02 degrees.

    I don't know if your students would be scared by the binary long division, but they shouldn't be. It is one of the most useful routines for math work/sensor signal processing on the Stamp.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2006-03-18 18:30
    Hi Don,

    I was taken aback by the course 4.3 degree resolution in the compute1 algorithm, but it makes sense when you follow it through. Here is an improvement, not quite as good as compute3, but about 0.3 degree Fahrenheit. (The accuracy Analog Devices claims for the sensor is 1.5 degrees Celsius typical, 4 degrees worst case).

    compute3:  ' uses remainder from division to get an extra significant figure
      degf = t2 / 10
      degf = 10 * t1 / degf * 10 + (10 * t1 // degf * 10 / degf) 
      degf = 45500 - (degf * 72)
      RETURN
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Don PomplunDon Pomplun Posts: 116
    edited 2006-03-21 06:46
    Tracy,
    Thanks for the effort on your temperature Compute algorithms. It took some "soul searching" to the early days of long division to sort out. I like the 3rd one even better ;=)
    Regards,
    Don
Sign In or Register to comment.