Example Button problem

/*
Button

Turns on and off a light emitting diode(LED) connected to digital
pin 13, when pressing a pushbutton attached to pin 7.

The circuit:

  • LED attached from pin 13 to ground

  • pushbutton attached to pin 2 from +5V

  • 10K resistor attached to pin 2 from ground

  • Note: on most Arduinos there is already an LED on the board
    attached to pin 13.

created 2005
by DojoDave http://www.0j0.org
modified 17 Jun 2009
by Tom Igoe

*/

// 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);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

I’ve followed the instructions properly, but when i execute the program the LED just stays on and nothing happens when the button is pressed. Is there another way of connecting it? I have the mega328, my goal is to make a green LED turn on when the button isn’t pressed and a red LED turn on when the button is pressed.

The way i’ve connected is.

Pin 13 to LED, LED to resistor then resistor to ground
Pin 2 to one end of the button, resistor connected to the ground from the same end as pin two, and 5V to the other end of the button.

What kind of switch are you using? Is is a momentary contact switch? 2 pins or 4?

Do you have a multi-meter? If not, you should. Do you know how to use it? If not, you should.

yes I have a multimeter but what for? And its a four pin button

Then use it to see that you have the wires connect to to legs that are connected only when the switch is pressed. My guess is that you have them connected to two legs that are connected all the time.

yes I have a multimeter but what for? And its a four pin button

PaulS has it correctly. There are four ways to wire to that switch with two wires, and only two of the ways will work correctly. An ohm meter reading will validate that you are wired correctly, not pushed = open connections, pushed = just a few ohms.

Lefty

yes that is true and if it then still doesnt work try disconnecting the button and trying another one or even simulating a button with just a wire

grtz diamantmatch

Hi guys,

I’ve got a problem with this example application. It works, but the behaviour is not as expected.

When pressing the switch on my Arduino Mega, the LED does not immediately turn on, but instead turns on when i RELEASE the button.

For example i could hold the button down all day without the LED coming on. Is this expected behaviour? How can i get around this? I need input ports to register the event as soon as it occurs, not when I’ve released the button…

Thanks. :-/

You could begin by posting your code. Depending on how you are wiring the switch, and how you are reading the switch setting, the code may, or may not, be right. Don't know without seeing it.

Not to worry, I’ve solved the problem now. Turns out i had a faulty pull down resistor. Switching it over solves the problem.

Thanks anyway