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. ![]()
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);
}
}