Problem using pin 9 on the Arduino as select pin (S2) of a shift register

I’m using a Mega 2650, in standard and also pro form, and I’ve been having a rather strange problem using pin 9 on the Arduino as one of the select pins (S2) of a shift register. Essentially the pin is behaving as though it isn’t connected. So when I switch between the 8 inputs of the shift register, the lower 4 inputs are repeated.

I’ve taken things back to the simplest sample code I can find (see below). No external libraries are in use. The same code (other than changing the pin number in the last element of the selectPins array), works when I change pin 9 to something else, like 8 or 4. I’ve tested this across several different boards. I’ve also confirmed via a separate test that pin 9 is showing 5 volts when set HIGH using a multimeter.

I’ve looked online, and can’t find anything special about pin 9, other than it being one of many PWM pins. So I’m guessing the issue must be in the code somewhere? I know that this is going to be one of those situations where I’ve missed something obvious and I’m going to feel like an idiot, but I’m willing to take my medicine if there’s a chance I might learn something. :slight_smile:

Thanks in advance for your help.

const int selectPins[3] = {6, 7, 9}; 
const int zInput = A0; // Connect common (Z) to A0 (analog input)

void setup() 
{
  Serial.begin(9600); // Initialize the serial port
  // Set up the select pins as outputs:
  for (int i=0; i<3; i++)
  {
    pinMode(selectPins[i], OUTPUT);
    digitalWrite(selectPins[i], HIGH);
  }
  pinMode(zInput, INPUT); // Set up Z as an input

  // Print the header:
  Serial.println("Y0\tY1\tY2\tY3\tY4\tY5\tY6\tY7");
  Serial.println("---\t---\t---\t---\t---\t---\t---\t---");
}

void loop() 
{
  // Loop through all eight pins.
  for (byte pin=0; pin<=7; pin++)
  {
    selectMuxPin(pin); // Select one at a time
    delay(100);   
    int inputValue = analogRead(zInput); // and read Z
    Serial.print(String(inputValue) + "\t");
  }
  Serial.println();
  delay(100);  
}

void selectMuxPin(byte pin)
{
  for (int i=0; i<8; i++)
  {
    if (pin & (1<<i))
      digitalWrite(selectPins[i], HIGH);
    else
      digitalWrite(selectPins[i], LOW);
  }
}

Please explain what the selectMuxPin() function does ?

What happens when the value of i is outside the bounds of the selectPins array ?

Quite right. :slightly_smiling_face: What would happen is that I’d be trying to do a digitalwrite to an undefined address, which would have undefined results.

Can’t believe I missed this. Will change the 8 to a 3 and I’m sure all will be well again. I’ll now put on a dunce cap and stand in the corner for the next two hours!

:roll_eyes:

Let us know how you get on when you fix that problem

Yes, it works perfectly now.

I must have introduced this bugs weeks ago, but randomly it happened to be working with the default pins used by the sample code (2,3,4). This was part of a much larger project, which has been exhibiting strange behaviour and which I’ve been scouring for off by one array bugs and suchlike. So this could well be the reason.

Can’t believe this was right in front of me, but many thanks for pointing it out! :+1:

As another observation, if I’d have got rid of the magic numbers, it would have forced me to think about what that loop was really doing and highlighted my mistake. A lesson that I’ll keep in mind for the future.

1 Like

From experience both with my own code and code posted here, when arrays are involved it is always worth checking that they are not being accessed with an index that is out of bounds

Good luck with your project going forward

1 Like

A more fundamental mistake hasn't been mentioned. You are not using a shift register. You are using a demultiplexer. An example of a demultiplexer would be 74hc138. An example of a shift register would be 74hc595.

1 Like

Quite right, my apologies.
I’m using a Texas Instruments CD74HC4051E Multiplexor/Demultiplexer. For some reason I thought a multiplexor was a type of shift register but further investigation reveals that, as you point out, these are entirely different things.

Thank you all for your time. Feeling pretty foolish, but I’ve learnt a lot from your feedback. :slightly_smiling_face:

No need to feel foolish. These concepts are confusing at first.

1 Like