Mastering ESP32 Part 4: Understanding ESP32 GPIO Pins and Digital Inputs

Mastering ESP32 Part 4 cover: ESP32 GPIO Pins and Digital Inputs, with an ESP32 board, a pushbutton and a lit red LED on a breadboard

Mastering ESP32 Part 4: Understanding ESP32 GPIO Pins and Digital Inputs

Think of a doorbell. A finger presses the button, two metal contacts touch, and something at the other end of the wire notices and reacts. An ESP32 can be the thing that notices. A pushbutton wired to one of its pins changes the voltage on that pin, and the sketch reads the change and decides what to do about it.

A pin used this way is a digital input: a GPIO (general-purpose input/output pin) that the sketch reads instead of drives. The read has only two possible answers, HIGH or LOW, whatever the real voltage on the pin is. Whether the answer is the one you expect depends on three things: the voltage that reaches the pin, a resistor that holds the pin steady while nothing is driving it, and which pin you chose.

The choice of pin matters more on the ESP32 than on many boards. Some GPIOs are read at reset to decide how the chip starts, some are wired to the memory that holds your program, four on a typical board can only be inputs and have no internal resistor, and two carry the serial link you use to upload code. A button on the wrong one can leave a board that will not boot, or an input that changes on its own, with no error in the sketch.

Part 3, Mastering ESP32 Part 3: Your First ESP32 Project: Blinking an LED, drove one output on GPIO 27. This part adds an input. It covers the voltage levels an input accepts, a map of which pins to use and which to leave alone, the five strapping pins, floating inputs and pull resistors, and a pushbutton on GPIO 4 that controls the LED from Part 3, including how to handle switch bounce. Everything runs at 3.3V from USB power. Analog readings belong to Part 10, timing with millis() to Part 13, and interrupts to Part 14.

Bill of Materials

Component Description Buy on Amazon Buy on Temu Buy on SparkFun Buy on Seeed Studio Datasheet
ESP32 development board Original ESP32 (ESP32-WROOM-32) on a 30-pin DevKit-style board. Not an S2, S3, C3 or C6 board. Amazon Link Not yet available Not yet available Not yet available ESP32 datasheet
Tactile pushbutton One momentary pushbutton, the small four-leg 6x6mm style. Amazon Link Temu Link SparkFun Link Not yet available Omron B3F datasheet
5mm red LED The LED from Part 3. Amazon Link Temu Link SparkFun Link Not yet available Vishay TLHR5400 datasheet
220Ω resistor One resistor for the LED, as in Part 3. A resistor kit covers it. Amazon Link Temu Link SparkFun Link Seeed Link N/A
10kΩ resistor One resistor, used only in the section on input-only pins. The same kit covers it. Amazon Link Temu Link SparkFun Link Seeed Link N/A
Breadboard and jumper wires Four jumper wires cover the button and LED circuit. Amazon Link Temu Link SparkFun Link Seeed Link N/A
Micro-USB data cable The cable used since Part 2. It must carry data, not only power. Amazon Link Temu Link SparkFun Link N/A N/A

The products linked above may contain affiliate links. The Makers Workbench earns from qualifying purchases when these links are used.

The button and the board must both stay on the 3.3V side of the circuit. Nothing in this lesson connects to the pin marked 5V or VIN.

Series Baseline

This series uses a classic ESP32-WROOM-32 module on a 30-pin DevKit-style board, programmed with Arduino IDE 2. The latest stable Arduino-ESP32 core on November 30, 2026 is 4.0.0, published on November 19, 2026.[1] The pin behavior described here was read from the 4.0.0 source, and the sketches were compiled with core 3.3.9 using arduino-cli. They use only pinMode, digitalRead, digitalWrite, delay, and Serial. If Boards Manager offers a newer stable core when you read this, use that one.

All logic on this board runs at 3.3V, and the pins are not 5V tolerant. The numbers in this lesson come from the original ESP32 chip. The S2, S3, C3, C6, and H2 are different chips with different pin maps, so none of the pin tables here apply to them.

What a Digital Input Reads

A digital input turns a voltage into one of two words. The function that does it is digitalRead(pin), which returns HIGH (the number 1) or LOW (the number 0).[2] Before reading, pinMode(pin, INPUT) or one of its pull variants tells the chip to treat the pin as an input.

