Really simple Push Button question

If I am going to attach a N.O. button to an IO pin, do I need a pull down resistor?

yes. Otherwise the pin would float when the switch is not closed.

I don't know what a N.O. button is, but could you use it to connect your pin to ground and use the mega168's internal pull up?

  • Ben

NO=normally open
NC=normally closed

No connect a NO switch to the input and ground. There is no need to put a pull up resistor if you enable the internal ones by writing a 1 to the pin after you initialises it as an input.
You will then read a 0 when it is pressed and 1 when it is not.

No connect a NO switch to the input and ground. There is no need to put a pull up resistor if you enable the internal ones by writing a 1 to the pin after you initialises it as an input.
You will then read a 0 when it is pressed and 1 when it is not.

what do you mean by 'write'? digitalWrite(pin, 1);? because, that doesn't work. all subsequent reads of the pin will be HIGH.

No connect a NO switch to the input and ground. There is no need to put a pull up resistor if you enable the internal ones by writing a 1 to the pin after you initialises it as an input.
You will then read a 0 when it is pressed and 1 when it is not.

what do you mean by 'write'? digitalWrite(pin, 1);? because, that doesn't work. all subsequent reads of the pin will be HIGH.

if you do a digitalWrite(pin, HIGH) on a pin that is in input mode, it will enable the internal pull-up resistor.
As mike has said, this pin will then read high unless the pin is pulled to ground by the switch.

Here is a simplified version:

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

You will read a 0 on the pin when the button is pushed and a 0 when it is not.

Here is a simplified version:

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

You will read a 0 on the pin when the button is pushed and a 0 when it is not.

I think jes means ... and a 1 (HIGH) when its not

Perfect! Thanks guys! The pull up works perfectly.

D'oh! Thanks for the correction.

I have an even more simple button question and was hoping someone can help me:

Is the wiring correct in the picture at http://www.arduino.cc/en/Tutorial/Button ?

I don't see how pressing the button does anything unless the prototyping board is the type that had to be soldered underneath or something. But assuming it is just the push-in type, shouldn't the ground be on the other side of the gap that the button is bridging, so that when the button is pressed it is allowing the less resistant path to the ground?

As it is currently wired, I don't see why pushing the button would do anything.

Also, is this text from the same page correct?:

"You can also wire this circuit the opposite way, with a pull-down resistor keeping the input LOW, and going HIGH when the button is pressed. If so, the behavior of the sketch will be reversed, with the LED normally on and turning off when you press the button. "

Wouldn't keeping the input LOW mean that the input/LED would normally be off, not on?

I am just a beginner so assuming I am just misunderstanding. Any help is greatly appreciated. Thanks!

That diagram is not very clear. I think it is correct, the 5v line is connected to the resistor in the breadboard but it looks like the input is connected to pin 7 but the code has pin 2 !

This tutorial will give you a much better idea of how to use a switch with the Arduino: Arduino Tutorial - Lesson 5