Thermocouple Multiplexer and Amplifier Boards

I'm trying to read in 8 thermocouples on an Ocean Controls KTA 259 board, along with two additional Max31856 thermocouple amplifier boards and send the data to MATLAB. The first time each thermocouple is read everything works fine, but in each additional read the temperatures associated with "B" and "C" read Nan and 256 respectively, which are definitely not correct. I think the multiplexer code is what is causing the problems, but I cannot find any libraries in Arduino that have code for this specific multiplexer. I'm not the best at Arduino so some of the code came from online libraries.

int flowmetervalue;
int powervalue;
long t;
int incomingByte=10;
bool delaysetflag=false;
float flowmetervoltage;
float powervoltage;
float tccj1;
float tctemp1;
float tccj2;
float tctemp2;
long tz;
int m=1; //1 is slow (for setting up measurement), 0 is fast
bool stop=0; //data is sent while stop=0, data is not sent when stop=1
// int modePin=8; 
// int stopPin=10;
// int incomingByte=10;
// bool delaysetflag=false;

#include <Adafruit_MAX31856.h>
#include "TCMuxShield.h"
TCMuxShield tc = TCMuxShield(9); 

Adafruit_MAX31856 tc1 = Adafruit_MAX31856(5, 2, 3, 4);
Adafruit_MAX31856 tc2 = Adafruit_MAX31856(6, 2, 3, 4); 

void setup() {
  // put your setup code here, to run once:
  Serial.begin(500000);
  tz=micros();

  tc1.begin();
  tc1.setThermocoupleType(MAX31856_TCTYPE_K);

  tc2.begin();
  tc2.setThermocoupleType(MAX31856_TCTYPE_K);
}

void loop() {

  //Aqcuire Data
    flowmetervalue=analogRead(0); 
    powervalue=analogRead(1);
    flowmetervoltage = flowmetervalue * 5.0/1023;
    powervoltage = powervalue * 5.0/1023;  

    tccj1=tc1.readCJTemperature();
    tctemp1=tc1.readThermocoupleTemperature();

// if (tctemp1>1){}
// else {tctemp1=0.1;}

    tccj2=tc2.readCJTemperature();
    tctemp2=tc2.readThermocoupleTemperature();

  //Acquire Time
    t=micros();

  //Send data
    if (true){
      Serial.print('X'); Serial.print(flowmetervoltage);
      Serial.print('Y'); Serial.print(powervoltage);
      Serial.print('A'); Serial.print(tccj1);
      Serial.print('B'); Serial.print(tctemp1);
      Serial.print('C'); Serial.print(tccj2);
      Serial.print('D'); Serial.print(tctemp2);
      //Serial.print('T'); Serial.print(t);   
      //Serial.print('!'); // "!" is playing the role of termination character
    
    for (char i = 1; i < 9; i++) 
    {
        tc.readTemperature(i);
        float temp = tc.temperature;
        Serial.print('E');
        Serial.print(temp);
    }

      Serial.print('T'); Serial.print(t);   
      Serial.print('!'); // "!" is playing the role of termination character
    }


  //Recieve data
    if (Serial.available()>0){
      incomingByte=Serial.read();
      delaysetflag=true;
    }

  //Delay
    if (incomingByte>0){
      delay(incomingByte*4);
    }

    //Closed State, what does this do?
    if(incomingByte==255){
    }
    else {
    }

}

Not enough information, but thank you for posting the code correctly on your first post.

this specific multiplexer

What multiplexer?

Forum members will need to see a complete wiring diagram of the setup, with pins, connections and parts clearly labeled (hand drawn is preferred). Also post links to the product pages or manuals for the external components.

Additional advice can be found in the "How to get the best out of this forum" post.

I noticed that when you are establishing serial communication, you are setting the speed to 500000. As far as I know this isn't a standard value. Having an inconsistency between your declared serial speed and the speed that your slave device is looking for can definitely cause the sort of errors you are seeing.

