Multiple MAX31855 Instances in an array

Breakout boards, sensors, other Adafruit kits, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
wesg
 
Posts: 118
Joined: Thu Mar 18, 2010 10:00 pm

Multiple MAX31855 Instances in an array

Post by wesg »

I'm looking to have multiple MAX31855 sensors in my project, and that means reading them all efficiently. I understand that the chips can already share DO and CS pins, so I'm trying to code up a foundation that makes it really easy to add more sensors.

How can I go about adding multiple objects to a single array and referencing them?

Currently I create the array like this:

Code: Select all

Adafruit_MAX31855 * thermocouples[1];
(I'm only using 1 sensor at the moment)

Then add objects in a loop like this:

Code: Select all

for (int i = 0; i < NUMBER_OF_THERMOCOUPLES; i++) {
    Adafruit_MAX31855 thermocouple(thermoCLKs[i], sharedThermoPins[1], sharedThermoPins[0]);
    thermocouples[i] = thermocouple;  
  }
And finally I try to reference them this way:

Code: Select all

Adafruit_MAX31855 * thermocouple = thermocouples[0];
  Serial.println(thermocouple.readCelsius());
but unfortunately it's not going quite so well.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Multiple MAX31855 Instances in an array

Post by adafruit_support_rick »

Try this:

Code: Select all

#include "Adafruit_MAX31855.h"

int thermoDO = 3;
int thermoCS = 4;
int thermoCLK = 5;

#define NUMBER_OF_THERMOCOUPLES 2
int sharedThermoPins[] = {thermoDO, thermoCS}; 
int thermoCLKs[NUMBER_OF_THERMOCOUPLES] = {thermoCLK, thermoCLK+1};
Adafruit_MAX31855  *thermocouples[NUMBER_OF_THERMOCOUPLES];
  
void setup() {
  Serial.begin(9600);
  
  Serial.println("MAX31855 array test");
  // wait for MAX chip to stabilize
  delay(500);
  
  for (int i = 0; i < NUMBER_OF_THERMOCOUPLES; i++) {
    thermocouples[i] = new Adafruit_MAX31855(thermoCLKs[i], sharedThermoPins[1], sharedThermoPins[0]);  
  }
}

void loop() {
  // basic readout test, just print the current temp
    for (int i = 0; i < NUMBER_OF_THERMOCOUPLES; i++) {
      Serial.print("Internal Temp "); Serial.print(i); Serial.print(" = ");
      Serial.println(thermocouples[i]->readInternal());

      double c = thermocouples[i]->readCelsius();
      if (isnan(c)) {
        Serial.print("Something wrong with thermocouple");Serial.print(i); Serial.println("!");
      }else {
        Serial.print("C ");Serial.print(i); Serial.print(" = "); 
        Serial.println(c);
      }
      //Serial.print("F ");Serial.print(i); Serial.print(" = ");
      //Serial.println(thermocouples[i].readFarenheit());
   }
   delay(1000);
}

User avatar
wesg
 
Posts: 118
Joined: Thu Mar 18, 2010 10:00 pm

Re: Multiple MAX31855 Instances in an array

Post by wesg »

Thanks, Driver, that looks good. It seems, though, that Arduino does not support the "new" keyword, so I haven't yet added the thermocouples to the array.

mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am

Re: Multiple MAX31855 Instances in an array

Post by mtbf0 »

wesg wrote:I understand that the chips can already share DO and CS pins
should that not be DO and CLK? though it seems as if you could just pull CS low on every board, share the data line and strobe individual clocks per chip.

... unless i didn't read far enough into the product description to see that there's a good reason one might want to deselect all of one's MAX31855s. noise maybe...

look at me! i still haven't learned to just shut up when i know not whereof i speak, but i, at least, am now capable of thinking of reasons why i should.

User avatar
wesg
 
Posts: 118
Joined: Thu Mar 18, 2010 10:00 pm

Re: Multiple MAX31855 Instances in an array

Post by wesg »

mtbf0 wrote:
wesg wrote:I understand that the chips can already share DO and CS pins
should that not be DO and CLK? though it seems as if you could just pull CS low on every board, share the data line and strobe individual clocks per chip.
You're right about that, my mistake! This thread has the answer, which means I need to readjust my pin assignments.

mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am

Re: Multiple MAX31855 Instances in an array

Post by mtbf0 »

so how about

Code: Select all

#include "Adafruit_MAX31855.h"

int thermoDO = 3;
int thermoCS = 4;
int thermoCLK = 5;

#define NUMBER_OF_THERMOCOUPLES 3

Adafruit_MAX31855 tc0 (thermoCLK, 3, thermoCLK);
Adafruit_MAX31855 tc1 (thermoCLK, 6, thermoCLK);
Adafruit_MAX31855 tc2 (thermoCLK, 7, thermoCLK);

Adafruit_MAX31855  *thermocouples = {tc0, tc1, tc2};

void setup() {
  Serial.begin(9600);
  Serial.println("MAX31855 array test");
// wait for MAX chip to stabilize
  delay(500);
}

void loop() {
  // basic readout test, just print the current temp
    for (int i = 0; i < NUMBER_OF_THERMOCOUPLES; i++) {
      Serial.print("Internal Temp "); Serial.print(i); Serial.print(" = ");
      Serial.println(thermocouples[i]->readInternal());
      double c = thermocouples[i]->readCelsius();
      if (isnan(c)) {
        Serial.print("Something wrong with thermocouple");
        Serial.print(i); Serial.println("!");
      }else {
        Serial.print("C ");Serial.print(i); Serial.print(" = ");
        Serial.println(c);
      }
      //Serial.print("F ");Serial.print(i); Serial.print(" = ");
      //Serial.println(thermocouples[i].readFarenheit());
   }
   delay(1000);
}

User avatar
wesg
 
Posts: 118
Joined: Thu Mar 18, 2010 10:00 pm

Re: Multiple MAX31855 Instances in an array

Post by wesg »

Thanks to this Arduino.cc thread I've figured it out using a dynamic MALLOC call.

For future reference, I'll try to outline the different components I used to make this work, using the MAX31855 library.

Code: Select all

// declare a dynamic array variable above setup()
Adafruit_MAX31855 * thermocouples;

// create the thermocouple objects you want (inside the setup() function). The variables should be self explanatory
void createThermocouples() {
  thermocouples = (Adafruit_MAX31855 *) malloc(sizeof(Adafruit_MAX31855) * NUMBER_OF_THERMOCOUPLES);
  for (int i = 0; i < NUMBER_OF_THERMOCOUPLES; i++) {
    Adafruit_MAX31855 thermocouple(sharedThermoPins[1], thermoCSs[i], sharedThermoPins[0]);
    thermocouples[i] = thermocouple;
  }
}

// read a thermocouple inside loop(). Use an array to print them all
 double reading = thermocouples[0].readCelsius();
I can provide my full code if necessary. The array used in declaring the objects is just my way of enclosing the different sensors.

mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am

Re: Multiple MAX31855 Instances in an array

Post by mtbf0 »

if you know the number of sensors at compile time, i'd think it would be better to stay away from malloc, which could lead to tears in case you ever get memory constrained.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Multiple MAX31855 Instances in an array

Post by adafruit_support_rick »

wesg wrote:It seems, though, that Arduino does not support the "new" keyword
Well, I compiled and ran that code with Arduino 1.0.1. It didn't complain about 'new'.

User avatar
jpwerner1505
 
Posts: 5
Joined: Sun Sep 29, 2013 9:31 pm

Re: Multiple MAX31855 Instances in an array

Post by jpwerner1505 »

wesg wrote:Thanks to this Arduino.cc thread I've figured it out using a dynamic MALLOC call.

For future reference, I'll try to outline the different components I used to make this work, using the MAX31855 library.

Code: Select all

// declare a dynamic array variable above setup()
Adafruit_MAX31855 * thermocouples;

// create the thermocouple objects you want (inside the setup() function). The variables should be self explanatory
void createThermocouples() {
  thermocouples = (Adafruit_MAX31855 *) malloc(sizeof(Adafruit_MAX31855) * NUMBER_OF_THERMOCOUPLES);
  for (int i = 0; i < NUMBER_OF_THERMOCOUPLES; i++) {
    Adafruit_MAX31855 thermocouple(sharedThermoPins[1], thermoCSs[i], sharedThermoPins[0]);
    thermocouples[i] = thermocouple;
  }
}

// read a thermocouple inside loop(). Use an array to print them all
 double reading = thermocouples[0].readCelsius();
I can provide my full code if necessary. The array used in declaring the objects is just my way of enclosing the different sensors.
If you wouldn't mind posting the code, it would be of great help to those of us just getting started :D Thanks!

eltron247
 
Posts: 1
Joined: Tue Dec 03, 2013 10:05 pm

Re: Multiple MAX31855 Instances in an array

Post by eltron247 »

I apologize ahead of time as I realize this post is old, however, I second that request. The code would be very helpful to us beginners if you would be so kind as to post it.

User avatar
Wbru
 
Posts: 1
Joined: Mon Oct 01, 2018 9:19 am

Re: Multiple MAX31855 Instances in an array

Post by Wbru »

I am new on this forum
I am Wim (Bill) Brussee
I com from the netherlands (and my englisch is not so good)
I am working on a sketch for a speksteen stove
I have a sketch for one max31855 and this works good but the sketch for two max31855 is a problem
I hope you can help me with the full code for two max31855 for the arduino pro mini i use the Arduino IDE 1.8.7

Greatings,

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Products from Adafruit”