The chip does not compare the voltage with a single halfway point. The datasheet defines two limits, each as a fraction of the pin's supply voltage, which is 3.3V on this board.[3]

Reading Datasheet name Rule At 3.3V
Guaranteed HIGH VIH, high-level input voltage At least 0.75 × supply, up to supply + 0.3V 2.475V to 3.6V
Guaranteed LOW VIL, low-level input voltage From −0.3V up to 0.25 × supply −0.3V to 0.825V

The arithmetic is short:

VIH(min) = 0.75 × 3.3V = 2.475V

VIL(max) = 0.25 × 3.3V = 0.825V

Between 0.825V and 2.475V lies a band 1.65V wide where the datasheet promises nothing. A pin held in that band may read either value, and two boards may disagree. A working digital circuit therefore drives the pin well above 2.475V or well below 0.825V, and passes through the middle only while switching.

Voltage bar for a 3.3V ESP32 input showing LOW from -0.3V to 0.825V, an undefined band to 2.475V, HIGH to 3.6V, and 5V far above the top

An input also takes almost no current from whatever drives it. The datasheet gives a maximum input current of 50nA (nanoamps, or billionths of an amp) in either state.[3] The pin senses the voltage without loading the circuit that supplies it. That property becomes the reason for pull resistors later in this lesson.

The 3.3V Limit

The upper end of that range is also a limit to respect. Three separate statements describe it, and each comes from a different place.

  • The input rule. The datasheet's maximum for a high input is the pin supply plus 0.3V, which is 3.6V on a 3.3V supply.[3]
  • The supply pins. The absolute maximum ratings table allows −0.3V to 3.6V on the chip's power supply pins. A voltage beyond an absolute maximum rating may damage the part permanently. That table lists supply pins, not individual GPIOs.[3]
  • Espressif's guidance. The company's hardware FAQ states that the voltage tolerance of a GPIO is 3.6V and that a voltage divider should protect the pin from anything higher.[4]

A 5V signal is outside all three. A sensor or module that outputs 5V needs a level shifter (a small circuit that converts one logic voltage to another) or a voltage divider (two resistors that scale a voltage down) between its output and the ESP32 pin. A button needs neither, because it only connects the pin to ground or to the board's own 3.3V.

Which Pins Can Be Digital Inputs

The ESP32 chip has 34 GPIOs, numbered 0 to 19, 21 to 23, 25 to 27, and 32 to 39.[5] There is no GPIO 20, 24, or 28 to 31. A 30-pin board brings out fewer than 34, and the order of the pins along its edges differs between makers, so match the printed GPIO number and not the position.

The code always uses the GPIO number. A pin printed D4, G4, or IO4 is GPIO 4, and digitalRead(4) reads it. The table below sorts the pins by what can go wrong with a button or switch on them.

GPIO Use as a digital input Reason
4, 13, 14, 16, 17, 18, 19, 21, 22, 23, 25, 26, 27, 32, 33 First choices Ordinary input/output pins with internal pull-up and pull-down. Some have other default jobs, listed further down
0, 2, 5, 12, 15 Avoid for buttons Strapping pins: their level at reset changes how the chip starts[3]
1, 3 Leave alone UART0 transmit and receive, used for uploading and the Serial Monitor[5]
6, 7, 8, 9, 10, 11 Do not use Connected to the module's flash memory[5]
34, 35, 36, 39 Input only No output driver and no internal pull resistors[3]
37, 38 Not on these boards Input-only pins that exist on the chip but are not on the headers of Espressif's DevKitC board[6]

ESP32 GPIO map grouping pins into first choices, strapping pins, serial pins, flash pins, and input-only pins

Pins Connected to the Flash Memory

Flash is the memory chip that stores your sketch while the power is off. On a WROOM module it sits under the metal shield and talks to the ESP32 over GPIO 6 to 11. Espressif's DevKitC V4 board does bring these pins out, labeled CLK, D0, D1, D2, D3, and CMD, and its user guide says to avoid using them because doing so can disrupt access to the flash.[6] Many 30-pin boards leave them off the headers. Either way, they are taken.

GPIO 16 and 17 are free on boards built with a WROOM module. Boards with a WROVER module use them for extra memory called PSRAM, so skip both on a WROVER board.[6]

The Serial Pins

GPIO 1 is labeled TX or TX0 and GPIO 3 is labeled RX or RX0 on most boards. Together they form UART0, the first serial port. The USB-to-serial chip on the board is wired to them, and so every upload and every Serial.println() travels over this pair. A button on either one competes with that traffic. Part 9 covers the Serial Monitor.

