Welcome to the Parallax Discussion Forums, sign-up to participate.

# MLX90614 Thermometer Program

Posts: 5,770
edited 2009-06-15 - 16:00:18
The MLX90614 demo (attached) is also found here at the Parallax product page:
www.parallax.com/Store/Sensors/TemperatureHumidity/tabid/174/CategoryID/49/List/0/Level/a/ProductID/520/Default.aspx?SortField=ProductName%2cProductName

You will see this section in the demo:
```overWordsize:
Celsius = ((27315-(temperature*2))/100)
CelsiusDec = (27315-(temperature*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
```

I want to display the left digit and then the right digit of the "decimal only."
But it seems the decimal portion does not act as a normal number.

For example, if the original number is 91.42, then
f2 is 42
f2a is 92
f2b is 2

(f2a should be 4)

I tried this but it is only working on the second decimal digit and not the first.
Unfortunately there are no comments in the author's demo program.
```f2 = CelsiusDec
DEBUG " f2 is ", DEC2 f2, CR

f2a = f2/10
DEBUG " f2a is ",DEC  f2a,CR

f2b = f2//10
DEBUG " f2b is ",DEC  f2b,CR
```

Any ideas how to make it work correctly?
Thanks sincerely.
humanoido

Post Edited (humanoido) : 4/8/2009 7:05:32 AM GMT

• Posts: 5,770
edited 2009-04-08 - 07:01:34
Maybe I can make this more simple. From the given demo,
how to convert the number to the right of the decimal point into a whole number?
An example, convert 22.57 to 57.
• Posts: 374
edited 2009-04-08 - 16:22:31
Take this simpler portion of the demo code:
```  Celsius = (temperature/100*2)-273
CelsiusDec = (temperature*2)-27315

DEBUG CRSRXY, 23, 4,DEC Celsius,".",DEC2 CelsiusDec, CLREOL
```

It looks like you're working from the belief that CelsiusDec containes a floating point number like 91.42, when it actually contains, in your example, the number ~9142. The only reason that the debug statement displays .42 is that the decimal point is hard coded and the "Formatter" DEC2 is used to display the·last two·digits of the variable.

If you work through the code with a raw "temperature" value of say, 18,000, you'll see what's going on.

Relying on the formula: C = K -273.15

Celsius = (18000/100*2)-273·········or 87
CelsiusDec = (18000*2)-27315······ or 8685

The DEBUG line above would display 87.85

Since the thermometer is returning values in .02 degrees Kelvin, the programmer could just have easily·done the calculation as you tried to do:

```Celsius = ((temperature*2)-27315)/100
CelsiusDec = ((temperature*2)-27315)//100

DEBUG CRSRXY, 23, 4,DEC Celsius,".",DEC CelsiusDec, CLREOL
```

Which would display 86.85
I'm not sure which approach would be more accurate without some testing.

Duffer
• Posts: 5,770
edited 2009-04-09 - 14:45:07
Duffer, thank you for your excellent explanation and code snippet examples. This is very clear. Are you a teacher at the university? I can finally understand the principles involved, and it makes very good sense. The coding is efficient and I will be able to complete the program I'm working on. Thanks again!

humanoido
• Posts: 2
edited 2009-04-14 - 14:36:56
did u get the example code from parallax website to work???when ever i run the program the output i get is always fail..i think there is some problem in receiving the serial data...how to go about this..any ideas....
• Posts: 5,770
edited 2009-04-16 - 17:47:59
Yes, the sample code works but it's somewhat overcomplicated.
If your code constantly produces a pec fail, it's likely
the EEPROM value is not properly initialized. Review the
storage parameters and you'll see what's happening.
You'll need to rewrite the code anyway. Once its
properly initialized, it works every time.

humanoido
• Posts: 17
edited 2009-05-26 - 07:19:00
Humanoido, Blue-Knight:

I never got the MLX90614 to work. Can you maybe email me/post any simple code that works? I would be forever grateful. I have wasted over 40 hours to no avail whatsoever. Humanoido: you are the only one on the discussion forum who has made ths work at all. Can you help, please?

Rupert
• Posts: 5,770
edited 2009-05-27 - 13:05:19
ruperthart,

The demo code works. Exactly what is it doing or not doing? Which stamp, stamp board, and which demo program are you running? Is your battery new? (first priority check with a meter and the battery under load) Did you initialize the EEPROM? What is your wiring? You should post the code, and a photo of your setup and show which wiring schematic you followed. My code is for the Penguin robot, has some additional changes and features, and is currently on hold until it appears in the Penguin Book. The main issue is all about initializing it, otherwise it gives the PEC fail. After figuring that out, the temperature sensor works excellent! There is another post that spells out how to do that, and gives snippets of code, found here in the Sensor Forum.

humanoido

