Max31855x8 and reading loops

Dear friends,

I would like to select specific channel in max31855x8 measurement for further calculations . I read previous discussion and I tried also improved found codes. However, I did not found any solution and solved topic with this question.

I am using Arduino Uno connected to max31855x8. In basic example code datalogging works perfectly, but I am not able extract from loop specific channel (Therm) for further calculations. I want to connect another pressure transducers and use this temperatures for measurement of velocity on each pitot tube.

Please find improved code:

#include <MAX31855.h>

// Pin connections to the MAX31855x8 board 
// The power requirement for the board is less than 2mA.  Most microcontrollers can source or sink a lot more
// than that one each I/O pin.  For example, the ATmega328 supports up to 20mA.  For convenience, the board
// is placed directly on top of a row of I/O pins on the microcontroller.  Power is supplied to the board by
// holding the GND pin low and the VIN pin high
#define GND  GND
#define T0   4
#define T1   5
#define T2   6
#define VIN  5V
#define MISO 8
#define CS   9
#define SCK  10

int counter = 0;
// Create the temperature object, defining the pins used for communication
MAX31855 temp = MAX31855(MISO, CS, SCK);

void setup() {
  // Display temperatures using the serial port
  Serial.begin(9600);
  
  // Initialize pins
  pinMode(GND, OUTPUT);
  pinMode(T0, OUTPUT);
  pinMode(T1, OUTPUT);
  pinMode(T2, OUTPUT);
  pinMode(VIN, OUTPUT);
  
  // Power up the board
  digitalWrite(GND, LOW);
  digitalWrite(VIN, HIGH);
  delay(200);
}


void loop () {
  // Display the junction temperature
//  float temperature = temp.readJunction(CELSIUS);
//  Serial.print("J=");
//  printTemperature(temperature);
    
  // Display the temperatures of the 8 thermocouples
  for (int therm=0; therm<8; therm++) {
    // Select the thermocouple
    digitalWrite(T0, therm & 1? HIGH: LOW);
    digitalWrite(T1, therm & 2? HIGH: LOW);
    digitalWrite(T2, therm & 4? HIGH: LOW);
    // The MAX31855 takes 100ms to sample the thermocouple.
    // Wait a bit longer to be safe.  We'll wait 0.125 seconds
    delay(125);
    
   float temperature = temp.readThermocouple(CELSIUS);
    if (temperature == FAULT_OPEN)
        continue;
    Serial.print(" T");
    Serial.print(therm);
    Serial.print("=");
   // printTemperature(temperature);
    Storefile(temperature);

  }

}


// Print the temperature, or the type of fault
void printTemperature(double temperature) {
  switch ((int) temperature) {
    case FAULT_OPEN:
      Serial.print("FAULT_OPEN");
      break;
    case FAULT_SHORT_GND:
      Serial.print("FAULT_SHORT_GND");
      break;
    case FAULT_SHORT_VCC:
      Serial.print("FAULT_SHORT_VCC");
      break;
    case NO_MAX31855:
      Serial.print("NO_MAX31855");
      break;
    default:
      Serial.print(temperature);
      break;
  }

}


void Storefile(float therm){
    while(1){//while loop

    if(counter==0){
      float I0=therm;
      float ro0 = 352.6 / (I0 + 273.15);
      counter++;
      Serial.print(" T0");
      Serial.print("=");
      Serial.print(I0);
      Serial.print(" ro0= ");
      Serial.print("=");
      Serial.print(ro0);
      delay(125);
      break; }
    }//counter 0

    if(counter==1){
      float I1=therm;
      float ro1 = 352.6 / (I1 + 273.15);
      counter++;
      Serial.print(" T1");
      Serial.print("=");
      Serial.print(I1);
      Serial.print(" ro1= ");
      Serial.print("=");
      Serial.print(ro1);
      delay(125);

    }

    if(counter==2){
      float I2=therm;
      counter++;
      Serial.print(" T2");
      Serial.print("=");
      Serial.print(I2);
      delay(125);

    }

    if(counter==3){
      float I3=therm;
      float ro3 = 352.6 / (I3 + 273.15);
      counter++;
      Serial.print(" T3");
      Serial.print("=");
     Serial.print(I3);
      delay(125);

    }

    if(counter==4){
      float I4=therm;
      counter++;
     Serial.print(" T4");
      Serial.print("=");
      Serial.print(I4);
      delay(125);

    }

    if(counter==5){
      float I5=therm;
      counter++;      Serial.print(" T5");
      Serial.print("=");
     Serial.print(I5);
      delay(125);
 }
    }

    if(counter==6){
      float I6=therm;
      counter++;
      Serial.print(" T6");      
      Serial.print("=");     
      Serial.print(I6);
      delay(125);

    }

    if(counter==7){
      float I7=therm;
      counter++;
      Serial.print(" T7");
      Serial.print("=");
      Serial.print(I7);

    }

    if(counter==8){
      counter=0;//reset counter to 0
     Serial.println();
 }
   
    }