Input-Only Pins

GPIO 34, 35, 36, and 39 can read but cannot drive, and pinMode(34, OUTPUT) is rejected. The datasheet also states that these pins have no internal pull-up or pull-down circuit.[3] Boards often print GPIO 36 as VP and GPIO 39 as VN.

They are useful for signals that something else always drives, such as the output of a sensor. A bare button on one of them needs an external resistor, which a later section adds. GPIO 36 and 39 carry one more caveat: when certain analog circuits in the chip are powered up, including the analog-to-digital converters, both inputs are pulled low for about 80ns (billionths of a second).[7] A sketch that only polls a button is unlikely to notice, but it is a reason to prefer other pins for edge-sensitive signals.

Other Jobs the First-Choice Pins Share

A GPIO can be connected to one of several internal circuits, so a pin that is a fine digital input today may be wanted for something else later. The defaults for the ESP32 Dev Module board entry are worth knowing before you assign pins in a larger project.[8]

Job Default pins What it is Where it is covered
I²C SDA 21, SCL 22 A two-wire bus for sensors and displays A later part
SPI SCK 18, MISO 19, MOSI 23, SS 5 A faster bus for displays and memory cards A later part
DAC 25, 26 Digital-to-analog converter, a true analog output A later part
ADC 32 to 39 (ADC1); 0, 2, 4, 12 to 15, 25 to 27 (ADC2) Analog-to-digital converter, which measures a voltage Part 10

The ADC2 group cannot be used for analog readings while Wi-Fi is running.[5] That restriction applies to analog measurement. A button read with digitalRead() on GPIO 4 is a digital input and is a separate function of the same pin. GPIO 12 to 15 are also the pins used by JTAG, a hardware debugging interface, which matters only if you connect a debugger.[5]

Strapping Pins

A strapping pin is a pin the chip samples at reset to choose a startup setting. The sampled value is held in a latch until the chip is powered down, and after startup the pin works as an ordinary GPIO.[3] Reset happens at power-up and every time the EN button is pressed, since EN is the chip's enable input and pulling it low restarts the chip.[6]

With nothing connected, a weak resistor inside the chip gives each strapping pin its default level. An external part that pulls harder wins, and the chip starts with a different setting.[3]

Pin Default What it selects at reset Wiring that causes trouble
GPIO 0 Pulled up With GPIO 2, whether to run your program or wait for an upload A button to ground held during reset
GPIO 2 Pulled down Must be low or unconnected to enter upload mode[9] A pull-up resistor
GPIO 12 Pulled down The flash supply voltage Anything that holds it high
GPIO 15 Pulled up Whether the startup messages print Anything that holds it low
GPIO 5 Pulled up With GPIO 15, the timing of an SD card style interface in which the ESP32 is the device Rarely matters for these projects

Five ESP32 strapping pins with their default pull direction and the startup setting each one selects

GPIO 0 and the Boot Mode

GPIO 0 decides between two ways of starting. When it is high at reset, the chip runs the program in flash, which is the normal case. When GPIO 0 and GPIO 2 are both low, the chip enters download mode and waits for a new program over serial.[3] The BOOT button on many boards connects GPIO 0 to ground for exactly this purpose, and holding BOOT while pressing EN forces download mode on Espressif's board.[6]

Now picture your own button from GPIO 0 to ground. In normal use it works. If someone holds it while the board powers up, GPIO 2 is already low by default, and the board waits for an upload instead of running the sketch. The fault appears only at startup, which makes it hard to trace.

GPIO 12 and the Flash Voltage

GPIO 12 selects the voltage the ESP32 supplies to its flash memory. Low, the default, gives about 3.3V. High gives 1.8V.[3] The flash on a common WROOM-32 module needs 3.3V, so a pull-up resistor or a pressed button that connects GPIO 12 to 3.3V at reset can leave the flash underpowered and the board failing to boot.[9] One-time programmable settings inside the chip can override the pin, but do not change them to rescue a wiring choice. Move the wire.

GPIO 15, GPIO 2, and GPIO 5

GPIO 15 controls the messages the chip prints on serial as it starts. Held low at reset, it silences them.[3] The sketch still runs, but a blank start in the Serial Monitor looks like a dead board. GPIO 2 matters during uploads: a pull-up on it can stop the chip from entering download mode.[9] GPIO 5 does not choose the boot mode at all.[3]

