Table of Contents
The ESP32 has built-in pull-up and pull-down resistors, which can be enabled in software using the pinMode() function. The resistance values for the internal pull-up and pull-down resistors in the ESP32 are approximately 45kΩ. These internal resistors are useful when you need a simple way to ensure a default state (either HIGH or LOW) for a GPIO pin, without needing to add external resistors.
Enabling internal pull-up resistor:
pinMode(GPIO_PIN, INPUT_PULLUP);
Enabling internal pull-down resistor:
pinMode(GPIO_PIN, INPUT_PULLDOWN);
Basic Arduino Code
This example configures GPIO 22 as an input, using the internal pull-up resistor.
#define BUTTON_PIN 22 // Define the GPIO pin connected to the button
void setup() {
Serial.begin(115200); // Initialize serial communication
pinMode(BUTTON_PIN, INPUT_PULLUP); // Configure pin 22 as input with internal pull-up resistor
}
void loop() {
// Read the button state
int buttonState = digitalRead(BUTTON_PIN);
if (buttonState == LOW) {
// Button is pressed
Serial.println("Button Pressed");
} else {
// Button is not pressed
Serial.println("Button Released");
}
delay(100); // Small delay for debounce
}