Introduction to Digital Inputs
Digital inputs allow a microcontroller like the ESP32 to detect a simple two-state condition — usually represented as HIGH (1) or LOW (0). The most common source of a digital input is a mechanical push button or switch, but the same principle applies to many digital sensors such as PIR motion sensors, IR obstacle sensors, limit switches, and reed switches — any device that outputs a simple ON/OFF signal.
On the ESP32, HIGH typically corresponds to the onboard operating voltage (3.3 V), while LOW corresponds to Ground (0 V). Every GPIO pin used as a digital input must be driven to one of these two well-defined states at all times — never left undefined.
The Floating Pin Problem
Imagine connecting a push button between a GPIO pin and 3.3 V, with nothing else attached. When the button is pressed, the pin correctly reads 3.3 V (logic HIGH). But what happens when the button is released? The pin is connected to nothing — it is "floating."
3.3V
|
[Button] (open)
|
|----- GPIO Pin ---> ??? (undefined voltage - FLOATING)
The solution is to always provide a defined default path to either Ground or 3.3 V using a resistor. This is where pull-down and pull-up resistors come in.
Pull-Down Resistor Configuration
A pull-down resistor connects the GPIO pin to Ground (0V) through a resistor (commonly 10 kΩ). The switch itself connects the pin to 3.3 V.
3.3V
|
[Button]
|
+-------------- GPIO Pin
|
[10k Ohm Resistor]
|
GND
How it works
| Button State | Current Path | GPIO Reading |
|---|---|---|
| Not Pressed | Pin connected to GND through resistor | LOW (0) |
| Pressed | Pin connected directly to 3.3V | HIGH (1) |
When the button is not pressed, the pull-down resistor keeps the pin firmly at 0 V. When pressed, 3.3 V dominates and only a small, safe current (3.3V ÷ 10,000Ω ≈ 0.33 mA) flows through the resistor to ground — small enough to be of no concern.
Pull-Up Resistor Configuration
A pull-up resistor connects the GPIO pin to 3.3 V through a resistor, while the switch connects the pin to Ground. This is the most commonly used configuration in practice, and it is the built-in default on many ESP32 pins.
3.3V
|
[10k Ohm Resistor]
|
+-------------- GPIO Pin
|
[Button]
|
GND
How it works
| Button State | Current Path | GPIO Reading |
|---|---|---|
| Not Pressed | Pin pulled up to 3.3V through resistor | HIGH (1) |
| Pressed | Pin connected directly to GND | LOW (0) |
Internal Pull-Up / Pull-Down Resistors
The ESP32 has built-in internal pull-up and pull-down resistors on its GPIO pins (typically ~45 kΩ) that can be enabled purely through software, removing the need for an external resistor during prototyping.
✅ Good for:
- Rapid prototyping and breadboard testing
- Educational projects
- Simple hobby circuits
⚠️ Use external resistors for:
- Industrial-grade or production hardware
- Long cable runs prone to noise
- Precise, guaranteed resistor tolerance requirements
For robust, professional-grade products, dedicated external resistors are still recommended to ensure predictable, noise-resistant, and manufacturable designs.
Practical Code Examples (Arduino Framework)
1. Reading a Button with an External Pull-Down Resistor
#define BUTTON_PIN 13
void setup() {
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT);
}
void loop() {
int state = digitalRead(BUTTON_PIN);
if (state == HIGH) {
Serial.println("Button Pressed");
} else {
Serial.println("Button Released");
}
delay(200);
}
2. Reading a Button Using the ESP32's Internal Pull-Up Resistor
#define BUTTON_PIN 13
void setup() {
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
int state = digitalRead(BUTTON_PIN);
if (state == LOW) {
Serial.println("Button Pressed");
} else {
Serial.println("Button Released");
}
delay(200);
}
3. Reading a Digital Sensor (e.g. PIR Motion Sensor)
Most digital sensors output a clean HIGH/LOW signal directly, so no external resistor is required.
#define PIR_PIN 14
void setup() {
Serial.begin(115200);
pinMode(PIR_PIN, INPUT);
}
void loop() {
int motion = digitalRead(PIR_PIN);
if (motion == HIGH) {
Serial.println("Motion Detected!");
}
delay(500);
}
Step-by-Step Upload Instructions
Follow these steps in the Arduino IDE, one command per line:
Open Arduino IDE
Select Tools menu
Select Board: "ESP32 Dev Module"
Select the correct COM Port
Copy the code into the editor
Click Verify (checkmark icon)
Click Upload (arrow icon)
Open Serial Monitor
Set baud rate to 115200
Summary
| Concept | Key Point |
|---|---|
| Floating Pin | Undefined voltage — must always be avoided |
| Pull-Down Resistor | Defaults pin to LOW; button pulls it HIGH |
| Pull-Up Resistor | Defaults pin to HIGH; button pulls it LOW |
| Internal Resistors | Enabled in software; convenient for prototyping |
| External Resistors | Recommended for reliable, production-grade designs |