Labs / Arduino + ESP32 with Wokwi

09 Re-exploring GPIO Input: Setting Pull-up/Pull-down Resistors

Author: Li Zisheng · Source: FreakStudio open documentation · License: CC BY 4.0

Contents

1. Introduction

In previous experiments, we used the INPUT mode to read the status of the keys. But you may have noticed a problem: the keys sometimes malfunction — even when you release the key, the output in the serial monitor remains unchanged. What's going on here?

In this lesson, we will clarify this issue and introduce a key concept: pull-up and pull-down resistors. Finally, we will conduct a practical experiment — "Key Control LED Light" — to apply pull-up and pull-down resistors to help you consolidate what you have learned.

2. Why is the key reading unstable?

Let's review our control logic

However, during actual operation, you may notice some strange phenomena:

The button was not pressed, but the program displayed it as "pressed".
Running the same code several times will result in different reading results each time.

This is not a bug in your code, but a classic "pitfall" in hardware circuits — floating pins.

2.1 Cause Analysis

Let's conduct a detailed analysis of the circuit logic for button control:

This means: the pin is neither at a high level nor a low level —— it is connected to "nothing but air".

The problem lies precisely in this step.

2.2 Floating Pins

We refer to the situation where a pin is not connected to any valid voltage source as a floating pin.

What happens when reading a floating pin? To verify this, we can conduct a simple experiment:

  1. Set the pin to INPUT mode;
  2. In the loop () function, use digitalRead () to continuously read its state;
  3. Print the results using Serial.print ();
  4. Add a little delay () for easier observation;
  5. Try several unconnected pins and output signals simultaneously.
void setup() {
   Serial.begin(115200);                // communication baud rate 115200
   
   pinMode(2,INPUT);                   // set pin 2 as input mode
   pinMode(4,INPUT);                   // set pin 4 as input mode
   pinMode(5,INPUT);                   // set pin 5 as input mode
   pinMode(12,INPUT);                   // set pin 12 as input mode
   pinMode(13,INPUT);                   // set pin 13 as input mode
   pinMode(14,INPUT);                   // set pin 14 as input mode
   pinMode(15,INPUT);                   // set pin 15 as input mode
   pinMode(18,INPUT);                   // set pin 18 as input mode
}
 
void loop() {
   int a2 = digitalRead(2);           // read the digital signal of pin 2
   int a4 = digitalRead(4);           // read the digital signal of pin 4
   int a5 = digitalRead(5);           // read the digital signal of pin 5
   int a12 = digitalRead(12);         // read the digital signal of pin 12
   int a13 = digitalRead(13);         // read the digital signal of pin 13
   int a14 = digitalRead(14);         // read the digital signal of pin 14
   int a15 = digitalRead(15);         // read the digital signal of pin 15
   int a18 = digitalRead(18);         // read the digital signal of pin 18
   
   Serial.print(a2);                // output the digital signal value over serial
   Serial.print(",");
   Serial.print(a4);                // output the digital signal value over serial
   Serial.print(",");
   Serial.print(a5);                // output the digital signal value over serial
   Serial.print(",");
   Serial.print(a12);                // output the digital signal value over serial
   Serial.print(",");
   Serial.print(a13);                // output the digital signal value over serial
   Serial.print(",");
   Serial.print(a14);                // output the digital signal value over serial
   Serial.print(",");
   Serial.print(a15);                // output the digital signal value over serial
   Serial.print(",");
   Serial.println(a18);              // output the digital signal value over serial
   delay(1000);                         // pause for one second
}

You will find that for pins not connected to any circuit, the readout values change randomly and may differ after each refresh.

This indicates that pins in a floating state do not default to a low level!

Our intuition tends to tell us: "If a pin isn't connected to anything, it should be 0, right? No voltage means a low logic level!"

Why is the reality not what we think it is now?

In a high-impedance state, the pin acts like a "no-man's land" that anyone can influence; even a tiny amount of static electricity, a finger hovering nearby, or electromagnetic fluctuations from surrounding wires can hold it at either a high or low logic level. Every time the device powers on or refreshes, the logic level read could be 1 or 0, with the value determined randomly.

3. Pull-up and pull-down resistors

So how to solve this problem? The answer is — to assign a "default logic level" to the pin. This way, even when the button is not pressed, the pin's state will not fluctuate randomly and can maintain a stable logic value (high or low). For this purpose, we introduce two common solutions: pull-up resistors and pull-down resistors.

3.1 Pull-up Resistor: Defaults to being pulled to high level

"Pull-up" means connecting the pin by default to VCC (high level) via a resistor.

In this way, the level of the pin is "determined" regardless of whether there is an input signal or not.

Let's take a look at the working principle of pull-up resistors:

We connect a resistor between the pin and VCC. When the key is not pressed, current flows from VCC → resistor → pin, and the pin level is high 1.

Once you press the key, the pin is directly connected to GND: pin → GND, and the pin is effectively pulled to a low level, which is 0.

KEY1 10k R1

Status

Pin Level

Causes

Key not pressed

High level (1)

The pull-up resistor provides it with a default "high" level from VCC.

Key pressed

Low level (0)

Pressing the button directly grounds the pin, pulling the voltage level low.

