user42: I've been using a button to interact with a sketch (on my Uno), and it's mostly working fine. However, sometimes it seems to be detecting multiple presses when it's only pressed once.
It's a small circuit-mounted momentary button, which is normally open. It's connected from +5V directly to an input pin. I've also added a 10K pull-down resistor on the pin to make sure the input isn't left floating.
Here's a test sketch I've been using, which reports the number of presses to the serial monitor:
int oldState = LOW;
int numPresses = 0;

void setup()
{
    pinMode(5, INPUT);
    Serial.begin(9600);
}

void loop()
{
    const int newState = digitalRead(5);

    if (newState != oldState) {
        if (newState == HIGH) Serial.println(++numPresses);
        oldState = newState;
    }
}

Sometimes when I press the button once, the serial monitor shows two presses in a row. It sometimes even reports a press when I release the button.
Is it possible to fix this? Or do I just have a faulty button or pin or something?

user84: This is your button bouncing which is quite usual behavior; it has nothing to do with button quality.
There are 2 ways to "debounce" a button: by hardware or by software.
Personally, I prefer by hardware; just google for button debouncing circuit and you should find many examples.
If you don't have the necessary components available, you can use software debouncing.
By software, you have an example here on Arduino web site.

user225: The button contacts themselves are probably bouncing in and out of contact, which is causing the circuit to open and close multiple times, even though the button position isn't changing. 
It's a common problem with buttons and would be the first thing I would check. In order to fix this, you need to de-bounce the button. There are various ways in which to accomplish this, some of which can be done in the sketch itself.
A good solution is code that measures the duration of each button state and only registers a change in state if the current duration is greater than the desired duration. That option should be used for more complex projects. The code for it will be project specific and likely use sketch defined variables, but an example of what needs to be done can be found here. 
If the sketch is very basic or you just want to make sure that bounce is the problem, you can add a short delay after the button state changes. For example: 
void loop()
{
    const int newState = digitalRead(5);

    if (newState != oldState) {
        if (newState == HIGH) Serial.println(++numPresses);
        oldState = newState;
        delay(10) //Adjust the delay length to be as short as possible without triggering errors
    }
}    


user11: Contact Bounce
This is known as contact bounce and happens when the metal contacts in the button or switch bounce against each other when you push it. The microcontroller is fast enough to pick up these bounces and, as far as it's concerned, you're toggling the switch many times in as much as 10 milliseconds of time.
Different switches and buttons will have a different amount of contact bounce, and even one switch will behave differently between two presses.
Here's a button press captured on a scope (image from Maxim):

At 2ms/div we don't see a stable signal until 6ms after initial contact!
Solutions
Add a delay
The absolute simplest solution is to introduce a short delay after you read the button input. This will prevent the code from picking up any bounces and when it resumes, the signal should be stable.
if (digitalRead(A3) == 0) {
    // do something here
    delay(500);
}

In the example above, I delay 500ms after reading the state of the pin. The button is normally high so when I press it down, the state of the pin goes to logic 0. I pick that up in the code and do something and then wait at least 500ms before continuing.
Obviously, this code will just halt for half a second every time you push a button, which may or may not be acceptable in your application. You can decrease the amount of delay depending on the contact bounce time of your switch, too, but I play it safe here.
Ignore button presses too close to each other
Similar to the solution above but instead of adding a hard delay, you use the millis() function to keep track of when the last time the switch was triggered. Every time you see a switch trigger, you compare against the time and, if not enough time has elapsed (say like 500ms in my previous example), you ignore it.
This solves the problem of having to use delay() in your code.
Switch debouncer IC
There are things like the MAX6816 which is an IC that you put in between your switch and microcontroller input. It will denounce the signal for you. At a price of $1.62 @1k you'll probably never use this.
Other
There are many solutions to this problem... I always do it in software, but some others include using a latch or capacitor.
Debounce Code – one post to rule them all

42: <p>I've been using a button to interact with a sketch (on my Uno), and it's mostly working fine. However, sometimes it seems to be detecting multiple presses when it's only pressed once.</p>

<p>It's a small circuit-mounted momentary button, which is normally open. It's connected from +5V directly to an input pin. I've also added a 10K pull-down resistor on the pin to make sure the input isn't left floating.</p>

<p>Here's a test sketch I've been using, which reports the number of presses to the serial monitor:</p>

<pre><code>int oldState = LOW;
int numPresses = 0;

void setup()
{
    pinMode(5, INPUT);
    Serial.begin(9600);
}

void loop()
{
    const int newState = digitalRead(5);

    if (newState != oldState) {
        if (newState == HIGH) Serial.println(++numPresses);
        oldState = newState;
    }
}
</code></pre>

