Multiplexed thermocouples (AD595 amplifier + 2 x MC14051 mux) problem switching

Hi, I am building a multiplexed thermocouple for a reprap. I am using an uno to prototype it.

I have the following set up:

2 x MC14051 mux
1 x AD595 amplifier

On a separate circuit I have a MCP23017 connected to an LCD (the problem is not here).

At the moment I have the thermocouple inputs split between two 4051 mux chips. I can switch between channels by specifying the pin addresses in the code. However, when I try to switch channels during the main loop. The readings on the first channel change from a real temperature to something like "0.49" which indicates to me a loss of input into the AD595. However, the second channel is displaying a real temperature to my LCD reads "0.49" first line, "25.00" second line.

Here is the code below:

#include <Wire.h>
#include <AD595.h>
#include <LiquidTWI2.h>
// Connect via i2c, default address (0)
LiquidTWI2 lcd (0);
AD595 thermocouple;

void setup () {
  
  //set up thermocouple
  
  
  //set up digital pins
  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  
  
  
  //set up serial feed
   Serial.begin(9600);
   // Set the kind of integrated use
   // (Remove the quotes from the one used)
//lcd.setMCPType(LTI_TYPE_MCP23008);
   lcd.setMCPType (LTI_TYPE_MCP23017);
   // Sets the type of LCD (columns, rows)
   lcd.begin (16, 2);
   // Write a message on the LCD.
   //lcd.print ("Palaeopi.org.uk");
   
   //lcd.print (".gb");
   delay(500);
}
void loop () {
  
  
  //set up channel 0 on both 4051s
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
 
  delay(5000);
 
  thermocouple.init(3);
  
  delay(500);
  
  lcd.setCursor (0,0);
  float temp1 = thermocouple.measure(TEMPC);
  Serial.print("T1: ");
  Serial.println(temp1);
  lcd.print(temp1);
  
  //set up channel 0 on both 4051s
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, HIGH);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, HIGH);
  
  delay(5000);
  
  thermocouple.init(3);
  
  delay(500);
  
  lcd.setCursor (0,1);
  float temp2 = thermocouple.measure(TEMPC);
  Serial.print("T2: ");
  Serial.println(temp2);
  lcd.print(temp2);
  
}

Any clue what is going on here? Why if I deactivate the second channel by commenting out does the first channel begin reading real temperatures again?

Here is the fritzing layout:

Hi, I've heard of, and seen myself, similar things before. Switching the input source for an analog input seems to corrupt the next reading. Try discarding the first reading after you switch the mux address and take the second reading. Alternatively insert a small delay between switching the mux and taking the reading.

Paul

Inserting delays does nothing. The below code highlights the problem:

#include <Wire.h>
#include <AD595.h>
#include <LiquidTWI2.h>
// Connect via i2c, default address (0)
LiquidTWI2 lcd (0);
AD595 thermocouple;

void setup () {
  
  //set up thermocouple
  thermocouple.init(3);
  
  //set up digital pins
  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  
   //set up serial feed
   Serial.begin(9600);
   
   // Set the kind of integrated use
   // (Remove the quotes from the one used)
   //lcd.setMCPType(LTI_TYPE_MCP23008);
   lcd.setMCPType (LTI_TYPE_MCP23017);
   
   // Sets the type of LCD (columns, rows)
   lcd.begin (16, 2);
   delay(500);
}
void loop () {
  
  //set up channel 0 on both 4051s
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  delay(1000);
  
  lcd.setCursor (0,0);
  
  float temp1;
  
  for (int i = 0; i < 10; i++) 
  {
     float temp1 = thermocouple.measure(TEMPC);
     Serial.println(temp1);
     Serial.println(analogRead(3));
     if (i == 9)
     {
        lcd.print(temp1); 
     }
  }

  //set up channel 1 on both 4051s

  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, HIGH);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, HIGH);
  delay(1000);

}

Rewrite digital pins during loop and LCD reads "0.49" from analog input 3. Comment out digital pin rewriting and a temperature is displayed. When analogRead is implemented changes from "55" to "0" or "1".

