Uno R3: Button on Pin 3 causing Pin 2 to appear "pressed"?

Absolute newbie to Arduino’s and hardware programming in general, but my Google-fu failed me and I couldn’t find anything about this issue I’m running into so here goes:

I’m trying to wire something up that has 4 buttons that increment/decrement an integer value. Tight now I’ve only wired up one button on my breadboard to test with and I’m starting to see some strange behavior. I tested the button setup on every pin.

  • Pins 0 and 1 weren’t working but from the board (and the product page for the Unos) these pins seems to be reserved for transmitting and receiving TTL data so that’s somewhat expected, even if I don’t fully understand (yet) what that means.
  • Pins 2, 4, 5, 8, 12 worked as expected
  • Pins 3, 6, 7, 9, 10, 11, 13: these are strange ones - for example with pin 3 even when the code is not “watching” this pin when I have the button plugged into it pin 2 seems to be detecting “changes”. Is this pin linked to pin 2? I couldn’t find any special case documentation and looking at the pin mappings (http://arduino.cc/en/Hacking/PinMapping168) nothing really called out that these are special/reserved pins.

I have the NO button wired on a breadboard with pin 1 of the button connected to the 5V and the other to ground (via a 20k resistor… arbitrary resistor that was just lying around) as well as a jumper that I move between the digital inputs.

The code I’m using to test the button states is as follows:

#include <Event.h>

int buttonPins[] = {2};
const int numButtons = sizeof(buttonPins) / sizeof(int);
int oldButtonStates[numButtons];

void setup()
{
  Serial.begin(9600);

  for (int i = 0; i < numButtons; i++)
  {
    pinMode(buttonPins[i], INPUT);
    oldButtonStates[i] = digitalRead(buttonPins[i]); // Detect initial state so we don't "miss" the first click
  }
}

void loop()
{
  for (int i = 0; i < numButtons; i++)
  {
    processButton(i);
  }
}

void processButton(int buttonIndex)
{
  int newButtonState = digitalRead(buttonPins[buttonIndex]);
  
  if (newButtonState != oldButtonStates[buttonIndex]) // Edge detection
  {
    Serial.print("Button ");
    Serial.print(buttonIndex);
    Serial.print(" on pin: ");
    Serial.print(buttonPins[buttonIndex]);
    Serial.print(" state: ");
    Serial.print(digitalRead(buttonPins[buttonIndex]));
    Serial.print(" was: ");
    Serial.println(oldButtonStates[buttonIndex]);
    
    oldButtonStates[buttonIndex] = newButtonState;
  }
}

The output of the code when the button is connected to pin 3 and pressed (briefly) and nothing is connected to any of the other digital pins:

Button 0 on pin: 2 state: 0 was: 0
Button 0 on pin: 2 state: 1 was: 1
Button 0 on pin: 2 state: 0 was: 0
Button 0 on pin: 2 state: 0 was: 1
Button 0 on pin: 2 state: 0 was: 0
Button 0 on pin: 2 state: 0 was: 1

At first I thought it might be related to the PWM pins but with some of the PWM pins working as expected I am thoroughly baffled.

Am I missing something glaringly obvious?

Am I missing something glaringly obvious?

Yes. If you have nothing connected to a pin that is defined as an input, it floats and returns values picked up by noise.

See this:- http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

Am I missing something glaringly obvious?

It would be far simpler to connect one side of the switch to ground, and the other side to the digital pin, and turn on the pullup resistor for the pin (pinMode(somePin, INPUT); digitalWrite(somePin, HIGH);).

"Many beginners think that if you connect nothing to an input it will read as a low, this is not true. It will certainly read something but without any connection, the input is said to be floating. In practice this usually results in reading a succession of highs and lows, seemingly at random. In fact what is happening is that the input is acting as an antenna and picking up lots of signals out of the air. This is mainly mains interference but can be any other signals. Also if you touch the pin then the situation gets worse and this often startles and amazes a beginner but this is perfectly normal behaviour."

Well I certainly qualify as a beginner though "amazed" isn't the word I'd have used.

I'll try hooking up a few other buttons and see if this solves it but I'm fairly certain it will.

Thanks!

though "amazed" isn't the word I'd have used.

You would if you have seen some of the posts I have seen. Some people seem to thing this is a magical thing.

It makes perfect sense now that I know that but I never would have guessed it worked like that before reading that article. Now that I know that I'm seeing similar comments in a lot of posts/websites though so maybe I'm just dense and missed it before :)

Thanks again!

Electronic Ouija Board… Anyone?

Bob