Simple Button Circuit Not Behaving Correctly

I am trying to test an Arduino circuit which uses a button, I believe I have the circuit
setup correctly except the button ignores when it has been pressed, however when I wiggle
pin 2's wire - the wire which should be connecting the Arduino & button, it acts as if the
button has been pressed or not depending on how it is wiggled.

When the button state is equal to high, an LED should light up and a string should
be printed to the serial monitor, this part behaves as expected but is dependent on the
wiggling of pin 2's wire

My only theory as to why this circuit is not working correctly is due to (a) faulty wire/s, I
have ensured no wires are loose & even used different wires. Please help.

Circuit Diagram & Sketch:

Please tell me anything else I should provide. Thanks.

I have since attached 2 pictures of the circuit (forgive the photography) & the sketch. Again, thank-you.

Feel free to request a different picture of the circuit however I couldn't post both due to 'entity is too large error'.

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    Serial.println("ON THIS IS ON");
    // delay(4000);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);


As mentioned.
Always show us your current compete sketch. Use CTRL T to format the sketch. Please use code tags. Use the </> icon in the posting menu. [code] Paste sketch here. [/code]

Show/attach a good schematic of your circuit. Show us a good image of your wiring.


however when I wiggle
pin 2's wire - the wire which should be connecting the Arduino & button, it acts as if the
button has been pressed or not depending on how it is wiggled.

That sounds like you have two problems:-

  1. You have not wired it up like you think you have, as a hint always wire across diagonal connectors in a four pin tack switch.
  2. Your incorrect wiring is faulty as it is not making firm contact. This is what happens when you use poor quality breadboards or components that are too big or too small for the breadboard.

But as the others have said we need to see your wiring and your code to eliminate the mistakes you can make with those.

You could use even a cheap meter or continuity tester to test your wiring. Being sure beats guessing any day.

Once you have this you have only begun to explore contact buttons. They have a behavior when opening and closing that is too short/fast for you to see with that sketch, sparks make the switch ON/OFF many times for a couple-few milliseconds that other sketches can trip over. The Debounce example which uses the same wiring as the Button sketch addresses the matter.

There is an easier way to wire buttons without the wire and resistor to ground and not using the 5V pin. When the pin reading the button is pinMode(pin,INPUT_PULLUP) instead of pinMode(pin,INPUT), the chip powers the pin with weak 5V through a 20K to 50K resistor. Pin not connected to anything reads HIGH. If you ground the pin (say through a pressed button) it will read LOW, opposite from INPUT mode supplied with 5V on the press. There is no need for a pulldown (that resistor and wire going to ground) and the pin set INPUT_PULLUP can be grounded safely since the chip-supplied current is so weak. I use a jumper to test my button sketches, I ground it on the grounded metal Arduino board USB port connector box. That bounces too.

You can fill a wire connected to a pin moded as INPUT with current until it reads HIGH. If you just have a wire from 5V to button and button to pin, a short press and release of the button will fill the wire from button to pin with charge so the pin reads HIGH. When you read the pin, one single microamp of charge gets removed from the wire. Read enough times without pressing the button and the pin will eventually read LOW. The Button example circuit has that wire from pin 2 to ground through the 10K resistor just to drain the wire of charge when the button is not pressed. That is what's happening with that circuit.