Could there be a grounding problem? I have everything on the same ground circuit.

Update. If I implement delays and analogRead() calls through a loop. Once the loop gets to the 8th reading it starts reading real temperatures again with a digital rewrite in the loop. Code below.

#include <Wire.h>
#include <AD595.h>
#include <LiquidTWI2.h>
// Connect via i2c, default address (0)
LiquidTWI2 lcd (0);
AD595 thermocouple;

void setup () {
  
  //set up thermocouple
  thermocouple.init(3);
  
  //set up digital pins
  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  
   //set up serial feed
   Serial.begin(9600);
   
   // Set the kind of integrated use
   // (Remove the quotes from the one used)
   //lcd.setMCPType(LTI_TYPE_MCP23008);
   lcd.setMCPType (LTI_TYPE_MCP23017);
   
   // Sets the type of LCD (columns, rows)
   lcd.begin (16, 2);
   delay(500);
}
void loop () {
  
  //set up channel 0 on both 4051s
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  delay(1000);
  
  lcd.setCursor (0,0);
 for (int i = 0; i < 9; i++)
 {
     analogRead(3);
     delay(500);
     analogRead(3);
     delay(500);
     float temp1 = thermocouple.measure(TEMPC);
     Serial.print(i);
     Serial.print(' ');
     Serial.print(temp1);
     Serial.print(' ' );
     Serial.print(analogRead(3));
     Serial.println();
    if(i ==  8)
    {
       lcd.print(temp1); 
    }
  }

  //set up channel 1 on both 4051s

  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, HIGH);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, HIGH);
  delay(1000);

}

How would I write a piece of code to check if the reading is a "real" temperature or not? Basically get the channel read until the real temperatures appear again, then switch, do the same and go on to the next iteration.

So its something like 8 seconds and 16 analogReads before the signal from ther thermocouple/amplifier has settled down? That's pretty bad. I think it must be some impedance problem between the thermocouple and the amplifier, introduced by the mux chip. Have you googled for similar circuits? Perhaps someone like Grumpy_Mike can advise, but he will want to see a proper schematic. You can do that in the schematic view in Fritzing.

You should have decoupling caps on those chips. 0.1uF close to the power pins of each chip, and perhaps 10-47uF on the breadboard power rails wouldn't hurt.

Here is the schematic: -

Thanks for your help. I though 8 seconds was a bit too long...

That diagram is a bit too small to see. However when using multiplexes all unused channels must be terminated. The simplest way to do that is to connect them to ground. Otherwise the unused channels pick up stray signals and dump charge onto other channels, this is called cross talk.

You should have decoupling caps on those chips.

You should. It is a waste of time doing anything else until you do.

http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html

Hi,

Thanks for the suggestions. I'll add some decoupling caps on the ICs, and some grounds to the excess terminals and see how it works out. Thanks for being patient with a complete noob.

Regards,

Rick

Hi Guys,

I grounded the unused mux pins and added a 10uF to main power and 0.1uF near to each IC and performance is actually worse.

I've made the schematic into a PDF, perhaps you'll be able to see better. This is pre-modification still.

https://dl.dropboxusercontent.com/u/3806714/Untitled%20Sketch%202_schem.pdf

All the best,

Rick

I grounded the unused mux pins and added a 10uF to main power and 0.1uF near to each IC and performance is actually worse.

I can't see that on the schematic.

If adding decoupling made it worst then you have something else seriously wrong with your wiring that you are not telling us about.

Grumpy_Mike:
I can't see that on the schematic.

PalaeoPi:
This is pre-modification still.

I think I will try this again with 1 multiplexer instead of two. See if I get the same effect. I have decoupling caps on the power rails of all the ics. I'll also try simplify the circuit to just what's needed to get the readings e.g. 2 x 4051 and 1 x AD595 and remove the LCD.

I have thermocouple wires wired between the two mux's, could that cause problems I.e. one wire on channel 0 of one mux and one wire on channel 0 of one mux and second thermocouple bridging both channel 1s of the mux's. Make sense?