The simplest rule for a project is to give buttons, switches, and sensors that drive a pin at power-up to the first-choice pins, and to use a strapping pin only after you have checked what the attached part does during reset.

Floating Inputs and Pull Resistors

Connect a wire to an input and leave the other end in the air. The pin now has nothing setting its voltage. Because an input draws so little current, small charges picked up from nearby wiring, your hand, or the electrical noise of the room are enough to move it, and digitalRead() can return either value. This is a floating input.

A pushbutton creates this situation by design. While pressed, it connects the pin to something. While released, it connects the pin to nothing.

A pull resistor gives the pin a default voltage for the released state. A pull-up resistor connects the pin to 3.3V, so the pin reads HIGH until something pulls it to ground. A pull-down resistor connects the pin to ground, so it reads LOW until something pulls it to 3.3V. The resistance is high enough that the button overrides it.

Three input circuits: a floating pin, a pin with an internal pull-up and a button to ground, and a pin with a pull-down and a button to 3.3V

The Internal Resistors

The ESP32 has both resistors built in on most pins, and pinMode() switches them in.[2]

Mode Internal resistor Wire the button between Released Pressed
INPUT None Not enough on its own Floating Depends on the wiring
INPUT_PULLUP Pull-up Pin and ground HIGH LOW
INPUT_PULLDOWN Pull-down Pin and 3.3V LOW HIGH

The datasheet gives the resistance as 45kΩ (45,000 ohms) typical, with no minimum or maximum value.[3] Treat it as approximate. Two quick calculations show that it does the job. With the button open, the only current through the pull-up is the input's leakage, at most 50nA, so the resistor drops:

V = I × R = 0.00000005A × 45,000Ω = 0.00225V

The pin sits at about 3.298V, far above the 2.475V needed for HIGH. With the button pressed, the pin is connected to ground, which reads as LOW, and the current through the resistor is:

I = V / R = 3.3V / 45,000Ω = 0.000073A

That is about 73µA (microamps, or millionths of an amp), in line with the figure of about 75µA that the datasheet gives for the internal pull strength.[3]

This lesson uses INPUT_PULLUP with the button to ground. It needs two wires, and no wire carries 3.3V across the breadboard where a slip could short it to ground. The result reads backwards at first: pressed is LOW.

One chip fault is worth knowing because you may read about it. On pins that also serve the low-power part of the chip, GPIO 4 among them, the ordinary pull-resistor settings do nothing, and separate low-power registers control the resistors.[7] Espressif's GPIO driver handles this for you: it detects those pins and writes the correct registers.[10] The Arduino core calls that driver, so pinMode(4, INPUT_PULLUP) works.[8] The fault matters to programs that write the hardware registers directly.

The same driver refuses to enable a pull on GPIO 34 to 39, but pinMode() does not report that failure back to your sketch.[10] A line such as pinMode(34, INPUT_PULLUP) compiles and the sketch carries on with a floating pin.

At this point the voltage limits, the pins to use, and the resistor that holds an idle input are settled. What remains is the physical switch, which brings two problems of its own: its four legs, and the way its contacts close.

Wire the Button

A four-leg tactile pushbutton is a switch with two terminals, each brought out on two legs. The legs are joined in pairs inside the body, and pressing the cap connects one pair to the other. The datasheet for the Omron B3F, a common switch of this style, draws exactly that arrangement.[11] If you wire to two legs from the same pair, the pin is connected to ground all the time. If you wire to two diagonally opposite legs, they are always on different pairs, whichever way the switch is turned.

A four-leg pushbutton straddling a breadboard center channel, with the two internally joined leg pairs marked and the diagonal legs used for wiring

From To Why
GPIO 4 One leg of the button The pin reads the voltage here
Diagonally opposite leg GND Pressing the button connects the pin to ground
GPIO 27 One leg of the 220Ω resistor LED output, unchanged from Part 3
Other leg of the 220Ω resistor LED anode (longer leg) The resistor limits the LED current
LED cathode (shorter leg) GND Completes the LED loop

The pushbutton on GPIO 4 with the internal pull-up drawn inside the chip, and the LED and 220 ohm resistor on GPIO 27

The Electronics Lab version of the circuit shows the same connections on a DevKitC V4 board, without the breadboard.

