Hi. I am doing a project to measure array capacitors. I have a working capacitance measuring circuit. I am using two 4051 multiplexer chip, one for charging the capacitor and the other for taking in measurement reading. If I select one pin of the 4051 to charge the capacitor and the other pin of the other 4051 to take in measurement reading, it works i.e. I am able to measure one capacitor. However, when I measure capacitors in array on 8 channels, the measurement becomes wrong value. It seems that the value, let say on channel one, some of it will "leak" into the second channel, giving wrong result. I am not sure what is the reason.
const int pin_Out_S0 = 8;
const int pin_Out_S1 = 9;
const int pin_Out_S2 = 10;
const int pin_Out_S00 = 5;
const int pin_Out_S11= 6;
const int pin_Out_S22 = 7;
const int pin_In_Mux1 = A0;
const int outputPinMux = A2;
const int numReadings = 10;
const int numChannel = 8;
const float strayCap = 27.7*2.8; //initially this was 30.00
const int maxADC = 1023;
float capacitance[numChannel] = {0.0};
int readings[numChannel][numReadings];
int readIndex = 0;
int channelIndex = 0;
int total[numChannel] = {0};
int average[numChannel] = {0};
void setup() {
pinMode(pin_Out_S0, OUTPUT);
pinMode(pin_Out_S1, OUTPUT);
pinMode(pin_Out_S2, OUTPUT);
pinMode(pin_Out_S00, OUTPUT);
pinMode(pin_Out_S11, OUTPUT);
pinMode(pin_Out_S22, OUTPUT);
pinMode(outputPinMux, OUTPUT);
pinMode(pin_In_Mux1, OUTPUT);
Serial.begin(9600);
for(int thisChannel = 0; thisChannel< numChannel; thisChannel++){
for(int thisReading = 0; thisReading < numReadings; thisReading++){
readings[thisChannel][thisReading] = 0;
}
}
}
void loop() {
do{
total[channelIndex] = total[channelIndex] - readings[channelIndex][readIndex];
pinMode(pin_In_Mux1, INPUT);
digitalWrite(outputPinMux, HIGH);
updateMux(channelIndex);
updateMux1(channelIndex);
readings[channelIndex][readIndex] = analogRead(pin_In_Mux1);
total[channelIndex] = total[channelIndex] + readings[channelIndex][readIndex];
//Clear everything for next measurement
digitalWrite(outputPinMux, LOW);
pinMode(pin_In_Mux1, OUTPUT);
//Calculate and print result
average[channelIndex] = total[channelIndex] / numReadings;
capacitance[channelIndex] = (float)average[channelIndex] * strayCap / (float)(maxADC - average[channelIndex]);
Serial.print(channelIndex);
Serial.print(": ");
Serial.print(capacitance[channelIndex],2);
Serial.print(F(" pF ("));
Serial.print(average[channelIndex]);
Serial.print(F(") "));
channelIndex = channelIndex + 1;
}while(channelIndex<numChannel);
channelIndex = 0;
readIndex = readIndex + 1;
if(readIndex >= numReadings){
readIndex = 0;
}
Serial.println("");
}
void updateMux(int i){
digitalWrite(pin_Out_S00, HIGH && (i & B00000001));
digitalWrite(pin_Out_S11, HIGH && (i & B00000010));
digitalWrite(pin_Out_S22, HIGH && (i & B00000100));
}
void updateMux1 (int i) {
digitalWrite(pin_Out_S0, HIGH && (i & B00000001));
digitalWrite(pin_Out_S1, HIGH && (i & B00000010));
digitalWrite(pin_Out_S2, HIGH && (i & B00000100));
}

