Go Down

Topic: MAX31855 library (Read 30394 times) previous topic - next topic

robtillaart


Wrote a first version of a library for the MAX31855 thermocouple  board. Code can be found on Github
- https://github.com/RobTillaart/Arduino/tree/master/libraries/MAX31855 -

There are some differences with the Adafruit library, most important is that I split the reading and the fetching of the temperature. So to get a temperature one needs to do 2 calls

status = tc.read();  // workhorse
temp = tc.getTemperature(); // will return same value until a new read.

The reason for this split is 2 fold:
1) more explicit status information -> Adafruit returns NAN when there is a failure
2) The object now automatically is a buffer for the last value read.

As always remarks and comments are welcome.

demo sketch
Code: [Select]
//
//    FILE: max31855_demo1.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.01
// PURPOSE: thermocouple lib demo application
//    DATE: 2014-01-02
//     URL:
//
// Released to the public domain
//

#include "MAX31855.h"

const int doPin = 7;
const int csPin = 6;
const int clPin = 5;

MAX31855 tc(clPin, csPin, doPin);

void setup()
{
  Serial.begin(115200);
  Serial.print("Start max31855_demo: ");
  Serial.println(MAX31855_VERSION);
  Serial.println();

  tc.begin();
}

void loop()
{
  int status = tc.read();
  if (status != 0)
  {
    Serial.print("stat:\t\t");
    Serial.println(status);
  }

  float temp = tc.getTemperature();
  int m = temp*10 - 200;
  Serial.print(temp);
  Serial.print('\t');
  for (int i=0; i< m; i++) Serial.write(']');
  Serial.println();
}


TODO:
- refactor library
- check datasheet for details missed.

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart

updated to version 0.1.01,
major changes:
+ substantial improvement for speed - now ~540 uSec for a read() - was 68 mSec (first get it right, then get it fast)
   chip supports 5MHz clock so for the UNO no delay's are needed,
   but this might become an issue on faster chips e.g. DUE, then a delayMicroseconds(1) needs to be inserted again.
   to be tested.

+ 2 additional test sketches - including one for timing of the read() function
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

jabami

Hi Rob,

thanks again for another very useful library. I´m using a 74HC4050  high to low http://www.nxp.com/documents/data_sheet/74HC4050_CNV.pdf level shifter in order to read the values from a 4 channel MAX31855K Board (http://www.playingwithfusion.com/productview.php?pdid=20&catid=1001).
Now, I have a multimeter capable of measuring the Temp. with a K type thermocouple and the readings I get from the arduino implementation are allways 1 deg lower. Any Ideas why that could be? Tolerances of the MAX31855K ? Did you experience anything similar?

best regards,
Jan

robtillaart

Can it be caused by not an exact 5V voltage?
Or different connectors that bias the resistance.

Is the 1C offset constant over a whole range e.g. 0..100 C
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

jabami

Hi Rob,

the offset seems to be constant over a wide range, but at the moment I´m blaming the MAX31855 itself. As I read the datasheet, it can only give +- 2°C readings.  Or am I getting something wrong where?

Quote
Can it be caused by not an exact 5V voltage?

I´m feeding 3,3 V from the 3,3V Arduino Pin to the 74HC4050 and these seem to be quite constant. Not as an LDO but usable, I guess.

But a bug in your Code is, that it cannot handle lower than 0°C temp. readings. As soon  as temperature drops below 0, the reading will be offset by 2048. Thus being -2047,5 for a temperature value of -0,5. Or maybe I just implemented it wrong, but I could not find any obvious mistake.

Best regards,
Jan



robtillaart


But a bug in your Code is, that it cannot handle lower than 0°C temp. readings. As soon  as temperature drops below 0, the reading will be offset by 2048. Thus being -2047,5 for a temperature value of -0,5. Or maybe I just implemented it wrong, but I could not find any obvious mistake.

Not tested with negative values ...  I'll put it on my todo list to check.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart

Hi Jan,

Confirmed the bug this evening for negative values.
working on a fix.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart

Version 0.1.03 now available on - https://github.com/RobTillaart/Arduino/tree/master/libraries/MAX31855 -

+ fixed the negative temperatures
+ added an (const) offset for the temperature. (version 0.1.02 , released too today)

@Jan,
Thanks again for your feedback!
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

jabami

Hi Rob,

thanks for the fix. Fortunately we have sub zero temperature outside so I can test your new code  :)

Greetings,
Jan

robtillaart

I tested it yesterday with frozen veggies from the fridge, but real life sub zero is way cooler ;)

What I noticed is that the temperature from the PT element was influenced by the internal temperature.
When I put my finger on the 31855 chip the internal temp increased but so did the PT element.

Can you confirm that?
If confirmed I think some drift compensation algorithm might be needed.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

awyork

#10
Jun 22, 2014, 05:59 pm Last Edit: Jun 22, 2014, 06:03 pm by awyork Reason: 1
You may want to use a signed 16 bit integer for the temperatures.  At least until you add the fraction.  These numbers are twos compliment (see the Wikipedia page.)  It would be best to use a signed 12-bit int, but I doubt one is available.  You can OR the negative values by 0xF000 (I think).  Sorry don't have anything to test with yet, but I think by just subtracting 2048 you are introducing a 1 degree error. 

Also regarding the temp error when placing your finger on the chip, see the below from the data sheet.   It should cause an error.  

The device senses and corrects for the changes in
the reference junction temperature with cold-junction
compensation. It does this by first measuring its internal
die temperature, which should be held at the same tem
-
perature as the reference junction. It then measures the
voltage from the thermocouple's output at the reference
junction and converts this to the noncompensated ther
-
mocouple temperature value. This value is then added
to the device's die temperature to calculate the thermo
-
couple's "hot junction" temperature. Note that the "hot
junction" temperature can be lower than the cold junction
(or reference junction) temperature.
Optimal performance from the device is achieved when
the thermocouple cold junction and the device are at
the same temperature. Avoid placing heat-generating
devices or components near the MAX31855 because this
could produce cold-junction-related errors.

robtillaart

Updated to version 0.1.04

+ float replaced by double to support ARM (e.g. DUE)
+ updated examples too


Code can be found on - https://github.com/RobTillaart/Arduino/tree/master/libraries/MAX31855 -

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

vanbcguy

I just switched some code I had from the Adafruit library over to yours, v0.1.04.  I'm seeing all my temperature readings come through at almost exactly 50% of what they should be - ie before startup ambient was 24C and I had a reading of 12C, after I've been running for a bit normal operating temperature at idle should be around 150-160C and I'm reading 75-80C.

I've verified with two other measuring devices plus I know the behavior of my system from the previous code so I'm not sure what is going on here...

Any thoughts?

robtillaart

A factor two could be caused by the code if it misses the least significant bit.

Did you try the example code that comes with the lib or did you use your own code?
If the latter please post it (using [code]...[/code]code tags)

Have you tried the 0.1.03 version, as that was confirmed to work independently by jamabi

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

druidino

Hi rob I'm new in electronics and programmation and i've some trouble run code for for max31855 on my arduino micro, because several errors are signaled by my ver1.0.5 IDE.
That's what he say for the line : MAX31855 tc(clPin, csPin, doPin);

sketch_jun20d:27: error: 'tc' was not declared in this scope

What's the matter?

thank you!

Go Up