The Electronics Lab wiring: a pushbutton between GPIO 4 and a GND pin, and a red LED with a 220 ohm resistor between GPIO 27 and a GND pin

Disconnect USB before wiring.

  1. Keep the LED circuit from Part 3 in place.
  2. Press the button into the breadboard so it straddles the center channel, the groove that separates the two halves of the board. The legs on opposite sides of the channel then sit in separate rows.
  3. Run a jumper wire from GPIO 4 to the row holding one button leg.
  4. Run a jumper wire from the row holding the diagonally opposite leg to a GND pin.
  5. Check that no wire goes to 3.3V or 5V on the button side, then reconnect USB.

GPIO 4 is a first-choice pin: it is not a strapping pin, it is not connected to flash or the serial port, and it has an internal pull-up. Its other roles, such as an analog input in the ADC2 group, are not used here.[5]

Read the Button

The first sketch tests one thing, whether the pin follows the button. It prints a line twice a second, so you can watch the value change while you press.

// Part 4, sketch 1: report what GPIO 4 reads, twice a second.

const int kButtonPin = 4;            // The button is wired from GPIO 4 to GND.

void setup() {
  Serial.begin(115200);              // Start the serial link at 115200 baud, matching the Serial Monitor setting.
  pinMode(kButtonPin, INPUT_PULLUP); // Make GPIO 4 an input and switch in the internal pull-up resistor.
}

void loop() {
  int level = digitalRead(kButtonPin); // Read the pin. The result is HIGH or LOW.

  if (level == LOW) {                  // LOW means the button is connecting the pin to GND.
    Serial.println("LOW  (pressed)");
  } else {                             // Otherwise the pull-up is holding the pin HIGH.
    Serial.println("HIGH (released)");
  }

  delay(500);                          // Wait half a second so the output is readable.
}

int level creates a named box that holds the whole number returned by digitalRead(). The == operator compares two values, while a single = stores one. The if block runs when the comparison is true, and the else block runs when it is not.

Select ESP32 Dev Module and your port as in Part 2, then choose Upload. Open Tools > Serial Monitor and set the baud list to 115200 baud. With the button released you should see HIGH (released) repeating. Hold the button and the lines change to LOW (pressed).

The output looks like this when the button is pressed partway through:

HIGH (released)
HIGH (released)
LOW  (pressed)
LOW  (pressed)
HIGH (released)

To see a floating input for yourself, change INPUT_PULLUP to INPUT, upload again, and watch the released state. The value may hold steady, flip when your hand approaches the wire, or change at random. A floating pin can appear to work for a while, which is why the fault is easy to miss. Change the mode back before continuing.

Control the LED from the Button

The second sketch connects the input to the output. The LED is lit while the button is held.

// Part 4, sketch 2: the LED on GPIO 27 is lit while the button on GPIO 4 is held.

const int kButtonPin = 4;            // Button to GND.
const int kLedPin = 27;              // LED and 220 ohm resistor to GND.

void setup() {
  pinMode(kButtonPin, INPUT_PULLUP); // Input with the internal pull-up, so released reads HIGH.
  pinMode(kLedPin, OUTPUT);          // Output that drives the LED.
}

void loop() {
  bool pressed = (digitalRead(kButtonPin) == LOW); // true while the button connects GPIO 4 to GND.

  digitalWrite(kLedPin, pressed ? HIGH : LOW);     // LED on when pressed, off when released.
}

A bool is a value that is either true or false. The comparison turns the backwards-looking LOW into a variable whose name says what happened. The expression pressed ? HIGH : LOW is a short form of if and else: it gives HIGH when pressed is true and LOW otherwise.

The loop has no delay(), so it repeats continuously and the LED follows the button with no delay you can see.

Switch Bounce

A switch does not close in one clean step. The metal contacts strike each other and spring apart several times before they settle, and the pin sees a rapid run of HIGH and LOW values. This is contact bounce. It happens on release as well.

How long it lasts depends on the switch. The Omron B3F datasheet gives a maximum bounce time of 5ms (milliseconds, or thousandths of a second).[11] In a published test of 18 switches of various kinds, Jack Ganssle found that after setting aside two unusual ones, the rest averaged about 1.6ms of bounce, with a longest case of 6.2ms. One of the two exceptions bounced for 157ms when released.[12] Your button may be better or worse than any of these, and this lesson has not measured it.

