Button detection with or without a resistor?

Hi everyone. That's my first time with Arduino developing but I've already used other microcontrollers.

I have the classic button detection problem:

The button that can link two pins when pressed, and is an open circuit when not pressed.

Every solution I've found around uses an external resistor but someone gave me this code without knowking why works. This represents a little bit simpler solution. In this example one leg is connected to the digital pin 7 and the other one to the ground.

int PinButton = 7;

void setup()
{
  Serial.begin(9600);
  digitalWrite(PinButton,HIGH);
}

void loop()
{
  if(!digitalRead(PinButton))
     Serial.println("Button Pressed");
}

This perfectly works but I'm not sure is the best/correct solution, can you please give me some suggestions? And more, why voltage goes to 0 value when 7 and ground are connected? Where the digitalRead reads the voltage? Maybe I'm asking too much but a little circuit scheme could help me.

Thank you in advance

I/O pins are set as INPUT by default. If your write a HIGH to an INPUT pin it enables the chip's internal PULLUP resistor. All the details about that sort of thing are in the relevant Atmel datasheet.

The more usual way to achieve that with an Arduino is

pinMode(buttonPin, INPUT_PULLUP);

(Note how I have changed the name of your variable so it is more clearly the name of an I/O pin to which a button is connected.)

...R

The reason that the resistor is necessary (internal or external) is that a pin with no connection is “floating”. The inputs have such a high input resistance it takes very little to effect the input state of one that is floating. So you never really know what the floating input will do. The resistor ties the pin to a known level so that it must be purposely driven to the opposite state.

digin.jpg

Ok so I can use the code I showed to you because of the presence of the internal pullup resistor that was implicitly activated. Thank you, you were very helpful!

But it would make more sens to use what Robin2 showed you. Does the same but it's more clear what happens :wink:

yurijh:
Ok so I can use the code I showed to you because of the presence of the internal pullup resistor that was implicitly activated. Thank you, you were very helpful!

No that code does not enable the internal pull up resistors and so does not work perfectly. It might work for you for the time being but it will not work consistently for everyone all the time.

The problem with electronics is that people who have no clue as to what is going on, and anything they try that functions, they think is a solid reliable design because it worked once, then they think they know something. Where as in fact they know less than nothing.

Grumpy_Mike:
No that code does not enable the internal pull up resistors

Are you sure?

If I have got it wrong (quite possible) how do you set the internal pullup resistors without using pinMode INPUT_PULLUP

…R

It depends on the Arduino chip and in some cases which pin. Would you try it on a Due?

Using INPUT_PULLUP leaves chance out of it.

yurijh, are you up on contact switch “bounce”?
www.gammon.com.au/switches

GoForSmoke:
It depends on the Arduino chip and in some cases which pin. Would you try it on a Due?

That is not the context in which I asked my question in Reply #6

I have already recommended using INPUT_PULLUP.

...R