4

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

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:

  • thanks, it looks like a good asking, I'll try this in a few hours but I'm sure it will work :-) – clement Jul 24 '14 at 12:44
  • 2
    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 Jul 24 '14 at 18:04
6

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)

  • I'll try this method! Thanks for the tip :-) – clement Jul 24 '14 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 Jul 24 '14 at 13:46
  • 2
    You can now configure the internal pull-up with pinMode(pin,INPUT_PULLUP) without the need for digitalWrite. – Craig Jul 24 '14 at 20:56
  • Cool. What are the IDE/Arduino version requirements to support that? – Duncan C Jul 24 '14 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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