3.2 Pull-down resistor: default to pull to low level

"Pull-down" means connecting a pin by default to GND (low level) via a resistor.

Similarly, regardless of whether there is an input signal or not, the level of the pin is "determined".

Let's take a look at the working principle of the pull-down resistor:

We connect a resistor between the pin and GND; when the key is not pressed, the current flows from the pin → resistor → GND, and the pin level is pulled low to 0.

Once you press the key, the pin is directly connected to VCC, and at this point: the current flows from VCC → key → pin → resistor → GND; the pin is effectively pulled high, which is 1.

KEY1 1 1k R1 2

Status

Pin Level

Causes

Key not pressed

Low level (0)

The pull-down resistor provides it with a default "low" level from GND.

Button pressed

High level (1)

Pressing the key directly connects the pin to VCC, pulling the voltage level high.

3.3 Software Implementation

The ESP32 also directly integrates the function of pull-up and pull-down resistors internally, which we can configure directly via code. Internal pull-up or internal pull-down is supported, eliminating the need for external resistors.

pinMode(2,INPUT_PULLUP);                 // set pin 2 as input mode, enable internal pull-up resistor
pinMode(4,INPUT_PULLDOWN);               // set pin 4 as input mode, enable internal pull-down resistor

It should be noted that GPIO34 to GPIO39 can only be configured as a normal input mode, and do not support input pull-up or input pull-down modes.

Below is a sample code that demonstrates how to enable internal pull-up resistors on multiple pins and read their states:

void setup() {
   Serial.begin(115200);                // communication baud rate 115200
   
   pinMode(2,INPUT_PULLUP);             // set pin 2 as input mode, enable internal pull-up resistor
   pinMode(4,INPUT_PULLUP);             // set pin 4 as input mode, enable internal pull-up resistor
   pinMode(5,INPUT_PULLUP);             // set pin 5 as input mode, enable internal pull-up resistor
   pinMode(12,INPUT_PULLUP);            // set pin 12 as input mode, enable internal pull-up resistor
   pinMode(13,INPUT_PULLUP);            // set pin 13 as input mode, enable internal pull-up resistor
   pinMode(14,INPUT_PULLUP);            // set pin 14 as input mode, enable internal pull-up resistor
   pinMode(15,INPUT_PULLUP);            // set pin 15 as input mode, enable internal pull-up resistor
   pinMode(18,INPUT_PULLUP);            // set pin 18 as input mode, enable internal pull-up resistor
}
 
void loop() {
   int a2 = digitalRead(2);           // read the digital signal of pin 2
   int a4 = digitalRead(4);           // read the digital signal of pin 4
   int a5 = digitalRead(5);           // read the digital signal of pin 5
   int a12 = digitalRead(12);         // read the digital signal of pin 12
   int a13 = digitalRead(13);         // read the digital signal of pin 13
   int a14 = digitalRead(14);         // read the digital signal of pin 14
   int a15 = digitalRead(15);         // read the digital signal of pin 15
   int a18 = digitalRead(18);         // read the digital signal of pin 18
   
   Serial.print(a2);                // output the digital signal value over serial
   Serial.print(",");
   Serial.print(a4);                // output the digital signal value over serial
   Serial.print(",");
   Serial.print(a5);                // output the digital signal value over serial
   Serial.print(",");
   Serial.print(a12);                // output the digital signal value over serial
   Serial.print(",");
   Serial.print(a13);                // output the digital signal value over serial
   Serial.print(",");
   Serial.print(a14);                // output the digital signal value over serial
   Serial.print(",");
   Serial.print(a15);                // output the digital signal value over serial
   Serial.print(",");
   Serial.println(a18);              // output the digital signal value over serial
   delay(1000);                         // pause for one second
}

After the pull-up resistor is enabled, the pin will default to a high level even when it is floating, and the random pin voltage phenomenon will no longer occur.

4. Key-controlled LED

Now let's conduct a very classic experiment: to control the on/off state of an LED with a button.

4.1 Circuit Connection

Connect one end of a pushbutton to GND, and the other end to a GPIO pin, such as GPIO2;

Meanwhile, for this pin, enable the internal pull-up resistor to ensure that the pin remains at a high level when the key is not pressed;

Connect the anode (long leg) of an LED to GPIO 15, and the cathode (short leg) to GND.

4.2 Software Implementation

void setup() {
  pinMode(2, INPUT_PULLUP);   // set the button pin as input mode, enable pull-up
  pinMode(15, OUTPUT);        // set the LED pin as output mode
}

void loop() {
  int keyState = digitalRead(2); // read the button state
  if (keyState == LOW) {
    digitalWrite(15, HIGH); // button pressed, turn on the LED
  } 
  else 
  {
    digitalWrite(15, LOW);  // button released, turn off the LED
  }
}

After the pull-up resistor is enabled:

5. Summary of This Chapter

In this chapter, we learned about the principles and functions of pull-up resistors and pull-down resistors, and understood how they provide a stable default voltage level for input pins to prevent the pins from being in an uncertain "floating" state.

Subsequently, we conducted an experiment of controlling the on/off of an LED via a button to verify the actual application effect of pull-up resistors, and also mastered how to combine input and output to implement a complete interactive process.