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:
more explicit status information -> Adafruit returns NAN when there is a failure
The object now automatically is a buffer for the last value read.
As always remarks and comments are welcome.
demo sketch
//
// 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();
}
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
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 (Playing With Fusion - MAX31855 K-Type Thermocouple Sensor Breakout (4ch)).
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?
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?
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.
jabami:
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.
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.
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.
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...
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
I used the sketch you published at the top of this post but also the adafruit library on github, but anyone works for the same reason, I thought was my IDE version too old, but i'm working with an old mac and i can't upload new versions.
MAX31855.cpp:19:22: error: MAX31855.h: No such file or directory
MAX31855.cpp:21: error: 'MAX31855' has not been declared
MAX31855.cpp:21: error: expected constructor, destructor, or type conversion before '(' token
MAX31855.cpp:29: error: 'MAX31855' has not been declared
MAX31855.cpp: In function 'void begin()':
MAX31855.cpp:31: error: '_cs' was not declared in this scope
MAX31855.cpp:31: error: 'OUTPUT' was not declared in this scope
MAX31855.cpp:31: error: 'pinMode' was not declared in this scope
MAX31855.cpp:32: error: 'HIGH' was not declared in this scope
MAX31855.cpp:32: error: 'digitalWrite' was not declared in this scope
MAX31855.cpp:34: error: '_sclk' was not declared in this scope
MAX31855.cpp:35: error: '_miso' was not declared in this scope
MAX31855.cpp:35: error: 'INPUT' was not declared in this scope
MAX31855.cpp: At global scope:
MAX31855.cpp:38: error: 'uint8_t' does not name a type
MAX31855.cpp:75: error: 'uint32_t' does not name a type