Post Edited (humanoido) : 5/27/2009 1:25:03 PM GMT
• Posts: 17
edited 2009-05-28 - 06:05:36
Thanks, am working on it. Initalizing the EEprom seems to be the key. Not working yet. Will get back to you folks. Thanks for your response! r
• Posts: 141
edited 2009-05-29 - 01:22:12
I'm getting a "Fail" every time I run the demo code. . .this is the first time I've ever bought a sensor, downloaded sample code and had it "Fail". . .I'm sure I have it wired up properly. What ever could be wrong??
• Posts: 17
edited 2009-05-29 - 23:57:00
I haven't got any of my two units to work yet. Very frustrating. I am on version 45 of the software and it has sucked up several tens of hours of my time. I think the demo program is too complicated. The Melexis datasheet indicates that the default settings should be enough to work ot of the box. Let me know if you solve this.
• Posts: 5,770
edited 2009-06-01 - 19:41:51
ruperthart, in the other post thread, you mention another microprocessor that is not from Parallax and it is not a Basic Stamp. Are you using that instead of the Basic Stamp? This could be related to the source of unending perplexities. I suggest you try the circuit and code with the recommended Basic Stamp.

humanoido

Post Edited (humanoido) : 6/1/2009 7:46:52 PM GMT
• Posts: 17
edited 2009-06-02 - 07:11:41
You are very wise. I have been coming to the same conclusion. I am trying to get my stamp working. Best, and thanks. R
• Posts: 17
edited 2009-06-11 - 23:12:49
OK, got my three units to work using the basic stamp using the demo programs. Now to figure out how to get them to work with the other processor. Thanks to all for your help.

As commented elsewhere, to get the device to work using the Parallax programs as supplied, you MUST use the "write eeprom" program first if the "demo simple" program is to work. This is because the "demo simple" program reads address \$35, which has to be set up using the "write EEPROM" program first.

I suggest Parallax put up a simpler program to make the sensor work out of the box: "demo SUPER-simple": The Melexis device actually defaults to \$5A as the address (see Melexis data sheets). I suggest you skip the Parallax-supplied "write EEPROM" program which sets up \$35 instead and·just use the "demo simple" program, but this time addressing \$5A instead of \$35. See Humanaido's comments about unnecessary overcomplication.

Best, Rupert
• Posts: 17
edited 2009-06-12 - 23:39:30
Attached is the "demo-SUPER-simple" for BS2for those who can't get the·device to work out of the box or want a simpler solution to just get results. Works fine for me. Best regards, Rupert
• Posts: 17
edited 2009-06-12 - 23:44:19
Oh, and I never got the devices to work with my Basic Atom from Basic Micro, even though they work fine with the Stamp 2. if we could get code that works, it would massively increase the size of the Parallax Melexis temp sensors market. I expect it is something like timing, pullup resistors or syntax. Attached is my code, directly comparable to the "Demo-Super-Simple" code posted above. Any ideas?

Rupert

PS: neo=non-inverted, even parity, open drain.

'Basic Micro Atom 28

' =========================================================================

'
[noparse][[/noparse] I/O Definitions ]

Reset······· CON···· 1
Alr········· CON···· 2
Sensor······ CON···· 3

'
[noparse][[/noparse] Variables ]

temperature· VAR··· Word
tempL······· VAR··· temperature.LOWBYTE
tempH······· VAR··· temperature.HIGHBYTE
pec········· VAR··· Byte
slave·· VAR Byte

Main:
·gosub Init
·gosub gettemperature
end

Init:
·LOW Reset
·pause 1000
·INPUT Reset
·pause 50
·return

getTemperature:
· SEROUT Sensor,neo2400,[noparse][[/noparse]0,"!TEMR",\$5A,\$07]
· pause 50
·
· again:
· ·SERIN Sensor,neo2400,1000,PECfail,[noparse][[/noparse]tempL,tempH,pec]
· ·pause 40
·
·displayTemperature:
··serout s_out,i9600,[noparse][[/noparse]DEC temperature, " , ", dec pec, 10,13]

·goto again
return
·
pecfail:
serout s_out,i9600,[noparse][[/noparse]"pecfail", 10,13]
return
• Posts: 5,770
edited 2009-06-15 - 12:52:29
I'm sure we are not saavy with the other brand processor.
I suggest you post your code and question in THEIR forum
and someone there will quickly provide the answer.

humanoido
• Posts: 1
edited 2009-06-15 - 13:50:11
I want to know: Could I programming this MLX90614 with C or is the command list in datasheet only for Basic Stamp. My second question is how can I get the sensor temperature, I saw only the object temperature command !TEMPR

greetz ting
• Posts: 17
edited 2009-06-15 - 16:00:18
Humanaido: yes, tried posting there; no joy yet. Good idea.

Ting: instead of checking address 07 try 03. The Melexis datsheet states this is the sensor temperature. Haven't tried myself. Let us know if that works. Have you got the sensor to work with your processor?
• Posts: 2,194
Is it possible for the temperature readout to be in Fahrenheit this would be easier for me
• Posts: 6,514
edited 2020-05-11 - 22:51:54
Sure. The raw sensor data "temperature" is in units of 0.02 Kelvin.
```  Kelvin = temperature * 2   ' this is xxx.xx Kelvin resolved to 0.02 Kelvin
Celsius = Kelvin - 27315.         ' zero °Celsius is 273.15 Kelvin
```
The Fahrenheit equivalent of Kelvin is Rankine, and the scale factor is 9/5 to convert from Kelvin to Rankine.
```  Rankine = temperature * 2 / 5 * 9    ' /5 before *9 in order to avoid overflowing 16 bits.
Fahrenheit = Rankine - 45967.        '  0°F = 459.67 Rankine
Debug Dec Fahrenheit/100, ".", Dec2 Fahrenheit      ' works so long as °F is positive.
```