Illustrative trace of an input pin during a button press, showing rapid changes between HIGH and LOW before the level settles

Sketch 2 never shows the problem, because an LED that flickers for a few milliseconds looks the same as one that switches once. The trouble starts when each press should count once, for example to toggle a light. A loop that runs very fast sees every bounce as a new press.

Toggle the LED Once per Press

The third sketch changes the LED each time the button goes from released to pressed. When it first notices a change, it waits 20ms, reads again, and accepts the change only if the new level is still there. I chose 20ms because it is more than three times the 6.2ms figure above, and a press still feels immediate.

// Part 4, sketch 3: each press of the button on GPIO 4 toggles the LED on GPIO 27.

const int kButtonPin = 4;                 // Button to GND.
const int kLedPin = 27;                   // LED and 220 ohm resistor to GND.
const unsigned long kSettleMs = 20;       // Time to wait for the contacts to stop bouncing, in milliseconds.

int lastLevel = HIGH;                     // The last accepted button level. HIGH means released.
bool ledOn = false;                       // Whether the LED is currently on.
int pressCount = 0;                       // How many presses have been accepted.

void setup() {
  Serial.begin(115200);                   // Serial link for the messages.
  pinMode(kButtonPin, INPUT_PULLUP);      // Released reads HIGH, pressed reads LOW.
  pinMode(kLedPin, OUTPUT);               // Output for the LED.
  digitalWrite(kLedPin, LOW);             // Start with the LED off, matching ledOn.
  Serial.println("Ready. Press the button.");
}

void loop() {
  int level = digitalRead(kButtonPin);    // Read the pin now.

  if (level != lastLevel) {               // != means "is not equal to": something changed.
    delay(kSettleMs);                     // Let the bouncing finish.
    level = digitalRead(kButtonPin);      // Read again.

    if (level != lastLevel) {             // The change is still there, so accept it.
      lastLevel = level;                  // Remember the new level.

      if (level == LOW) {                 // Act on the press only, not on the release.
        ledOn = !ledOn;                   // ! flips true to false and false to true.
        digitalWrite(kLedPin, ledOn ? HIGH : LOW); // Update the LED.
        pressCount = pressCount + 1;      // Count this press.
        Serial.print("Press ");           // print() sends text without ending the line.
        Serial.print(pressCount);
        Serial.println(ledOn ? ": LED on" : ": LED off");
      }
    }
  }
}

The variable lastLevel is what lets the sketch notice a change. Reading LOW only says the button is down. Reading LOW after the last accepted level was HIGH says the button has just gone down, and that happens once per press. The three variables are declared above setup() so that their values survive from one pass of loop() to the next.

Each press prints one line, Press 1: LED on, then Press 2: LED off, and so on. Holding the button prints nothing more. To see bounce handling matter, set kSettleMs to 0 and press many times. If your switch bounces, the count will sometimes jump by more than one. If it does not, that switch is closing cleanly, which also happens.

The wait uses delay(), which stops the whole sketch for 20ms. That is acceptable here, where there is nothing else to do. A very short tap that starts and ends inside the wait is ignored. Part 13 rebuilds timing with millis(), which waits without stopping.

Try the Button in the Lab

The Lab runs sketch 3 on a simulated ESP32 with a pushbutton on GPIO 4 and the LED and 220Ω resistor on GPIO 27. It uses Espressif's DevKitC V4 board model because the simulator has no 30-pin board. The GPIO numbers are the same, and only the positions of the pins on the board differ.

From To Why
GPIO 4 Pushbutton, one side The pin reads the button
Pushbutton, other side GND Pressing connects the pin to ground
GPIO 27 220Ω resistor, first leg LED output
220Ω resistor, second leg LED anode (A) The resistor limits the current
LED cathode (C) GND Completes the LED loop
  1. Select Run. The first run compiles the sketch, so it takes a few seconds.
  2. Select Serial and wait for Ready. Press the button.
  3. Press and hold the pushbutton for about half a second, then release it. A very quick click may not register.
  4. Watch the LED change state and one numbered line appear.
  5. Press and hold it again. The LED returns to its earlier state.

In test runs of this Lab, the simulator printed Press 1: LED on right after the ready message, before the button was touched, so your first press may be counted as number 2. The simulated pin appears to read LOW until the simulator updates the button. On a board wired as shown, the pull-up holds the pin HIGH from the start, so that extra line should not appear. The Lab shows the logic of the sketch and not the effect of bounce, and it cannot show what a button on a strapping pin does to a real board at power-up.