I would recommend checking your KTA 259 board for what baud rate it actually is expecting, then set your Serial.begin statement to match.

Hope this helps!

500000 is indeed a standard UART serial baud rate, but is not supported by many devices.

This is also a file that needs to be in the same folder for the code to run.

#ifndef _TCMUXSHIELD_H
#define _TCMUXSHIELD_H

// Pins
#define MUX_A0 4
#define MUX_A1 5
#define MUX_A2 6
#define MUX_EN 7

// MAX31855 error codes
#define TC_OK   0
#define TC_OC   1
#define TC_SCG  2
#define TC_SCV  4

// MAX31855 register bits
#define MAX31855_SCV_BIT            2
#define MAX31855_SCG_BIT            1
#define MAX31855_OC_BIT             0
#define MAX31855_TCDATA_OFFSET      18
#define MAX31855_INTDATA_OFFSET     4
#define MAX31855_14B_SIGN_OFFSET    13
#define MAX31855_12B_SIGN_OFFSET    11

class TCMuxShield {
    
    public:
        // Constructor
        TCMuxShield(int _CS);
        
        // Methods
        int readTemperature(int tc_n);

        // Attributes
        float temperature;
    
    private:
        // Methods
        int getTCData(void);
        void activateMux(int tc_n);
        void disableMux(void);
        float linearizeTC(float tc_temp, float int_temp);

        // Attributes
        int PIN_CS;
};

#endif

Does the KTA 259 board have its own controller on ? Do you have the below pins connected to the controller MCu.

#define MUX_A0 4
#define MUX_A1 5
#define MUX_A2 6
#define MUX_EN 7

These are what actuall set what channel is read from to the single Max31856 on the KTA board, I havent used this before but you cycle through the 8 combinations to read from the 8 sensors with the A0/1/2 using BCD to set the channel

A0/1/2 = 000 ; Set to read input 1, Read with the Max31856 and store.
A0/1/2 = 100 ; Set to read input 2, Read with the Max31856 and store.

and so on.

Looking at the KTA-259 datasheet, it would appear that it is an Uno form factor shield.

Hence the connections to A0, A1, A2 and EN should be correct.

I think I'm using some of those pins to read in data from the Max31856 boards that are reading thermocouples separate from the KTA 259. The KTA 259 is attached directly to an Arduino Uno, the KTA 259 is directly on top on the Uno and is attached with pins that go into all of the Arduino's digital and analog input pins.

Noted, Thought to just ask as those pins set the channel the mux reads from and I wasnt sure what controller board you had used.

Have you tried just reading from the KTA259 with this ?

GitHub - mpetitjean/TCMuxShield_lib: An Arduino library for the TCMuxShield (KTA-259) by Ocean Controls that also implements NIST thermocouple linearization

#include "TCMuxShield.h"

TCMuxShield tc = TCMuxShield(9);

// the setup function runs once when you press reset or power the board
void setup()
{
    // initialize Serial
    Serial.begin(9600);
    while(!Serial) {;}
}

// the loop function runs over and over again forever
void loop()
{
    // Measure all thermocouple inputs, and display temperatures and error codes over serial
    for (char i = 1; i < 9; ++i)
    {
        int err_code = tc.readTemperature(i);
        Serial.print("-------------------\nThermocouple ");
        Serial.println(i, DEC);
        Serial.print("Temperature = ");
        Serial.println(tc.temperature);
        Serial.print("Error Code = ");
        Serial.println(err_code);
    }
    
    delay(5000);
}

This code works for my set up, but my main issue is that I want to read in two additional thermocouples on separate Max31856 boards. Whenever I try to add that into my code, it doesn't work.

Which Arduino has it's default SPI on pin 2,3,4.
Leo..

Posting an annotated schematic will help sort out your problems. Be sure to show all connections, power sources, grounds, etc. Post links to technical information on the hardware.

This is software based SPI used within the Adafruit MAX31856 example so pins outside hardware SPI can be used.