Connecting a Button to an ESP32

In this chapter, we’ll learn how to connect a button to the ESP32 GPIO pin 22, explore the difference between connecting the button to 3.3V or GND, understand the role of a resistor, and write basic Arduino code to interact with the button.

The button we are using here is called a momentary switch, which is “open by default, press to close.” The terms “open” and “close” refer to the electrical state of the button and how it interacts with the circuit.

When the button is not pressed, the two contacts inside the button are not connected. This is referred to as the button being open. In this state, the circuit is incomplete, and no current flows through it.

When the button is pressed, the two contacts inside the button close the circuit, allowing current to flow. This is referred to as the button being closed. In this state, the circuit is complete, allowing current to flow.

GPIO in Default High State

When the GPIO is in a default HIGH state, we need to connect the button to set it to LOW when pressed. Here’s how to do it:

  1. Default HIGH State: To ensure the GPIO is in a HIGH state by default, we use a pull-up resistor. The resistor connects the GPIO pin to 3.3V, which ensures the pin reads HIGH when the button is not pressed.
  2. Connecting the Button:
  • Connect one end of the button to the GPIO pin.
  • Connect the other end of the button to GND.

When the button is pressed, the GPIO pin will receive 0V (LOW) because the button connects the GPIO pin to GND. When the button is not pressed, the pull-up resistor ensures the GPIO pin remains HIGH.

GPIO in Default Low State

When the GPIO is in a default LOW state, we need to connect the button to set it to HIGH when pressed. Here’s how to do it:

  1. Default LOW State: To ensure the GPIO is in a LOW state by default, we use a pull-down resistor (10K). The resistor connects the GPIO pin to GND, which ensures the pin reads LOW when the button is not pressed.
  2. Connecting the Button:
  • Connect one end of the button to the GPIO pin.
  • Connect the other end of the button to 3.3V.

When the button is pressed, the GPIO pin will receive 3.3V, setting it to a HIGH state. When the button is not pressed, the pull-down resistor ensures the GPIO pin remains LOW.

Circuit behaves

Let’s explain the circuit behavior in detail.

In the pull-up configuration, why the GPIO Reads LOW When the Button is Pressed:

  • Button not pressed (GPIO pin reads HIGH):
  • The GPIO pin is connected to VCC through the pull-up resistor, and it sees a HIGH voltage (3.3V).
  • Button pressed (GPIO pin reads LOW):
  • When the button is pressed, the circuit behavior changes. The button creates a short between the GPIO pin and GND.
  • GND is at 0V, so when the button is pressed, it forces the GPIO pin to 0V, overriding the pull-up resistor.
  • The pull-up resistor doesn’t affect this because the button creates a low-resistance path to GND, which is dominant.

Let’s explain the circuit behavior in detail for the pull-down configuration.

In the pull-down configuration, why the GPIO Reads HIGH When the Button is Pressed:

  • Button not pressed (GPIO pin reads LOW):
  • The GPIO pin is connected to GND through the pull-down resistor, and it sees a LOW voltage (0V).
  • Button pressed (GPIO pin reads HIGH):
  • When the button is pressed, the circuit behavior changes. The button creates a short between the GPIO pin and VCC.
  • VCC is at 3.3V, so when the button is pressed, it forces the GPIO pin to 3.3V, overriding the pull-down resistor.
  • The pull-down resistor doesn’t affect this because the button creates a low-resistance path to VCC, which is dominant.

RELATED ARTICLES