Adafruit MAX31855 library

Hello All,

I’m looking to get a library for the MAX31855 working for the spark core. This chip is a thermocouple to serial amplifier. The git hub for the original by adafruit and the data sheet are linked to below.

I’m not that good at fixing libraries. I have a version that I attempted to fix a few of the errors that come up, but haven’t made much progress. Any help would be greatly appreciated.

This is still a work in progress, but this is working for me. I still have some cleanup to do.

double thermoTemp = 0;
double internalTemp = 0;
bool hasError = false;
bool scvFault = false;
bool scgFault = false;
bool ocFault = false;
unsigned long raw = 0;

typedef struct {
    int ThermoTemp : 14;
    unsigned char Reserved : 1;
    bool Fault : 1;
    int InternalTemp : 12;
    unsigned char Reserved2 : 1;
    bool SCVFault : 1;
    bool SCGFault : 1;
    bool OCFault : 1; 
} Temp;

union Reading {
  unsigned long bit32;
  unsigned int bit16[2];
  unsigned char bit8[4];
  Temp temp;
};


void setup() {
    Spark.variable("hasError", &hasError, BOOLEAN);
    Spark.variable("scvFault", &scvFault, BOOLEAN);
    Spark.variable("scgFault", &scgFault, BOOLEAN);
    Spark.variable("ocFault", &ocFault, BOOLEAN);
    Spark.variable("thermoTemp", &thermoTemp, DOUBLE);
    Spark.variable("internalTemp", &internalTemp, DOUBLE);
    Spark.variable("raw", &raw, INT);

    SPI.setClockDivider(SPI_CLOCK_DIV64);
    SPI.setBitOrder(MSBFIRST);
    SPI.setDataMode(SPI_MODE3);
    SPI.begin();
}

void loop() {
    Reading reading;
    
    //clear the data
    reading.bit32 = 0;
    
    digitalWrite(SS, LOW);
    delay(1);
    
    //read in the 32-bit value
    for(int i =3; i >= 0 ; i--) {
        reading.bit8[i] = SPI.transfer(0);
    }
    
    thermoTemp = (0x80000000 & reading.bit32 ? -1.0 : 1.0) * (double)((reading.bit32 >> 18) & 0x00001FFF) / 4.0;
    internalTemp =(0x00008000 & reading.bit32 ? -1.0 : 1.0) * (double)((reading.bit32 >> 4) & 0x000007FF) / 16.0;
    
    hasError = reading.temp.Fault;
    scvFault = reading.temp.SCVFault;
    scgFault = reading.temp.SCGFault;
    ocFault = reading.temp.OCFault;
    raw = reading.bit32;
    
    digitalWrite(SS, HIGH);    
    delay(100);
}

Thanks mattdot do you think you could do me a huge favor and add a couple more comments to this? I've read it over and I think I know what's going on, except I don't see how you define the inputs for the SPI and can't find much information about it. Could you tell me how you are connecting to your MAX31855 and what you are using to read out he data?

Thanks,

Dave

Nevermind. I got this working with the help of http://docs.spark.io/#/firmware/communication-spi

Another question: I have a board that has multiple MAX31855 chips on it that are on the same SPI line, but have different Chip Select lines (SS in your code)

I saw that you had problems in another post with using SPI on the core when you were not using the SS line. I would like to eventually be able to put this into a library or a function so that I can call it multiple times in a row using different pins for SS to read multiple thermocouples.

If you can help me with that it would be awesome!

Dave

I have plans to do the same thing with multiple thermocouples. I thought I could use different pins for CS/SS of each one, but I ran into the issues I posted on the other thread about having to use SS. I may try to revisit it and see what the issue is, but the work around would be to have another “switch” IC that routes SS to the right MAX31855. I’m more of a software guy so I’m still trying to figure out what the best component would be if it comes to that. Maybe a few transistors or something.

I was also debating whether I should use just one MAX31855 and just switch which thermocouple is connected to it. That might be the best solution to keep costs down. If I had 2 thermocouples, I’d just need to use one other output pin to control the switch. If I had 4 thermocouples, I would just need 2 output pins more.

