Why is it 0 when I press the button

int button=2;
int buttonVal;

void setup() {
  Serial.begin(9600);
  pinMode(button,INPUT);
  digitalWrite(button,HIGH);
}

void loop() {
  buttonVal=digitalRead(button);
  Serial.print("Your Button is: ");
  Serial.println(buttonVal);
}

why is it 0 when I press the button

As you can read, the digitalRead functions returns HIGH or LOW: https://www.arduino.cc/reference/en/language/functions/digital-io/digitalread/.
When that is printed as a number, then LOW happens to be 0.

By doing this

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

you have turned on the internal pullup resistor of the processor which will normally keep the input pin HIGH

How is the button wired ?

I understand that but why is it LOW

pin 2 to button first leg and ground to second without resistor

So when you press the button the input is taken from HIGH to LOW so why does it surprise you that pressing the button takes pin 2 LOW ?

where does the voltage go shouldn't it be high?

Voltage doesn't go anywhere but the current does

When you push the button the current flows between the 5v supply of the chip (remember that you have turned on the internal pullup resistor), through the pullup resistor to GND. The pin is connected to GND so why would you expect it to read HIGH (5V) ?

so the current is 0?

While you press the button, current flows from the internal pull-up resistor through the button to GND.
Therefore, the voltage of the button PIN drops to GND and reads 0.

When you release the button, the current loses that pass and doesn't flowed.
Therefore, the voltage of the button PIN is maintained at 5V coming from the internal pull-up resistor, so reads 1.

No

The voltage on the pin is 0, and the current is flowing through the built in pullup resistor, which is about 20K and 50K, from about 5V to 0V

I will leave it to you to calculate the current using Ohms law if you want to

1 Like

oh okay I forgot the fact that button is a resistor so if 5V / 20K would be 0,00025. Thanks a lot

A button is a resistor with either infinity resistance or with zero resistance.

From the datasheet of the 328P


If you enable the internal pullup, you open the FET and the signal Logic will be Vcc if no button is connected or if the button is not pressed. A button wired between Pxn and GND and pressed will short the Logic signal to ground.

Of course in reality, neither of these is precisely true. :grin:

Especially for little "tact" buttons.

But near enough most of the time. :+1:

@Paul_B
I'm sorry if you feel rude;
IMO, when doing such a post to a beginner's topic, it is better to explain why it is.

e.g.
I've seen topics that were beginner confused by posts such as denying that no current flows when the MOSFET is off.
It is true that off state MOSFET has a very very small leakage current through.
But on the spot, I'm convinced that it was an unnecessary point.

On a fence.

Teachers lie, ppl here lie. Not so much in the sense of a malicious lie, but strictly speaking not telling the truth.

I only had one teacher who had the good sense and common decency to state up front that he would be lying to us. Frequently.

He might say "never" instead of "very probably not ever", or zero when 0.001 might be more accurate; that you "can't" do something when it might just mean you shouldn't, or aren't at a point where it is a good idea.

And so forth. Sometimes always never maybe. For our own good?

He also promised that he would take no trouble to go out of his way to indicate that he was lying. Not telling the truth that is, or the whole truth.

And again was kind enough to allow that he would not catch us in his lies.

Occasionally some in the class would call him out on a lie. In those cases he would address the matter variously, sometimes briefly and other times using it as an opportunity to open the window a bit. Never ruffled a feather on that dude.

I am sure y'all know the kind of absolute statements that are made by the heavies around here. Most of the time they can be left uncommented upon. Those that know do, those that don't, well, don't and as long as the matter is not dangerously wrong and misleading it can be left as is.

My favorite, and one I will admit to challenging myself, shouldn't bother, is the idea that the mills() counter cannot be reset after power up…

Like anything you pick up (learn?) anywhere on the internets, if you get a tingle of a hint that it isn't 100 percent on the up and up, a vast wealth of alternate resources is available.

At least with electronics and software, there are facts and answers, and means to test and verify.

So I think if some noob is considering that a switch and a resistor are forming a voltage divider, I'd tell him the switch has zero resistance and to see how silly that is, or really hey! that is a voltage divider, never thought of it that way! and/but you numerator for the voltage on the switch is 0.

But it is hard being at a distance, for here really is a the kind of case where knowing the student(s) in front of you would be a guide to how to proceed.

a7

1 Like