External Resistors on Input-Only Pins

A button on GPIO 34, 35, 36, or 39 needs a resistor of your own, because there is none inside. Connect a 10kΩ resistor from the pin to 3.3V, and the button from the pin to ground. The behavior then matches INPUT_PULLUP: released reads HIGH, pressed reads LOW.

From To Why
3V3 One leg of the 10kΩ resistor The supply for the pull-up
Other leg of the 10kΩ resistor GPIO 34 Holds the pin HIGH while the button is open
GPIO 34 One leg of the button The pin reads the button
Diagonally opposite leg GND Pressing connects the pin to ground

While the button is held, the resistor sits between 3.3V and ground:

I = V / R = 3.3V / 10,000Ω = 0.00033A

That is 0.33mA, or 330µA, more than the internal resistor passes and still very small. A lower resistance holds the pin more firmly against electrical noise and wastes more current while pressed. A value near 10kΩ is a common middle ground, and it is useful on any pin with a long button wire, not only on these four. Check the wiring before applying power: a button wired from 3V3 straight to GND, with the resistor somewhere else, shorts the supply when pressed.

A 10 kilohm external pull-up from 3.3V to GPIO 34 with a pushbutton to ground

The sketch uses plain INPUT, since the resistor is outside the chip.

// Part 4, sketch 4: read a button on input-only GPIO 34 with an external 10k pull-up.

const int kButtonPin = 34;        // GPIO 34 has no internal pull resistor.

void setup() {
  Serial.begin(115200);           // Serial link for the messages.
  pinMode(kButtonPin, INPUT);     // Plain input. The 10k resistor to 3V3 does the pulling.
}

void loop() {
  bool pressed = (digitalRead(kButtonPin) == LOW);  // LOW means the button connects the pin to GND.
  Serial.println(pressed ? "pressed" : "released"); // Report the state in words.
  delay(500);                                       // Twice a second is enough to read.
}

Exercises

  1. Threshold check. A signal on an input measures 2.0V. What does the ESP32 read? Answer: the datasheet does not say. 2.0V is between 0.825V and 2.475V.
  2. Pull-down wiring. Rewire sketch 2 to use INPUT_PULLDOWN. Which connection and which line of code change? Answer: move the button's ground wire to 3V3, change the mode, and treat HIGH as pressed. Never connect it to 5V.
  3. Pin choice. A project needs a button, and the free pins are 12, 34, and 25. Which do you pick, and why not the others? Answer: GPIO 25. GPIO 12 is a strapping pin, and GPIO 34 would need an external resistor.
  4. Count releases. Change sketch 3 so it toggles when the button is released instead of pressed. Hint: one comparison changes. Upload it and watch when the LED reacts.

Troubleshooting

Symptom Most likely area First action
Always reads LOW Both wires on the same internal leg pair, or a short to ground Move one wire to the diagonally opposite leg
Always reads HIGH Button not seated, wrong GPIO, or no ground wire Touch a jumper from GPIO 4 to GND and watch for LOW
Value changes with nobody pressing Floating input Use INPUT_PULLUP, or add an external resistor on GPIO 34 to 39
One press counts several times Contact bounce Check that kSettleMs is 20, or try a larger value
Board will not start with the button held Button on a strapping pin Move it to GPIO 4
Upload fails after adding wiring Wire on a strapping or serial pin Disconnect the new wires and upload again
LED stays dark LED circuit Run the Part 3 blink sketch

Compile Errors

Read the first error in the Output panel. A message that a name was not declared usually means a spelling or capital letter differs from the declaration, since kButtonPin and kbuttonPin are different names. A message about an expected ; or } points at the line named or the one above it.

Upload Errors

If the upload cannot connect, unplug the wires you added in this lesson and try again. If it then succeeds, one of them is on GPIO 0, 2, 12, 1, or 3. If it still fails, follow the cable, port, and BOOT button checks in Part 2.

Runtime Problems

Open the Serial Monitor at 115200 baud and press EN. A normal start prints a boot line that includes SPI_FAST_FLASH_BOOT. A line that includes DOWNLOAD means GPIO 0 was low at reset and the chip is waiting for an upload.[9] Release the button, check for anything connected to GPIO 0, and press EN again. If no startup text appears at all but the sketch runs, check for something holding GPIO 15 low.

