user11: I have a push button wired to my Arduino but it seems to be triggering randomly.
I have one pin of the button connected to pin 2 on the arduino and the other connected to ground.
void setup() {
    Serial.begin(9600);

    pinMode(2, INPUT);
}

void loop() {
    Serial.println(digitalRead(2));
}

I expect it to print 1 continuously and go to 0 when I press the button down but sometimes it shows 0 even when I don't touch it.

user11: When a button is connected in that configuration, the input is what's called floating, meaning it's not a 0 or a 1. When the button is pressed, it is connected to ground, so that's definitely a 0, but when it's not pressed down, we don't know the value of the pin.
Pull Up Resistor
We need to include what's called a "pull-up" resistor to pull the signal up to a logic 1 when the button is not pressed.

Image from Sparkfun
What this means is when the button is not pressed, the Arduino reads a logic 1. When the button is pressed, the current flows through the resistor to ground and the Arduino reads a logic 0.
Internal Pull Up Resistor
The Arduino also has internal pull up resistors so you don't necessarily have to add an extra component to your circuit. There are a couple of ways to use this.
You used to have to do it like this:
pinMode(pin, INPUT);           // set pin to input
digitalWrite(pin, HIGH);       // turn on pullup resistors

Now we can do it simply in one line:
pinMode(pin, INPUT_PULLUP);

This enables the 20k pull up resistor on that pin. The input will no longer be floating when the button is not pressed.
Note: This only works when the other end is connected to ground.

11: <p>I have a push button wired to my Arduino but it seems to be triggering randomly.</p>

<p>I have one pin of the button connected to pin 2 on the arduino and the other connected to ground.</p>

<pre><code>void setup() {
    Serial.begin(9600);

    pinMode(2, INPUT);
}

void loop() {
    Serial.println(digitalRead(2));
}
</code></pre>

<p>I expect it to print 1 continuously and go to 0 when I press the button down but sometimes it shows 0 even when I don't touch it.</p>
 11: <p>When a button is connected in that configuration, the input is what's called <em>floating</em>, meaning it's not a 0 or a 1. When the button is pressed, it is connected to ground, so that's definitely a 0, but when it's not pressed down, we don't know the value of the pin.</p>

<h2>Pull Up Resistor</h2>

<p>We need to include what's called a "pull-up" resistor to pull the signal up to a logic 1 when the button is not pressed.</p>

<p><img src="https://i.stack.imgur.com/h9oX1.jpg" alt="pull up resistor"></p>

<p><a href="https://learn.sparkfun.com/tutorials/pull-up-resistors/what-is-a-pull-up-resistor" rel="noreferrer">Image from Sparkfun</a></p>

<p>What this means is when the button is not pressed, the Arduino reads a logic 1. When the button is pressed, the current flows through the resistor to ground and the Arduino reads a logic 0.</p>

<h2>Internal Pull Up Resistor</h2>

<p>The Arduino also has internal pull up resistors so you don't necessarily have to add an extra component to your circuit. There are a couple of ways to use this.</p>

<p>You used to have to do it like this:</p>

<pre><code>pinMode(pin, INPUT);           // set pin to input
digitalWrite(pin, HIGH);       // turn on pullup resistors
</code></pre>

<p>Now we can do it simply in one line:</p>

<pre><code>pinMode(pin, INPUT_PULLUP);
</code></pre>

<p>This enables the 20k pull up resistor on that pin. The input will no longer be floating when the button is not pressed.</p>

<p><strong>Note:</strong> This only works when the other end is connected to ground.</p>
