0
\$\begingroup\$

Here is a really simple circuit, that I don't quite understand:

https://microcontrollerslab.com/wp-content/uploads/2019/03/Push-button-interfacing-with-ESP32.jpg

I understand the LED portion. What I am not certain I understand is the push button portion.

The voltage goes from the 3.3v pin to the switch, which then routes to both the GPIO15 pin as well as the ground.

Why does it go to both? Is that because the voltage could be too high for the pin and you want to avoid burning it out? If so, do you always need to do this, or is it just sometimes and how do you decide when? Also, why have the resistor after the branch to the GPIO 15 pin?

If it's helpful, here is the link to the actual tutorial that explains what this diagram is for:

https://microcontrollerslab.com/push-button-esp32-gpio-digital-input/

I'm adding a self-drawn diagram that may not be any good but I think this is how it should be represented.

Here is my code, written in the arduino IDE, which works exactly as I expect: when you push the button, the LED toggles its state.

I'm just curious about why the resistor-to-ground part by the GPIO15 pin is necessary.

const int LED_PIN = 22;
const int PUSH_BUTTON = 15;

int pushButtonState;
int ledState = LOW;

void setup() {
  pinMode(LED_PIN, OUTPUT);
  pinMode(PUSH_BUTTON, INPUT);
  digitalWrite(LED_PIN, ledState);
}

void loop() {
  int newButtonState = digitalRead(PUSH_BUTTON);

  if (pushButtonState == LOW && newButtonState == HIGH) {
    if (ledState == LOW) {
      ledState = HIGH;
    }
    else {
      ledState = LOW;
    }
    digitalWrite(LED_PIN, ledState);
  }

  pushButtonState = newButtonState;
}

ESP32 Diagram

\$\endgroup\$
10
  • \$\begingroup\$ If you post a circuit diagram ,you will get better answers. \$\endgroup\$ – Miss Mulan May 2 at 23:34
  • \$\begingroup\$ I guess I could try to figure that out. I'm very new and not certain that would properly draw the diagram but could try. \$\endgroup\$ – dudewad May 2 at 23:37
  • \$\begingroup\$ As far as I can understand from your picture my suggestion is to not try it because if the EVE pin is an output pin and both D15 and EVE go high you will have a short circuit . \$\endgroup\$ – Miss Mulan May 2 at 23:46
  • \$\begingroup\$ I arleady did and it works haha..... I'm working on the diagram now though and will post it in a minute \$\endgroup\$ – dudewad May 2 at 23:50
  • \$\begingroup\$ Yeah make sure EVE pin and GPIO15 pin is not high at the same time. \$\endgroup\$ – Miss Mulan May 2 at 23:53
1
\$\begingroup\$

This is the typical way to connect a switch to digital logic. There is a pulldown resistor, which has one terminal connected to ground and the other end connected to the switch. The other terminal of the switch is connected to the power supply voltage (usually called Vdd).

Consider the point where the switch and resistor are connected. If the switch is open then the voltage at this point will be essentially 0, because of the connection through the resistor. If the switch is closed then current flows through the switch and through the resistor. This makes the voltage across the resistor increase until it is essentially equal to Vdd.

Now we connect our logic input at that point, where the resistor and switch are connected to each other. When the switch is open our logic input sees a very low voltage and when the switch is closed our logic sees a voltage close to the power supply voltage. An open switch is a logic zero, a closed switch is a logic one.

The resistor is necessary because without it the logic input would be "floating"...it wouldn't have a conductive path to either ground or the power supply. A floating logic input picks up noise from the environment and its logic level bounces all over the place. The resistor ensures that we get a valid, stable low voltage when the switch is open.

\$\endgroup\$
2
  • \$\begingroup\$ I feel like I understand taht, but maybe I'm missing something further. Is the gpio pin in this case simply "observing the voltage" and not actually "receiving" it? And what would happen if I got rid of the resitor-to-ground path here, would it cause a short? \$\endgroup\$ – dudewad May 3 at 0:05
  • \$\begingroup\$ Oh, actually, I re-read that last part and that makes more sense now. \$\endgroup\$ – dudewad May 3 at 0:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.