Hello again!
I have put a capacitor on the output of the mux and the readings are much more consistent. However, it seems that the capacitor just holds the voltage to long, so that for the next read the output is too high. My readings:
Through the Mux x: 274 y: 310 z: 333 Direct analog readings x: 332 | y: 333 | z: 277
Through the Mux x: 274 y: 332 z: 333 Direct analog readings x: 332 | y: 334 | z: 276
Through the Mux x: 274 y: 306 z: 329 Direct analog readings x: 332 | y: 333 | z: 277
Through the Mux x: 277 y: 310 z: 331 Direct analog readings x: 332 | y: 333 | z: 277
Through the Mux x: 274 y: 305 z: 331 Direct analog readings x: 333 | y: 335 | z: 277
Through the Mux x: 274 y: 314 z: 325 Direct analog readings x: 332 | y: 332 | z: 276
Through the Mux x: 310 y: 310 z: 331 Direct analog readings x: 332 | y: 334 | z: 277
It seems that the readings on the channel 0 are actually from the z pin on the sensor. when i position the sensor vertically the readings become:
Through the Mux x: 349 y: 295 z: 332 Direct analog readings x: 264 | y: 296 | z: 349
Through the Mux x: 350 y: 268 z: 334 Direct analog readings x: 266 | y: 334 | z: 350
Through the Mux x: 352 y: 271 z: 336 Direct analog readings x: 266 | y: 334 | z: 350
the readings from x pin on the sensor are now visible on the y channel (channel 1 in the code) of the multiplexer. How is this delay introduced.
I have drawn the schematics, see the attachment. When I keep the accelerometer flat the readings on from the analog pins are correct. I have used a delay of even 1000 ms to let the capacitor discharge but no luck. I also have connected GND to channel 15 on the mux and have this channel read in order to "clear" the output pin. But all output became 0.
About crosstalking: if read only one channel in the software but leave all the wires in place then the readings are correct, even if delays are set to 1ms.
What else can i do to prevent and test?
The datasheet of the mux is found here http://datasheet.octopart.com/CD74HC4067E-Texas-Instruments-datasheet-151719.pdf
and my code:
//to hold direct read from the analog output of the ADXL335
int xAnaRead;
int yAnaRead;
int zAnaRead;
//to hold readings from the mux:
int xMuxRead;
int yMuxRead;
int zMuxRead;
//mux pins
int s0 = 8;
int s1 = 9;
int s2 = 10;
int s3 = 11;
//The pin on which the Mux outputs
int SIG_pin = A0;
//Analog read pins
const int xPin = A8;
const int yPin = A9;
const int zPin = A10;
void setup(){
Serial.begin(9600);
}
void loop(){
//read value on channel 0 of Mux
xMuxRead = readMux(0);
//read analog value
int xAnaRead = analogRead(xPin);
delay(100); //to let the capacitator discharge
//read value on channel 1 of Mux
yMuxRead = readMux(1);
//read analog value
int yAnaRead = analogRead(yPin);
delay(100); //to let the capacitator discharge
//read value on channel 2 of Mux
zMuxRead = readMux(2);
//read analog value
int zAnaRead = analogRead(zPin);
delay(100); //to let the capacitator discharge
//Output the readings
Serial.print("Through the Mux x: ");
Serial.print(xMuxRead);
Serial.print("\t y: ");
Serial.print(yMuxRead);
Serial.print("\t z: ");
Serial.print(zMuxRead);
Serial.print("\t\t Direct analog readings x: ");
Serial.print(xAnaRead);
Serial.print(" | y: ");
Serial.print(yAnaRead);
Serial.print(" | z: ");
Serial.print(zAnaRead);
Serial.println("");
delay(100);//just here to slow down the serial output - Easier to read
}
//this is verbose but it works, and is more readable (i need that :)
int readMux(int channel){
int controlPin[] = {
s0, s1, s2, s3 };
int muxChannel[16][4]={
{
0,0,0,0 }
, //channel 0
{
1,0,0,0 }
, //channel 1
{
0,1,0,0 }
, //channel 2
{
1,1,0,0 }
, //channel 3
{
0,0,1,0 }
, //channel 4
{
1,0,1,0 }
, //channel 5
{
0,1,1,0 }
, //channel 6
{
1,1,1,0 }
, //channel 7
{
0,0,0,1 }
, //channel 8
{
1,0,0,1 }
, //channel 9
{
0,1,0,1 }
, //channel 10
{
1,1,0,1 }
, //channel 11
{
0,0,1,1 }
, //channel 12
{
1,0,1,1 }
, //channel 13
{
0,1,1,1 }
, //channel 14
{
1,1,1,1 } //channel 15
};
//loop through the 4 sig
for(int i = 0; i < 4; i ++){
digitalWrite(controlPin[i], muxChannel[channel][i]);
}
//read the value at the SIG pin
int val = analogRead(SIG_pin);
//return the value
return val;
}