Would you recommend me, please, how to fix the code?

These defines are supposed to reference pins

#define GND  GND
#define T0   4
#define T1   5
#define T2   6
#define VIN  5V
#define MISO 8
#define CS   9
#define SCK  10

but your GND and VIN defines are not pins. If you are connecting that part directly to GND and 5V, then you do not want to be treating these as pins and setting them to OUTPUT.

Inside your Storefile() function, you test counter but then you increment it so ALL of the tests will evaluate true. I think you want to increment counter once, at the end of the function.

blh64:
These defines are supposed to reference pins

#define GND  GND

#define T0  4
#define T1  5
#define T2  6
#define VIN  5V
#define MISO 8
#define CS  9
#define SCK  10



but your GND and VIN defines are not pins. If you are connecting that part directly to GND and 5V, then you do not want to be treating these as pins and setting them to OUTPUT.

Inside your Storefile() function, you test counter but then you increment it so ALL of the tests will evaluate true. I think you want to increment counter once, at the end of the function.

Thank you very much for your reply. I tried to adapt code based on your recommendation and it almost work. There is only issue, which I cannot solve. Please find following improved function:

void Storefile(float therm){
 
  while(1){//while loop
    if(counter==0){
     i0=therm;
      Serial.print(" T0");
      Serial.print("=");
      Serial.print(i0);
     // counter++;
      break; 
    }// counter 0

    if(counter==1){
      i1=therm; 
      float ro1 = (352.6 / (i1 + 273.15));
      Serial.print(" T1");
      Serial.print("=");
      Serial.print(i1);
      Serial.print(" rho1");
      Serial.print("=");
      Serial.print(ro1,3);
      Serial.print(" ");
      //forward file here
      break; 
  }

     if(counter==2){
      i2=therm;
      Serial.print(" T2");
      Serial.print("=");
      Serial.print(i2);
     break; 
  }

     if(counter==3){
      i3=therm;
      Serial.print(" T3");
      Serial.print("=");
      Serial.print(i3);
      break; 
  }

     if(counter==4){
      i4=therm;
      /*float temperature = temp.readJunction(CELSIUS);
      temperature = temp.readThermocouple(CELSIUS);
      if (temperature == FAULT_OPEN)
        continue;*/
      Serial.print(" T4");
      Serial.print("=");
      Serial.print(i4);
      break;
    }

     if(counter==5){
      i5=therm;
      Serial.print(" T5");
      Serial.print("=");
      Serial.print(i5);
      break; 
  }

     if(counter==6){
      i6=therm;
      Serial.print(" T6");
      Serial.print("=");
      Serial.print(i6);
     break;
    }

     if(counter==7){
      i7=therm;
      Serial.print(" T7");
      Serial.print("=");
      Serial.print(i7);
      
      break;
      
    }

   if(counter==8){
     TAVG=(i0+i1+i2+i3+i4+i5+i6+i7)/8;
      Serial.print(" TAVG");
      Serial.print("=");
      Serial.print(TAVG);
     counter=0;
     //where to store
     break;
    }//if statement*/
 
      }//while loop
    //reset counter to 0
       counter++;
}//void storefile

The issue is, the serial monitor shows following reading:

17:08:17.964 -> J=26.31  TAVG=26.31 T1=26.50 rho1=1.177  T2=25.75 T3=26.00 T4=26.75 T5=26.75 T6=26.50 T7=26.50

Somehow in monitor is missed first reading for T0 from While cycle and the T0 value is replaced by TAVG and I do not know how to fix it… :o

That is not a complete sketch. Please post your complete sketch so people can see everything. I have no idea what the initial value of counter is so I can't help.

Also, there is no reason to have a while(1) {..} loop inside your Storefile() function. You basically print one item and then break out of it. Just remove it and remove all the break statements.

I am sorry. Please, find sketch of code below:

#include <MAX31855.h>
#include <Wire.h>