<p>Sometimes when I press the button once, the serial monitor shows two presses in a row. It sometimes even reports a press when I <em>release</em> the button.</p>

<p>Is it possible to fix this? Or do I just have a faulty button or pin or something?</p>
 84: <p>This is your button <strong>bouncing</strong> which is quite usual behavior; it has nothing to do with button quality.</p>

<p>There are 2 ways to "debounce" a button: by hardware or by software.</p>

<p>Personally, I prefer by hardware; just google for <code>button debouncing circuit</code> and you should find many examples.</p>

<p>If you don't have the necessary components available, you can use software debouncing.</p>

<p>By software, you have an example <a href="http://arduino.cc/en/Tutorial/Debounce" rel="nofollow">here</a> on Arduino web site.</p>
 225: <p>The button contacts themselves are probably bouncing in and out of contact, which is causing the circuit to open and close multiple times, even though the button position isn't changing. </p>

<p>It's a common problem with buttons and would be the first thing I would check. In order to fix this, you need to de-bounce the button. There are various ways in which to accomplish this, some of which can be done in the sketch itself.</p>

<p>A good solution is code that measures the duration of each button state and only registers a change in state if the current duration is greater than the desired duration. That option should be used for more complex projects. The code for it will be project specific and likely use sketch defined variables, but an example of what needs to be done can be found <a href="http://arduino.cc/en/Tutorial/Debounce" rel="nofollow">here</a>. </p>

<p>If the sketch is very basic or you just want to make sure that bounce is the problem, you can add a short delay after the button state changes. For example: </p>

<pre><code>void loop()
{
    const int newState = digitalRead(5);

    if (newState != oldState) {
        if (newState == HIGH) Serial.println(++numPresses);
        oldState = newState;
        delay(10) //Adjust the delay length to be as short as possible without triggering errors
    }
}    
</code></pre>
 11: <h1>Contact Bounce</h1>

<p>This is known as contact bounce and happens when the metal contacts in the button or switch bounce against each other when you push it. The microcontroller is fast enough to pick up these bounces and, as far as it's concerned, you're toggling the switch many times in as much as 10 milliseconds of time.</p>

<p>Different switches and buttons will have a different amount of contact bounce, and even one switch will behave differently between two presses.</p>

<p>Here's a button press captured on a scope (image from <a href="http://www.maximintegrated.com/app-notes/index.mvp/id/159" rel="noreferrer">Maxim</a>):</p>

<p><img src="https://i.stack.imgur.com/ZrNMs.gif" alt="enter image description here"></p>

<p>At 2ms/div we don't see a stable signal until 6ms after initial contact!</p>

<h1>Solutions</h1>

<h2>Add a delay</h2>

<p>The absolute simplest solution is to introduce a short delay after you read the button input. This will prevent the code from picking up any bounces and when it resumes, the signal should be stable.</p>

<pre><code>if (digitalRead(A3) == 0) {
    // do something here
    delay(500);
}
</code></pre>

<p>In the example above, I delay 500ms after reading the state of the pin. The button is normally high so when I press it down, the state of the pin goes to logic 0. I pick that up in the code and do something and then wait at least 500ms before continuing.</p>

<p>Obviously, this code will just halt for half a second every time you push a button, which may or may not be acceptable in your application. You can decrease the amount of delay depending on the contact bounce time of your switch, too, but I play it safe here.</p>

<h1>Ignore button presses too close to each other</h1>

<p>Similar to the solution above but instead of adding a hard delay, you use the <code>millis()</code> function to keep track of when the last time the switch was triggered. Every time you see a switch trigger, you compare against the time and, if not enough time has elapsed (say like 500ms in my previous example), you ignore it.</p>

<p>This solves the problem of having to use <code>delay()</code> in your code.</p>

<h1>Switch debouncer IC</h1>

<p>There are things like the <a href="http://www.maximintegrated.com/datasheet/index.mvp/id/1896" rel="noreferrer">MAX6816</a> which is an IC that you put in between your switch and microcontroller input. It will denounce the signal for you. At a price of $1.62 @1k you'll probably never use this.</p>

<h2>Other</h2>

<p>There are many solutions to this problem... I always do it in software, but some others include using a <a href="http://tech.thetonegod.com/debounce/debounce.html" rel="noreferrer">latch or capacitor</a>.</p>

<p><a href="http://hackaday.com/2010/11/09/debounce-code-one-post-to-rule-them-all/" rel="noreferrer">Debounce Code – one post to rule them all</a></p>
