ESP32 — Digital and Analog I/O
I/O means Input/Output.
ESP32 interacts with the physical world mainly through its GPIO pins.
ESP32 │ ┌──────────┴──────────┐ │ │ INPUT OUTPUT │ │ ┌────┴────┐ ┌────┴────┐ │ │ │ │ Button Sensor LED Relay PIR LDR Buzzer Motor
1. What is Input?
An input is information coming into the ESP32.
Examples:
- Push button
- PIR motion sensor
- LDR
- Rain sensor
- Flame sensor
- Ultrasonic sensor
- RFID reader
- DHT22
For example:
Button pressed ↓ ESP32 ↓ "Someone pressed the button"
2. What is Output?
An output is something controlled by the ESP32.
Examples:
- LED
- Buzzer
- Relay
- Servo
- Motor
- Display
For example:
ESP32 ↓ GPIO HIGH ↓ LED ON
3. Digital I/O
Digital signals have two basic states:
LOW = 0 HIGH = 1
You can think of it as:
LOW → OFF HIGH → ON
For example, an LED:
GPIO LOW → LED OFF GPIO HIGH → LED ON
4. pinMode()
Before using a GPIO, you normally configure its mode.
Syntax:
pinMode(pin, mode);
Common modes:
INPUT OUTPUT INPUT_PULLUP
Example:
pinMode(2, OUTPUT);
This tells ESP32:
GPIO 2 will be used as an output.
5. Digital Output — LED
Connect:
ESP32 GPIO 2 ─── Resistor ─── LED ─── GND
Code:
#define LED_PIN 2 void setup() { pinMode(LED_PIN, OUTPUT); } void loop() { digitalWrite(LED_PIN, HIGH); delay(1000); digitalWrite(LED_PIN, LOW); delay(1000); }
The LED will:
ON → 1 second OFF → 1 second ON → 1 second OFF → 1 second ...
6. digitalWrite()
Used to control a digital output.
Syntax:
digitalWrite(pin, value);
Example:
digitalWrite(2, HIGH);
GPIO 2 becomes HIGH.
Turn it OFF:
digitalWrite(2, LOW);
7. Digital Input
Now suppose we have a push button.
Button │ ↓ ESP32 GPIO
The ESP32 reads whether the button is pressed.
Use:
digitalRead();
Example:
int buttonState = digitalRead(4);
Possible result:
LOW HIGH
8. Button Example
A simple button circuit:
3.3V │ Button │ ├──────── GPIO 4 │ Resistor │ GND
Code:
#define BUTTON_PIN 4 #define LED_PIN 2 void setup() { pinMode(BUTTON_PIN, INPUT); pinMode(LED_PIN, OUTPUT); } void loop() { int buttonState = digitalRead(BUTTON_PIN); if (buttonState == HIGH) { digitalWrite(LED_PIN, HIGH); } else { digitalWrite(LED_PIN, LOW); } }
When button is pressed:
Button ↓ GPIO HIGH ↓ ESP32 detects HIGH ↓ LED ON
9. INPUT_PULLUP
ESP32 provides an internal pull-up resistor.
Instead of an external resistor, you can often use:
pinMode(BUTTON_PIN, INPUT_PULLUP);
Typical wiring:
GPIO 4 ───── Button ───── GND
Code:
#define BUTTON_PIN 4 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"); } }
Important
With INPUT_PULLUP:
Button released → HIGH Button pressed → LOW
This can feel reversed at first.
10. Why Pull-Up/Pull-Down?
An input pin should not be left floating.
A floating pin can randomly read:
HIGH LOW HIGH LOW ...
even when you aren't doing anything.
A pull-up or pull-down resistor gives the pin a stable default state.
Without pull resistor: GPIO → ??? → unstable With pull-up: GPIO → HIGH by default
11. Digital Sensors
Many sensors provide digital output.
For example, a PIR sensor:
PIR OUT ───── GPIO 27
Code:
#define PIR_PIN 27 void setup() { Serial.begin(115200); pinMode(PIR_PIN, INPUT); } void loop() { int motion = digitalRead(PIR_PIN); if (motion == HIGH) { Serial.println("Motion Detected!"); } else { Serial.println("No Motion"); } }
Output:
Motion Detected! Motion Detected! No Motion No Motion
12. Digital Sensor + Output
Now combine a PIR sensor and LED.
PIR │ │ OUT ↓ ESP32 │ │ GPIO ↓ LED
Code:
#define PIR_PIN 27 #define LED_PIN 2 void setup() { pinMode(PIR_PIN, INPUT); pinMode(LED_PIN, OUTPUT); } void loop() { if (digitalRead(PIR_PIN) == HIGH) { digitalWrite(LED_PIN, HIGH); } else { digitalWrite(LED_PIN, LOW); } }
This is the basic concept behind:
Motion-activated lighting.
13. Analog Input
Digital input only understands:
LOW HIGH
But many sensors produce a range of values.
For example:
Dark → low value Medium → medium value Bright → high value
This is where analog input is useful.
14. analogRead()
ESP32 uses ADC to convert an analog voltage into a digital number.
Basic syntax:
int value = analogRead(pin);
For ESP32, ADC readings are commonly represented as:
0 → 4095
when using the typical 12-bit ADC configuration.
Conceptually:
0 V → around 0 ↓ 1 V → some value ↓ 2 V → higher value ↓ 3.3 V → around 4095
The exact behavior can depend on the ESP32 variant and ADC configuration.
15. LDR Example
An LDR can be used to detect light intensity.
Bright light ↓ LDR ↓ Analog voltage ↓ ESP32 ADC ↓ Digital value
Example:
#define LDR_PIN 34 void setup() { Serial.begin(115200); } void loop() { int value = analogRead(LDR_PIN); Serial.print("LDR: "); Serial.println(value); delay(500); }
You may see values such as:
LDR: 500 LDR: 1200 LDR: 2500 LDR: 3800
The exact direction—whether bright means higher or lower—depends on how your LDR voltage-divider circuit is wired.
16. Analog Sensor + Condition
Suppose your LDR gives:
0 ─────────────── 4095 Dark Bright
You can create a condition:
#define LDR_PIN 34 #define LED_PIN 2 void setup() { pinMode(LED_PIN, OUTPUT); } void loop() { int light = analogRead(LDR_PIN); if (light < 1500) { digitalWrite(LED_PIN, HIGH); } else { digitalWrite(LED_PIN, LOW); } }
Concept:
LDR │ ↓ analogRead() │ ↓ Light Value │ ┌──────┴──────┐ ↓ ↓ < 1500 >= 1500 ↓ ↓ LED ON LED OFF
Again, choose the threshold based on your actual sensor wiring and readings.
17. Digital vs Analog
| Feature | Digital | Analog |
|---|---|---|
| Values | HIGH/LOW | Range of values |
| Example | Button | LDR |
| Function | digitalRead() | analogRead() |
| Typical result | 0/1 | 0–4095 |
| Useful for | ON/OFF detection | Measuring levels |
18. ESP32 GPIO Types
ESP32 GPIOs can have different capabilities depending on the exact ESP32 variant/board.
Commonly encountered ESP32 pins include:
GPIO 0 GPIO 2 GPIO 4 GPIO 5 GPIO 12 GPIO 13 GPIO 14 GPIO 15 GPIO 16 GPIO 17 GPIO 18 GPIO 19 GPIO 21 GPIO 22 GPIO 23 GPIO 25 GPIO 26 GPIO 27 GPIO 32 GPIO 33 GPIO 34 GPIO 35
But do not assume every pin is interchangeable.
Some pins have special purposes or restrictions.
For example, on classic ESP32 boards:
GPIO 34–39
are input-only.
So this is generally not valid:
pinMode(34, OUTPUT);
for a classic ESP32.
19. Important: ESP32 Is 3.3V Logic
ESP32 GPIO logic is designed around 3.3V.
Do not directly put 5V into an ESP32 GPIO just because another Arduino board may tolerate 5V logic.
For example:
ESP32 GPIO ↑ │ 5V directly
❌ Avoid this.
For external 5V signals, you may need:
- Voltage divider
- Logic-level converter
- Appropriate interface circuit
20. Digital Input + Analog Input Together
A real project may use both.
For example, a smart room:
PIR │ └──── Digital Input │ ↓ ESP32 ↑ │ LDR ── Analog Input │ ↓ ESP32 │ ┌─────┴─────┐ ↓ ↓ LED Fan
The ESP32 can make decisions using both values.
Example:
#define PIR_PIN 27 #define LDR_PIN 34 #define LED_PIN 2 void setup() { Serial.begin(115200); pinMode(PIR_PIN, INPUT); pinMode(LED_PIN, OUTPUT); } void loop() { int motion = digitalRead(PIR_PIN); int light = analogRead(LDR_PIN); if (motion == HIGH && light < 1500) { digitalWrite(LED_PIN, HIGH); } else { digitalWrite(LED_PIN, LOW); } }
Meaning:
Motion detected? + Is room dark? ↓ YES + YES ↓ Light ON
This is a basic example of IoT automation logic.
21. Digital Output Isn't Only for LEDs
The same concept can control:
Relay
digitalWrite(RELAY_PIN, HIGH);
Buzzer
digitalWrite(BUZZER_PIN, HIGH);
Digital control signal
digitalWrite(CONTROL_PIN, HIGH);
However, motors and other high-current devices should not be connected directly to ESP32 GPIOs. Use a suitable driver such as a relay module, MOSFET driver, or motor driver.
22. Example — Relay Control
#define RELAY_PIN 25 void setup() { pinMode(RELAY_PIN, OUTPUT); } void loop() { digitalWrite(RELAY_PIN, HIGH); delay(3000); digitalWrite(RELAY_PIN, LOW); delay(3000); }
⚠️ Relay modules can be active LOW instead of active HIGH.
For an active-LOW relay:
digitalWrite(RELAY_PIN, LOW); // ON digitalWrite(RELAY_PIN, HIGH); // OFF
Always check your relay module.
23. Reading Multiple Digital Inputs
You can combine arrays with I/O.
int buttons[] = {4, 5, 18, 19}; void setup() { Serial.begin(115200); for (int i = 0; i < 4; i++) { pinMode(buttons[i], INPUT_PULLUP); } } void loop() { for (int i = 0; i < 4; i++) { int state = digitalRead(buttons[i]); Serial.print("Button "); Serial.print(i); Serial.print(": "); Serial.println(state); } delay(500); }
This is useful when you have multiple buttons.
24. The Complete I/O Flow
The ESP32 basically works like this:
PHYSICAL WORLD │ ┌─────────┴─────────┐ ↓ ↓ INPUTS OUTPUTS │ ↑ ┌──────┼──────┐ │ │ │ │ │ Button PIR LDR LED │ │ │ │ └──────┴──────┘ │ │ │ ↓ │ ESP32 CPU ─────────────┘ │ ↓ PROGRAM │ ↓ DECISION
25. Key Functions to Remember
Digital output
digitalWrite(pin, HIGH); digitalWrite(pin, LOW);
Digital input
int state = digitalRead(pin);
Analog input
int value = analogRead(pin);
Configure GPIO
pinMode(pin, OUTPUT); pinMode(pin, INPUT); pinMode(pin, INPUT_PULLUP);
🧠 Remember This
INPUT ↓ ESP32 reads something OUTPUT ↓ ESP32 controls something DIGITAL ↓ HIGH / LOW ANALOG ↓ Range of values
And the most important practical rule:
ESP32 GPIOs are for signals/control, not for powering high-current devices.