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

user2973
1,3918 silver badges12 bronze badges
asked Jul 24, 2014 at 8:34
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? 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:

answered Jul 24, 2014 at 10:24
2
  • thanks, it looks like a good asking, I'll try this in a few hours but I'm sure it will work :-) 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. 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)

answered Jul 24, 2014 at 13:12
4
  • I'll try this method! Thanks for the tip :-) 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.) 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. Commented Jul 24, 2014 at 20:56
  • Cool. What are the IDE/Arduino version requirements to support that? Commented Jul 24, 2014 at 22:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.