Shop OBEX P1 Docs P2 Docs Learn Events
MLX90614 Sensor Displaying in Fahrenheit — Parallax Forums

MLX90614 Sensor Displaying in Fahrenheit

jtm1202jtm1202 Posts: 2
edited 2011-05-30 20:56 in Accessories
No trouble communicating with the sensor and getting data in Kelvin and Celsius but I would like to display the temperature in Fahrenheit as well. I am close to the goal but there is something small I have overlooked and cannot put my finger on it. When I run the program I have modified I get a reading for Fahrenheit but it is not correct. Example: When celsius reads 20, fahrenheit reads 37.

What else do I need to modify?

Find file attached

Thanks!

Comments

  • ElectricAyeElectricAye Posts: 4,561
    edited 2011-05-27 09:52
    I'm not very familiar with the BS2, but it jumped out at me that 20 C is very close to 68 F. And 68-37= 31, which is very close to 32. Any chance you're subtracting a 32 somewhere in your conversion code where you shouldn't be?
  • jtm1202jtm1202 Posts: 2
    edited 2011-05-27 10:00
    Nice catch! I checked on that and I'm not subtracting 32 anywhere, just adding. Here is what i have

    Init:
    LOW Reset
    INPUT Reset
    DEBUG CLS, ' setup report screen
    "MLX90614 Infra Red Thermometer", CR,
    "=============================", CR,CR,
    "Kelvin......... ", CR,
    "Celsius........ ", CR,
    "Fahrenheit..... ", CR,
    "PEC test....... ", CR

    '
    [ Program Code ]

    getTemperature:
    SEROUT Sensor,baud,[0,"!TEMR",xslave,$07]
    SERIN Sensor,baud,1000,PECfail,[tempL,tempH,pec]

    checkPEC:
    z=0
    y = xslave<<1+0
    GOSUB calculateCRC
    y = $07
    GOSUB calculateCRC
    y = xslave<<1+1
    GOSUB calculateCRC
    y = tempL
    GOSUB calculateCRC
    y = tempH
    GOSUB calculateCRC
    IF z<>pec THEN PECfail
    GOTO convertTemperatures

    calculateCRC:
    w=z^y
    READ w,z
    RETURN

    convertTemperatures:
    Kelvin = (temperature/100)*2
    KelvinDec = temperature*2

    IF (temperature < 13650) THEN overWordsize

    Celsius = (temperature/50)-273
    CelsiusDec = (temperature//50*2)

    Fahrenheit = ((((temperature/50)-273) * 18) + 32) / 10
    FahrenheitDec = (((temperature//50*2) * 18) + 32) / 10

    GOTO displayTemperatures



    overWordsize:

    Celsius = (273-(temperature/50) )
    CelsiusDec = (temperature//50*2)
    DEBUG CRSRXY, 23, 4,"-",DEC Celsius,".",DEC2 CelsiusDec, CLREOL

    displayTemperatures:
    DEBUG CRSRXY, 23, 3,DEC Kelvin,".",DEC1 KelvinDec,CLREOL
    DEBUG CRSRXY, 23, 4,DEC Celsius,".",DEC2 CelsiusDec, CLREOL
    DEBUG CRSRXY, 23, 5,DEC Fahrenheit,".",DEC3 FahrenheitDec, CLREOL
    DEBUG CRSRXY, 23, 6,"Pass",CLREOL
    PAUSE 1000
    GOTO getTemperature

    PECfail:
    DEBUG CRSRXY, 23, 3,CLREOL,CR
    DEBUG CRSRXY, 23, 4,CLREOL,CR
    DEBUG CRSRXY, 23, 5, CLREOL,CR
    DEBUG CRSRXY, 23, 6,"Fail",CLREOL,CR
    PAUSE 500
    GOTO getTemperature
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2011-05-30 20:56
    Your Fahrenheit formula needs to add 320, not just 32.
    [SIZE=1]Fahrenheit = ((((temperature/50)-273) * 18) + 32[/SIZE][SIZE=1][COLOR=red]0[/COLOR][/SIZE][SIZE=1]) / 10[/SIZE]
    
    I think the other formulas and debugs are a little off, too. For example, it appears that the raw temperature value is returned in units of 0.02 Kelvin. Here is how I would write the conversions and the debugs...
    [SIZE=1]Kelvin = temperature * 2   ' in units of 0.01 K, resolution 0.02 K
    DEBUG CR, "Kevin=", DEC Kelvin / 100, ".", DEC2 Kelvin
    
    degC = Kelvin - 27315   ' units of 0.01 degC, resolution 0.02, can be negative[/SIZE] [SIZE=1]
    DEBUG CR, "degC=", REP "-" \ degC.bit15, DEC ABS degC / 100, ".", DEC2 ABS degC
    
    degF = Kelvin * 9/5 - 45967   ' units of 0.01 degF, can be negative[/SIZE] [SIZE=1]
    DEBUG CR, "degF=", REP "-" \ degF.bit15, DEC ABS degF / 100, ".", DEC2 ABS degF[/SIZE]
    
    note that the REP operator is used to handle the negative sign if needed, depending on the sign bit, bit15 of the number.
Sign In or Register to comment.