The ESP32, with its Wi-Fi and Bluetooth connectivity, along with a rich set of features, has become a top choice for IoT projects. To fully utilize the power of the ESP32, understanding its basic features such as input, output, and external interrupt functions is crucial.
If you just purchased your ESP32, and want to setup, you can check out part1 of this series.
Alright, let us start:
Input and output allow us to interact with the outside world, reading data from sensors and controlling devices. External interrupts provide an efficient way to react quickly to events that occur, such as when a button is pressed or a sensor detects motion.
Input: Used to read data from peripheral devices such as buttons, sensors, etc. Output: Used to control devices such as LEDs, motors, relays, etc.

Common commands for setting up input/output functions:
pinMode(pin, mode): Sets the mode for the pin (INPUT, INPUT_PULLUP, INPUT_PULLDOWN, or OUTPUT).
digitalRead(pin): Reads the digital value (0 or 1) from the input/output pin.
digitalWrite(pin, value): Writes a digital value (0 or 1) to the output pin.

const int buttonPin = 0; // Pin connected to the button
const int ledPin = 2; // Pin connected to the LED
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Set the pin mode to INPUT_PULLUP
pinMode(ledPin, OUTPUT); // Set the ledPin mode to OUTPUT
}
void loop() {
int buttonState = digitalRead(buttonPin); // Read the button state
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH); // Turn on the LED
} else {
digitalWrite(ledPin, LOW); // Turn off the LED
}
}

When using the INPUT and OUTPUT functions on the ESP32, it's important to choose the appropriate pins to avoid interfering with other operating modes of the ESP32. The table below provides a reference for suitable pin selections:
| GPIO | Input | Output | Notes |
|---|---|---|---|
| 0 | pulled up | OK | outputs PWM signal at boot, must be LOW to enter flashing mode |
| 1 | TX pin | OK | debug output at boot |
| 2 | OK | OK | connected to on-board LED, must be left floating or LOW to enter flashing mode |
| 3 | OK | RX pin | HIGH at boot |
| 4 | OK | OK | |
| 5 | OK | OK | outputs PWM signal at boot, strapping pin |
| 6 | x | x | connected to the integrated SPI flash |
| 7 | x | x | connected to the integrated SPI flash |
| 8 | x | x | connected to the integrated SPI flash |
| 9 | x | x | connected to the integrated SPI flash |
| 10 | x | x | connected to the integrated SPI flash |
| 11 | x | x | connected to the integrated SPI flash |
| 12 | OK | OK | boot fails if pulled high, strapping pin |
| 13 | OK | OK | |
| 14 | OK | OK | outputs PWM signal at boot |
| 15 | OK | OK | outputs PWM signal at boot, strapping pin |
| 16 | OK | OK | |
| 17 | OK | OK | |
| 18 | OK | OK | |
| 19 | OK | OK | |
| 21 | OK | OK | |
| 22 | OK | OK | |
| 23 | OK | OK | |
| 25 | OK | OK | |
| 26 | OK | OK | |
| 27 | OK | OK | |
| 32 | OK | OK | |
| 33 | OK | OK | |
| 34 | OK | input only | |
| 35 | OK | input only | |
| 36 | OK | input only | |
| 39 | OK | input only | |
When using the pins on the ESP32 as INPUT or OUTPUT, we need to pay attention to 5 special pins to avoid affecting the startup and programming process:
GPIO 0: Must be at a LOW level to enter bootloader mode. GPIO 2: Must be in a floating or LOW state during startup. GPIO 5: Must be at a HIGH level during startup. GPIO 12: Must be at a LOW level during startup. GPIO 15: Must be at a HIGH level during startup.
More to read at: https://randomnerdtutorials.com/esp32-pinout-reference-gpios/