Hey I got mine working on 4 chips. I had previously bought this: http://www.ebay.com/itm/MAX31855K-Quad-Ch-Type-K-Thermocouple-Breakout-270C-1372C-MAX6675-upgrade-/330866913450?pt=LH_DefaultDomain_0&hash=item4d093470aa and so far this works.

It also includes code to format the data for parsing with json and read the thermocouples out to a google spreadsheet.

It's still prob got a lot of stuff wrong with it.

double thermoTemp = 0;
double internalTemp = 0;
bool hasError = false;
bool scvFault = false;
bool scgFault = false;
bool ocFault = false;
unsigned long raw = 0;

typedef struct {
int ThermoTemp : 14;
unsigned char Reserved : 1;
bool Fault : 1;
int InternalTemp : 12;
unsigned char Reserved2 : 1;
bool SCVFault : 1;
bool SCGFault : 1;
bool OCFault : 1;
} Temp;

union Reading {
unsigned long bit32;
unsigned int bit16[2];
unsigned char bit8[4];
Temp temp;
};

char resultstr[64];

void setup() {

pinMode(D0,OUTPUT); //Set these pins to output for CS0-3
pinMode(D1,OUTPUT);
pinMode(D2,OUTPUT);
pinMode(D3,OUTPUT);

pinMode(D7,OUTPUT); // Turn on the D7 led so we know it's time
digitalWrite(D7,HIGH); // to open the Serial Terminal.
Serial.begin(9600); // Open serial over USB.
// while(!Serial.available()); // Wait here until the user presses ENTER in the Serial Terminal
digitalWrite(D7,LOW); // Turn off the D7 led ... your serial is serializing!
Serial.println("MAX31855 test");
// wait for MAX chip to stabilize
delay(1000);

Spark.variable("hasError", &hasError, BOOLEAN);
Spark.variable("scvFault", &scvFault, BOOLEAN);
Spark.variable("scgFault", &scgFault, BOOLEAN);
Spark.variable("ocFault", &ocFault, BOOLEAN);
Spark.variable("thermoTemp", &thermoTemp, DOUBLE);
Spark.variable("internalTemp", &internalTemp, DOUBLE);
Spark.variable("raw", &raw, INT);
Spark.variable("result", &resultstr, STRING); 
SPI.setClockDivider(SPI_CLOCK_DIV64);
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE3);
SPI.begin();

}

void loop() {

//Connect SO to A4
//Connect SCK to A3
//CS0-3 to D0-3
digitalWrite(D0, HIGH);
digitalWrite(D1, HIGH);
digitalWrite(D2, HIGH);
digitalWrite(D3, HIGH);
double t0;
double t1;
double t2;
double t3;
t0 = readChip(0,1);
t1 = readChip(1,1);
t2 = readChip(2,1);
t3 = readChip(3,1);
sprintf(resultstr, "{\"t2\":%f,\"t3\":%f}", t2, t3); 
delay(1000);

}

double readChip(int chip, bool print){
//read the temperature from the thermocouple connected to 'chip'
//print the data to the serial port if print is true
Reading reading;

//clear the data
reading.bit32 = 0;
digitalWrite(("D",chip), LOW);
delay(1);
//read in the 32-bit value
for(int i =3; i >= 0 ; i--) {
    reading.bit8[i] = SPI.transfer(0);
}
thermoTemp = (0x80000000 & reading.bit32 ? -1.0 : 1.0) * (double)((reading.bit32 >> 18) & 0x00001FFF) / 4.0;
internalTemp =(0x00008000 & reading.bit32 ? -1.0 : 1.0) * (double)((reading.bit32 >> 4) & 0x000007FF) / 16.0;
hasError = reading.temp.Fault;
scvFault = reading.temp.SCVFault;
scgFault = reading.temp.SCGFault;
ocFault = reading.temp.OCFault;
raw = reading.bit32;
digitalWrite(("D",chip), HIGH);
double tempF;
tempF =  (thermoTemp*9.0/5.0+32.0);
if (print = true) {
  Serial.print("T");
  Serial.print(chip);
  Serial.print(" = ");
  Serial.print(tempF);
  Serial.print(" F");
  Serial.println(""); 
}
delay(100);
return tempF;

}