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;
}