Why does a push button have digital and ground resistor in same pin and why connect to ground?

In this small program, I was learning to connect buttons through breadboard and was using OR gate in the code. I learnt how to connect however I want to know that to Why connect ground resistor and the digital pins in the same button pin. And if I connect the digital pin to any other pin, either the led glows out of nowhere or the program doesn't work at all..

Code ~

const int button1 = 2;  // the number of the pushbutton pin
const int button2 = 3;
const int ledPin = 13;    // the number of the LED pin

// variables will change:
int state1 = 0;  // variable for reading the pushbutton status
int state2 = 0;

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  state1 = digitalRead(button1);
  state2 = digitalRead(button2);
  
  if (state1 == HIGH || state2 == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

In your configuration.

When you read the logic level on a digital input that has nothing connected to it, you are reading the level on what is referred to as a floating pin.

This pin as it acts like an antenna, it picks up stray voltages from the circuits nearby.

There is no guarantee what level you will get back from the digitalRead(pin).

We put a 10k resistor from this pin to GND so the pin no longer floats i.e. it is essentially at GND.

We will now see a LOW (GND) on the pin from a digitalRead(pin).

1 Like

And if you want to know more than what @LarryD answered, search pull down and pull up resistors. Here you are adding a pull down.

If you were using a pull-up, you could use the built in resistor that your arduino UNO is offering and that’s activated just by setting the pin to INPUT_PULLUP instead of INPUT.

1 Like

Take a look, with a thanks to LarryD

1 Like

@mysterio ,

This is a common question for beginners, so I wrote this: Buttons and other electro-mechanical inputs (introduction)

1 Like

Sure, I will check it out. Thanks for the help.

Sorry, but since I have just started and am a beginner, so its hard for me to understand this schematic circuit diagram

Do you understand when you closed the switch, your 5 volts is connected to the pin.

Now when we do a digitalRead(pin) we will get a HIGH ?

1 Like

Ok So you mean that when I don't enter digitalRead in the code, it is grounded through the resistor and if I use digitalRead, it gets connected to the digital pin for that instance..

Thanks @paulpaulson @LarryD thanks for the help. I am taking a bit longer to understand cause I will just enter the 10th grade and using Arduino and all these components is new to me

Also, @J-M-L @PerryBebbington since I can only mention two in a post, a restriction due to new account :smiley:

No

The pin is always connected to GND through the 10k resistor, this GND can be overcome when we connect 5v to the pin using the switch.

When the switch connects 5v to the pin, the pin no longer sits at GND, it is connected to 5v.

When the switch is open, we read a LOW with digitalRead(), when the switch is closed, 5v on the pin gives a HIGH with digitalRead().

1 Like

Tell us what you think this line of code does:

state1 = digitalRead(button1);

I think that when we enter this code, the arduino will read the status of the button (Either High or Low) through the digital pin and then store the status in the variable state1

Yes :+1:

state1 will become a 1 (HIGH) or 0 (LOW).

1 Like

I watched some yt videos and now I think my concept is clear. So really the 5v and pin in front of it on the other side are always connected internally. When I press the button, 5v power also gets to the digital pins and so It reads a HIGH and because We don't want so much power so we remove the excess power through resistor and put it in the ground. Am I correct?

And this is how the four-pin tiny button works..

The 4 pin switch you show in the wiring image connects diagonal pins together when it is pressed (closed).

When the switch is open, the 10k resistor makes sure any voltage on the pin is discharged to GND. This is similar to you walking across a carpet and then discharging the static electricity in your body on a metal object (like a light switch).

This 10k resistor can be as high as 100k and more and still discharge the pins voltage.

When the switch is closed 5v will be on the pin and actually appear across the 10k resistor.

If you had a voltmeter, you would see 5v across this resistor.

As a side note, when we have 5v across the 10k resistor, the current through the resistor is:

I(current) = V(voltage) ÷ R(resistance) = 5V ÷ 10k = .0005Amps = 500uA

Google Ohms Law for the calculation

3 posts were split to a new topic: Pull up and pull down resistors

Thanks. I have studied Ohm's law

Studied, perhaps, but your grasp of it's application is not evident per your original post and further questions.