To separate a wiring fault from a code fault, remove the button and touch one jumper wire between GPIO 4 and GND. If sketch 1 shows LOW while the wire touches, the board and sketch work, and the fault is in the button or its rows.

Where This Fits in the Series

Part 1, Getting Started with the ESP32: A Complete Beginner's Guide, introduced the board and its limits, Part 2 set up Arduino IDE, and Part 3 drove an output. This part added an input and the pin rules that every later circuit follows. Part 5 connects the board to Wi-Fi, Part 10 reads analog voltages, Part 12 dims an LED with PWM, Part 13 replaces delay() with millis(), and Part 14 lets a button interrupt the program instead of being checked in a loop.

References

  1. Espressif Systems, "Arduino release 4.0.0," arduino-esp32 releases, GitHub, November 19, 2026. https://github.com/espressif/arduino-esp32/releases/tag/4.0.0 Accessed November 30, 2026.
  2. Espressif Systems, "GPIO," Arduino ESP32 documentation. https://docs.espressif.com/projects/arduino-esp32/en/latest/api/gpio.html Accessed November 30, 2026.
  3. Espressif Systems, "ESP32 Series Datasheet," version 5.3, sections 2.3, 3, 5.1, 5.3 and Appendix A. https://documentation.espressif.com/esp32_datasheet_en.pdf Accessed November 30, 2026.
  4. Espressif Systems, "Hardware Design," ESP-FAQ. https://docs.espressif.com/projects/esp-faq/en/latest/hardware-related/hardware-design.html Accessed November 30, 2026.
  5. Espressif Systems, "GPIO and RTC GPIO," ESP-IDF Programming Guide, ESP32. https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/peripherals/gpio.html Accessed November 30, 2026.
  6. Espressif Systems, "ESP32-DevKitC V4," user guide. https://docs.espressif.com/projects/esp-dev-kits/en/latest/esp32/esp32-devkitc/user_guide.html Accessed November 30, 2026.
  7. Espressif Systems, "ESP32 Series SoC Errata," version 3.1, GPIO-3.6 and GPIO-3.11. https://docs.espressif.com/projects/esp-chip-errata/en/latest/esp32/esp-chip-errata-en-master-esp32.pdf Accessed November 30, 2026.
  8. Espressif Systems, arduino-esp32 source at tag 4.0.0, variants/esp32/pins_arduino.h and cores/esp32/esp32-hal-gpio.c, GitHub. https://github.com/espressif/arduino-esp32/tree/4.0.0 Accessed November 30, 2026.
  9. Espressif Systems, "Boot Mode Selection," esptool documentation, ESP32. https://docs.espressif.com/projects/esptool/en/latest/esp32/advanced-topics/boot-mode-selection.html Accessed November 30, 2026.
  10. Espressif Systems, ESP-IDF source at tag v6.1.1, components/esp_driver_gpio/src/gpio.c, GitHub. https://github.com/espressif/esp-idf/blob/v6.1.1/components/esp_driver_gpio/src/gpio.c Accessed November 30, 2026.
  11. Omron, "B3F Tactile Switch" datasheet, ratings and dimensions. https://omronfs.omron.com/en_US/ecb/products/pdf/en-b3f.pdf Accessed November 30, 2026.
  12. Jack G. Ganssle, "A Guide to Debouncing," revision 3, June 2008, copy hosted by the University of Utah School of Computing. https://my.eng.utah.edu/~cs5780/debouncing.pdf Accessed November 30, 2026.

Wrap-Up and What's Next

A digital input reads HIGH above 2.475V and LOW below 0.825V on a 3.3V ESP32, and promises nothing between them. A pull resistor holds the pin while nothing else drives it, which INPUT_PULLUP provides on most pins and an external resistor provides on GPIO 34 to 39. Buttons belong on pins with no startup, flash, or serial role, and GPIO 4 is one of them. A short wait and a second read turn a bouncing contact into one counted press.

Part 5 connects the board to a Wi-Fi network, which puts the radio to work and brings the analog pin restriction from this lesson into play.

Hack The World and Make Awesome.

Add new comment

Restricted HTML

  • You can align images (data-align="center"), but also videos, blockquotes, and so on.
  • You can caption images (data-caption="Text"), but also videos, blockquotes, and so on.