SparkFun Forums 

Where electronics enthusiasts find answers.

Have questions about a SparkFun product or board? This is the place to be.
By badgerengineer93
#197970
Hi all,

I purchased the Mayhew Labs Mux Shield II (https://www.sparkfun.com/products/11723) and am having a weird issue with it. I have it connected to an Elegoo Mega2560 R3 (Arduino Mega) and am testing it with 16 analog inputs as a proof of concept so I can eventually use all 48 analog inputs.

I have 16 photoresistors, each of which are in series with a 4.7k ohm resistor. I have each pin in the I/O1 row of the Mux Shield reading the voltage across a 4.7k ohm resistor. My code (below) takes 10 readings from each pin and averages them to create a baseline value. Then, in the loop function, the voltage across each resistor is read and then subtracted from the baseline for that specific pin. The program outputs the difference from the baseline for all 16 pins.

The code works perfectly for about 45 seconds. The pin voltages hover around the baseline (some noise exists, causing them to fluctuate slightly). The voltages will also increase or decrease when I cover the sensors or shine a light on them. However, after about 45 seconds, the voltage readings get stuck at a certain value and no longer increase or decrease due to light input. The only solution I've found is to completely remove the USB cable from my laptop, then reconnect it and reupload the code, which causes it to work for another 45 seconds or so.

To ensure it is the multiplexer and not my code, I wired one photoresistor + resistor to analog pin A15. This pin hovers around its baseline and consistently responds to light input no matter how long the code has been running.

Does anyone have any idea why this is occurring?

Code:
Code: Select all
#include <MuxShield.h>

//Initialize the Mux Shield
MuxShield muxShield;

int baseline1[16];
int baseline2[16];
int baseline3[16];
int baseline15;

//Arrays to store analog values
int IO1AnalogVals[16];
int IO2AnalogVals[16];
int IO3AnalogVals[16];

void setup()
{
    //Set I/O 1, I/O 2, and I/O 3 as analog inputs
    muxShield.setMode(1,ANALOG_IN);
    muxShield.setMode(2,ANALOG_IN);
    muxShield.setMode(3,ANALOG_IN);

    for (int i=0; i<16; i++) {

      int temp1 = 0;
      int temp2 = 0;
      int temp3 = 0;
      int temp15 = 0;

      for(int j = 0; j < 10; j++) {

        //Analog read on all 16 inputs on IO1, IO2, and IO3
        temp1 += muxShield.analogReadMS(1,i);
        temp2 += muxShield.analogReadMS(2,i);
        temp3 += muxShield.analogReadMS(3,i);
        temp15 += analogRead(15);
        
      }

      temp1 /= 10;
      temp2 /= 10;
      temp3 /= 10;
      temp15 /= 10;

      baseline1[i] = temp1;
      baseline2[i] = temp2;
      baseline3[i] = temp3;
      baseline15 = temp15;
      
    }
    
    Serial.begin(9600);
    
}

void loop()
{
  for (int i=0; i<16; i++)
  {
    //Analog read on all 16 inputs on IO1, IO2, and IO3
    int raw1 = muxShield.analogReadMS(1,i);
    int raw2 = muxShield.analogReadMS(2,i);
    int raw3 = muxShield.analogReadMS(3,i);

    IO1AnalogVals[i] = raw1 - baseline1[i];
    IO2AnalogVals[i] = raw2 - baseline2[i];
    IO3AnalogVals[i] = raw3 - baseline3[i];
    
  }

  int a15 = analogRead(15) - baseline15;

 /*for (int i=0; i<16; i++)
  {
    Serial.print(IO1AnalogVals[i]);
    Serial.print("\t");
  }

  Serial.println("");*/

  /*//Print IO2 values for inspection
  for (int i=0; i<16; i++)
  {
    Serial.print(IO2AnalogVals[i]);
    Serial.print("\t");
  }

  Serial.println("");*/

  for (int i=0; i<16; i++)
  {
    Serial.print(IO1AnalogVals[i]);
    Serial.print("\t");
  }
  
  Serial.println(a15);
  Serial.println("----------------------------------------------------------------------------------------");
  
  delay (1000);
  
}
By paulvha
#197982
There is really nothing special / difficult about the board and the driver. The Mux on the board is connecting one of the 16 pins of an IO1, IO2 or IO3 to one of the analog pin A0, A1 or A2. That is it. The real analog measurement is done by the Arduino. The driver is only setting the address output pins to high / low to select the right channel on the mux and perform an Analogread on either A0, A1 or A2. Nothing fancy. Also you sketch has is straight forward, nearly the same as the MuxShieldAnalogIn.ino.

As your results get stalled, while still being able to read A15 for test, I suspect the Mux is somehow confused... a hardware issue: maybe cabling, maybe timing, maybe heat, maybe power supply..

some thoughts for debugging:
0. Make sure the cables are well connected and you power supply is strong enough
1. Do I assume correctly that the other end of your photo resistor is connected to VCC ?
2. Do I understand correctly that your A15 continues to respond while the Mux board results do not change anymore?
3. What happens if you run the example : MuxShieldAnalogIn.ino. Will that stop as well as after some time ?
Check that the Arduindo is still looping (e.g. with Serial.println()) so the issue must be with the board (I would expect so...)
What happens if you close the serial window and re-open it again (instead of unplug the USB ?)
what happens if you press reset on the Arduino instead of unplug.
If the Mux board does stop all the time, you could start to include some short delays in between reading from the different IOx and channels,
or within the driver needs some delays.
4. connect the photoresistor of one of the channels ALSO (at the same time) to A15 to check that it still changes (I would expect so.. but rule that option out completely)
By badgerengineer93
#198013
Thanks for your reply. Here are my responses to your thoughts:
0. Make sure the cables are well connected and you power supply is strong enough
1. Do I assume correctly that the other end of your photo resistor is connected to VCC ?
2. Do I understand correctly that your A15 continues to respond while the Mux board results do not change anymore?
3. What happens if you run the example : MuxShieldAnalogIn.ino. Will that stop as well as after some time ?
Check that the Arduindo is still looping (e.g. with Serial.println()) so the issue must be with the board (I would expect so...)
What happens if you close the serial window and re-open it again (instead of unplug the USB ?)
what happens if you press reset on the Arduino instead of unplug.
If the Mux board does stop all the time, you could start to include some short delays in between reading from the different IOx and channels,
or within the driver needs some delays.
4. connect the photoresistor of one of the channels ALSO (at the same time) to A15 to check that it still changes (I would expect so.. but rule that option out completely)
0. I believe all the cables are well-connected; however, I did solder female headers to the board myself and I could have messed something up there considering I haven't soldered in several years. The power supply shouldn't be an issue; I tested using the Arduino 5V output as well as an external power supply and saw the same behavior. I calculated the current draw to be only ~0.0085A under normal light conditions.

1. Yes, one end is connected to VCC and the other is connected to its 4.7k ohm resistor. The sensor reading is taken across the 4.7k ohm resistor.

2. Yes, the reading for A15 consistently functions as expected.

3. Yes, running the library example shows the same behavior (the readings will get stuck around a certain value from 0 to 1023. They will sometimes still respond to input but will only change +/- 20 or so, compared to when the program first starts and I can get them to fluctuate +/- 200 by covering some of the sensors.

The Arduino continues to output data to Serial, so it isn't that the loop stopped. It will just keep outputting the same or very similar values even when I cover and uncover the sensor.

Closing and reopening the Serial monitor has no effect; it just continues outputting the frozen values.

Resetting the Arduino using the reset button doesn't fix the issue either; sometimes the bit value that each sensor is stuck on will change, but it will still not respond to light input.

4. This one was very interesting. When I have one sensor connected to both A15 AND a multiplexer input, the outputs are the exact same and will eventually get stuck at a constant value. However, if I disconnect the wire connecting that sensor to the multiplexer, the A15 variable will start outputting correctly and functioning normally.

I'm not really sure what conclusion to come to from this; I'm considering just ordering another Mux Shield II and maybe seeing if I got a dud or I did something wrong when soldering it.
By paulvha
#198020
weird... but point 4 is very interesting. I seems the value is fixed by the MUX board. I looked again to the datasheet, schematic and driver. Nothing that pops up. I assume you have not modified the solder jumpers on the board.

Some thinking for debugging.
1. What is the value you get?
2. Are the values you get when stalled the same for each channel ?
3. Could it be that somehow the shift-register was triggered (somehow) and is set output a high value. You ADC should read nearly 1024 in that case.
4. Try to use IO2 (and/or IO3), maybe there is a bad chip on IO1.
5. Instead of looping through the 16 channels every time, try reading one 1 channels input in the loop and see what happens.
By badgerengineer93
#198474
Sorry for the late reply. Here's some more information on the values I'm getting:
mux troubleshooting.png
The left column is attached to I/O1, and the rightmost column is attached to pin A15 of the Arduino Mega2560. The middle columns are all attached to different I/O3 pins on the Mux Shield.

Above the orange line, all of the sensors are hovering around their baseline. +/- 10 is acceptable noise to me. However, beneath the orange line the values start to go crazy and eventually won't change in response to light.

I swapped out one of the photoresistors for a potentiometer to make sure it wasn't my cheap photoresistors causing the issue, but I saw the same exact issue after about 45 seconds to 1 minute of runtime.

The value never reads completely high (1023).

I also tried only reading the I/O3 channel instead of all three but I have the same issue. I'm getting pretty desperate at this point.
You do not have the required permissions to view the files attached to this post.
By paulvha
#198486
I am surprised to see A15 (connected directly to the Arduino without the mux-shield) also start to show larger differences. Although not as much as the mux-shield readings.. but still, there might be a heat problem ? What are the absolute ADC values you read as baseline and raw ? can make a loop to create a picture which shows those values instead of delta's ? Do you have a voltmeter and can you measure the A15 voltage in the beginning and when the differences is are increasing (e.g. after 45 min? ) ?