// Pin connections to the MAX31855x8 board 
// The power requirement for the board is less than 2mA.  Most microcontrollers can source or sink a lot more
// than that one each I/O pin.  For example, the ATmega328 supports up to 20mA.  For convenience, the board
// is placed directly on top of a row of I/O pins on the microcontroller.  Power is supplied to the board by
// holding the GND pin low and the VIN pin high
#define GND  3
#define T0   4
#define T1   5
#define T2   6
#define VIN  7
#define MISO 8
#define CS   9
#define SCK  10

//MAX31855 definition
float i0=0, i1=0, i2=0, i3=0, i4=0, i5=0, i6=0, i7=0;
float ro0=0, ro1=0, ro2=0, ro3=0, ro4=0, ro5=0, ro6=0, ro7=0;
float v0=0;
extern void Selection(int);     
int counter = 0;
float TAVG = 0;

// Create the temperature object, defining the pins used for communication
MAX31855 temp = MAX31855(MISO, CS, SCK);

// velocity
float velocity = 0;
 

void setup() {
  // Display temperatures using the serial port
  Serial.begin(9600);

  // Initialize pins
  pinMode(GND, OUTPUT);
  pinMode(T0, OUTPUT);
  pinMode(T1, OUTPUT);
  pinMode(T2, OUTPUT);
  pinMode(VIN, OUTPUT);
  
  // Power up the board
  digitalWrite(GND, LOW);
  digitalWrite(VIN, HIGH);
  delay(200);
}


void loop () {
  // Display the junction temperature
  float temperature = temp.readJunction(CELSIUS);
  Serial.print("J=");
  printTemperature(temperature);
    
  // Display the temperatures of the 8 thermocouples
  for (int therm=0; therm<8; therm++) {
    // Select the thermocouple
    digitalWrite(T0, therm & 1? HIGH: LOW);
    digitalWrite(T1, therm & 2? HIGH: LOW);
    digitalWrite(T2, therm & 4? HIGH: LOW);
    // The MAX31855 takes 100ms to sample the thermocouple.
    // Wait a bit longer to be safe.  We'll wait 0.125 seconds
    delay(125);
        temperature = temp.readThermocouple(CELSIUS);
    if (temperature == FAULT_OPEN)
      continue;
 //   Serial.print(" T");
 //   Serial.print(therm);
 //   Serial.print("=");
 //   printTemperature(temperature);
    Selection(temperature);
  }
  
  Serial.println();
}


// Print the temperature, or the type of fault
void printTemperature(double temperature) {
  switch ((int) temperature) {
    case FAULT_OPEN:
      Serial.print("FAULT_OPEN");
      break;
    case FAULT_SHORT_GND:
      Serial.print("FAULT_SHORT_GND");
      break;
    case FAULT_SHORT_VCC:
      Serial.print("FAULT_SHORT_VCC");
      break;
    case NO_MAX31855:
      Serial.print("NO_MAX31855");
      break;
    default:
      Serial.print(temperature);
      break;
  }
  Serial.print(" ");
}


void Selection(float therm){
    if(counter==0){
     i0=therm;
      Serial.print(" T0");
      Serial.print("=");
      Serial.print(i0);
    }// counter 0

    if(counter==1){
      i1=therm; 
      float ro1 = (352.6 / (i1 + 273.15));
      Serial.print(" T1");
      Serial.print("=");
      Serial.print(i1);
      Serial.print(" Rho1");
      Serial.print("=");
      Serial.print(ro1,3);
      Serial.print(" ");
  }

     if(counter==2){
      i2=therm;
      float ro2 = (352.6 / (i2 + 273.15));
      Serial.print(" T2");
      Serial.print("=");
      Serial.print(i2);
      Serial.print(" Rho2");
      Serial.print("=");
      Serial.print(ro2,3);
      Serial.print(" ");
  }

     if(counter==3){
      i3=therm;
      float ro3 = (352.6 / (i3 + 273.15));
      Serial.print(" T3");
      Serial.print("=");
      Serial.print(i3);
      Serial.print(" Rho3");
      Serial.print("=");
      Serial.print(ro3,3);
      Serial.print(" ");
  }

     if(counter==4){
      i4=therm;
      float ro4 = (352.6 / (i4 + 273.15));
      Serial.print(" T4");
      Serial.print("=");
      Serial.print(i4);
      Serial.print(" Rho4");
      Serial.print("=");
      Serial.print(ro4,3);
      Serial.print(" ");
    }

     if(counter==5){
      i5=therm;
      float ro5 = (352.6 / (i5 + 273.15));
      Serial.print(" T5");
      Serial.print("=");
      Serial.print(i5);
      Serial.print(" Rho5");
      Serial.print("=");
      Serial.print(ro5,3);
      Serial.print(" ");
  }

     if(counter==6){
      i6=therm;
      float ro6 = (352.6 / (i6 + 273.15));
      Serial.print(" T6");
      Serial.print("=");
      Serial.print(i6);
      Serial.print(" Rho6");
      Serial.print("=");
      Serial.print(ro6,3);
      Serial.print(" ");
    }

     if(counter==7){
      i7=therm;
      float ro7 = (352.6 / (i7 + 273.15));
      Serial.print(" T7");
      Serial.print("=");
      Serial.print(i7);
      Serial.print(" Rho7");
      Serial.print("=");
      Serial.print(ro7,3);
      Serial.print(" ");
    }

  if(counter==8){
     TAVG=(i0+i1+i2+i3+i4+i5+i6+i7)/8;
      Serial.print(" TAVG");
      Serial.print("=");
      Serial.print(TAVG);
      counter=0;

    }//if statement
       counter++;
}

