Understanding Pull-Up Resistors
Before diving into the specifics of ESP32 pull-up pins, it’s important to understand the role of pull-up resistors in a circuit. When a digital input pin is left floating (not connected to any voltage source), it can read random values, making it difficult to determine its logic level.
To avoid this issue, a pull-up resistor is connected between the input pin and a voltage source (typically Vcc) to ensure that the input reads a high (logical 1) state by default. When the input is connected to a low (logical 0) signal, the resistor pulls the input down to the ground, allowing the input to read a low state.
Built-in Pull-Up Pins on ESP32
The ESP32 microcontroller has 34 general-purpose input/output (GPIO) pins, which can be configured as either digital or analog pins. Among these 34 pins, some pins have built-in pull-up resistors that can be enabled by software.
The following table shows the pins on ESP32 that have built-in pull-up resistors:
Pin Number | Pin Name | Built-in Pull-Up Resistor |
0 | GPIO0 | Yes |
2 | GPIO2 | Yes |
4 | GPIO4 | Yes |
5 | GPIO5 | Yes |
12 | GPIO12 | Yes |
13 | GPIO13 | Yes |
14 | GPIO14 | Yes |
15 | GPIO15 | Yes |
25 | GPIO25 | Yes |
26 | GPIO26 | Yes |
27 | GPIO27 | Yes |
32 | GPIO32 | Yes |
33 | GPIO33 | Yes |
34 | GPIO34 | No |
35 | GPIO35 | No |
36 | GPIO36 | No |
39 | GPIO39 | No |
As you can see, most of the digital pins on ESP32 have built-in pull-up resistors. However, not all pins have this feature. Pins 34, 35, 36, and 39 do not have built-in pull-up resistors.
Note: In ESP32, Integrated pull-up, and pull-down resistors are only available in pins that support both input and output. GPIOs 34-39, which are limited to input-only, do not have these resistors built-in.
Check the complete ESP32 Pinout Reference.
Enabling Pull-Up Resistors on ESP32
To enable the pull-up resistor on an ESP32 pin, you can use the gpio_set_pull_mode() function provided by the ESP-IDF framework.
This function takes two arguments:
- The GPIO pin number
- The pull-up mode
The pull-up mode can be either GPIO_PULLUP_ENABLE or GPIO_PULLUP_DISABLE. Here’s an example code that enables the pull-up resistor on GPIO2:
void enable_pull_up() {
gpio_set_pull_mode(GPIO_NUM_2, GPIO_PULLUP_ENABLE);
}
pinMode (5, INPUT_PULLUP);
It’s important to note that enabling the pull-up resistor on a pin will affect its behavior when the pin is used as an output. In this case, the pull-up resistor will act as a weak current source and may affect the output voltage level.
Alternatively, we can also enable internal pull-ups on ESP32 using the pinMode() Arduino function.
The above code will enable the internal pull-up resistor on pin 5. Similarly, you can enable the internal pull-down resistor by specifying the mode INPUT_PULLDOWN.
Conclusion
Pull-up resistors are essential components in digital circuits, and the ESP32 microcontroller provides built-in pull-up resistors on most of its digital pins. Enabling these resistors can ensure stable logic levels and avoid floating input issues. However, it’s important to note that not all pins on ESP32 have built-in pull-up resistors, so it’s important to check the ESP32 pinout or datasheet before designing a circuit. Additionally, enabling the pull-up resistor may affect the behavior of the pin when used as an output.