5

I use the Pin 7 to get the status of a push button. I put 10Kohm R on the - .

The status is unstable because arduino says thet I push it but it is not true...

int button = 7;
...
// var for reading pushbutton value
int buttonState = 0;
...
buttonState = digitalRead(button);
  if(buttonState == HIGH)
  {
    myFunction();
  }

EDIT :

ARDUINO PIN 7 <---> R 10Kohm <--> [switch] <---> VCC

2
  • 1
    It sounds like you may have connected it incorrectly. Can you show a diagram of your circuit? Commented Jul 24, 2014 at 8:51
  • @Peter: thanks, I didn't use the GND like here: arduino.cc/en/uploads/Tutorial/button.png This looks like to be the mistake isn't it?
    – clement
    Commented Jul 24, 2014 at 9:10

2 Answers 2

10

At the moment, your resistor isn't doing anything useful. When your switch is open, the pin is still 'floating', which means it's ending up with random stray readings from nearby electromagnetic fields.

You basically need two connections coming from your Arduino pin. One goes through a resistor straight to ground. The other goes through your switch straight to +5V.

In that case, the resistor is acting as a pull-down, because it holds your pin at Ground whenever the switch is open. You could swap over the ground and +5V, and it the resistor would function as a pull-up instead.

This question deals with the same kind of issue:

2
  • thanks, it looks like a good asking, I'll try this in a few hours but I'm sure it will work :-)
    – clement
    Commented Jul 24, 2014 at 12:44
  • 3
    Note that the arduino already has pull-up resistors for all the pins inside the chip. You just need to enable them. See DuncanC's answer to see how.
    – Gerben
    Commented Jul 24, 2014 at 18:04
8

Note that you can also activate the internal pull-up resistor on the pin by first making it an input, then setting it to HIGH.

pinMode(pin, INPUT);
digitalWrite(pin, HIGH);

EDIT:

Based on info from Craig (in the comments below) it's simpler to use the new pinMode INPUT_PULLUP:

pinMode(pin, INPUT_PULLUP);

Then wire the switch directly to ground.

This is the simplest way to wire a pushbutton to an arduino (no resistor needed) but do you need an extra line of code.

Also, the pin will read as HIGH when not pressed, and LOW when pressed (which might seem counter-intuitive)

4
  • I'll try this method! Thanks for the tip :-)
    – clement
    Commented Jul 24, 2014 at 13:32
  • Glad to help. Remember to up-vote if it helps, and accept the best answer. (@PeterR.Bloomfield's answer above is probably the one you should accept since it directly addresses the problem with your circuit. My answer is more of a follow-on alternate approach.)
    – Duncan C
    Commented Jul 24, 2014 at 13:46
  • 2
    You can now configure the internal pull-up with pinMode(pin,INPUT_PULLUP) without the need for digitalWrite.
    – Craig
    Commented Jul 24, 2014 at 20:56
  • Cool. What are the IDE/Arduino version requirements to support that?
    – Duncan C
    Commented Jul 24, 2014 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.