If your first reading is an error, there is a ‘continue’ statement. You do not need all the duplicate code or the counter variable

#include <MAX31855.h>
#include <Wire.h>

// Pin connections to the MAX31855x8 board
// The power requirement for the board is less than 2mA.  Most microcontrollers can source or sink a lot more
// than that one each I/O pin.  For example, the ATmega328 supports up to 20mA.  For convenience, the board
// is placed directly on top of a row of I/O pins on the microcontroller.  Power is supplied to the board by
// holding the GND pin low and the VIN pin high
#define GND  3
#define T0   4
#define T1   5
#define T2   6
#define VIN  7
#define MISO 8
#define CS   9
#define SCK  10

float TAVG = 0;

// Create the temperature object, defining the pins used for communication
MAX31855 temp = MAX31855(MISO, CS, SCK);



void setup() {
  // Display temperatures using the serial port
  Serial.begin(9600);

  // Initialize pins
  pinMode(GND, OUTPUT);
  pinMode(T0, OUTPUT);
  pinMode(T1, OUTPUT);
  pinMode(T2, OUTPUT);
  pinMode(VIN, OUTPUT);

  // Power up the board
  digitalWrite(GND, LOW);
  digitalWrite(VIN, HIGH);
  delay(200);
}


void loop () {
  // Display the junction temperature
  float temperature = temp.readJunction(CELSIUS);
  Serial.print("J=");
  printTemperature(temperature);

  float TAVG = 0;
  // Display the temperatures of the 8 thermocouples
  for (int therm = 0; therm < maxChannels; therm++) {
    // Select the thermocouple
    digitalWrite(T0, therm & 1 ? HIGH : LOW);
    digitalWrite(T1, therm & 2 ? HIGH : LOW);
    digitalWrite(T2, therm & 4 ? HIGH : LOW);
    // The MAX31855 takes 100ms to sample the thermocouple.
    // Wait a bit longer to be safe.  We'll wait 0.125 seconds
    delay(125);
    float temperature = temp.readThermocouple(CELSIUS);
    Selection(therm, temperature);
    TAVG += temperature;
  }
  Serial.print(" TAVG");
  Serial.print("=");
  Serial.print(TAVG / 8);
  Serial.println();
}


// Print the temperature, or the type of fault
void printTemperature(double temperature) {
  switch ((int) temperature) {
    case FAULT_OPEN:
      Serial.print("FAULT_OPEN");
      break;
    case FAULT_SHORT_GND:
      Serial.print("FAULT_SHORT_GND");
      break;
    case FAULT_SHORT_VCC:
      Serial.print("FAULT_SHORT_VCC");
      break;
    case NO_MAX31855:
      Serial.print("NO_MAX31855");
      break;
    default:
      Serial.print(temperature);
      break;
  }
  Serial.print(" ");
}


void Selection(int counter, float therm) {
  float ro;
  if (counter == 0) {
    Serial.print(" T0");
    Serial.print("=");
    Serial.print(therm);
  }
  else {
    ro = (352.6 / (therm + 273.15));
    Serial.print(" T"); Serial.print(counter);
    Serial.print("=");
    Serial.print(therm);
    Serial.print(" Rho"); Serial.print(counter);
    Serial.print("=");
    Serial.print(ro, 3);
    Serial.print(" ");
  }
}