Warning: 3.3 V, not 5 V
The ESP32-S3’s GPIO handle 3.3 V maximum. A pin set as an output produces 3.3 V, not 5 V as on an Arduino Uno. A 5 V signal applied to an input can damage the chip.
GPIO & the digital world
Understand the digital GPIO of the ESP32-S3: logic levels, pull-up/pull-down and floating state.
Prerequisites:
So far, the microcontroller was thinking on its own. But a chip that only computes is useless: to be useful, it must act on the world (turn on an LED, spin a motor) and sense it (know whether you are pressing a button).
This contact with the physical world goes through the chip’s pins, the small metal legs lined up along the edges of the board. The most versatile ones are called GPIO, and this concept is devoted to the simplest of them: those that only handle all-or-nothing, like a wall switch flipped between on and off. No half-measures, and that is precisely what makes digital so reliable.
By the end of this concept, you will know how to:
The digital world only knows two states: 0 or 1, LOW or HIGH, 0 V or 3.3 V. Any intermediate signal is interpreted according to a threshold.
On the ESP32-S3 (3.3 V logic):
| State | Voltage | Arduino |
|---|---|---|
| LOW | 0 V | 0 / false / LOW |
| HIGH | 3.3 V | 1 / true / HIGH |
As an input, a pin does not switch exactly at 0 V or 3.3 V: it compares the voltage read with two thresholds, and between the two the state is undefined. The ESP32-S3 datasheet (powered at 3.3 V) gives:
| Threshold | Value | Interpretation |
|---|---|---|
| $V_{IL}$ (max LOW input) | ≤ 0.25 × VDD ≈ 0.8 V | Below: read as LOW |
| Undefined zone | ~0.8 V to ~2.5 V | To avoid: unpredictable reading |
| $V_{IH}$ (min HIGH input) | ≥ 0.75 × VDD ≈ 2.5 V | Above: read as HIGH |
Anything below ~0.8 V counts as 0, anything above ~2.5 V counts as 1. This grey zone in the middle explains why a floating input (see below) gives erratic readings: its voltage drifts in this undefined range.
As an output, conversely, the chip guarantees clear levels: close to 0 V for LOW ($V_{OL}$) and close to 3.3 V for HIGH ($V_{OH}$).
These values come from the DC Characteristics table of the ESP32-S3 datasheet (VDD = 3.3 V, 25 °C). This is where you find the answer to most questions about a pin:
| Symbol | What it is | Value |
|---|---|---|
| $V_{IH}$ | Minimum voltage read as HIGH |
0.75 × VDD |
| $V_{IL}$ | Maximum voltage read as LOW |
0.25 × VDD |
| $V_{OH}$ | Minimum guaranteed voltage on a HIGH output |
0.8 × VDD |
| $V_{OL}$ | Maximum guaranteed voltage on a LOW output |
0.1 × VDD |
| $I_{OH}$ | Current a HIGH output can supply |
40 mA |
| $R_{PU}$, $R_{PD}$ | Internal pull resistors | 45 kΩ |
Just before, the absolute maximum ratings table gives the limit never to exceed: 3.6 V on a pin. Beyond that, the chip can be destroyed; between 3.3 V and 3.6 V, it survives but is no longer within its normal operating conditions.
Example: a sensor that signals a HIGH with 2.2 V is not read reliably, since 2.2 V is below $V_{IH}$ ≈ 2.5 V.
Each GPIO pin (General Purpose Input/Output) can be configured as an input or an output by software.
pinMode(LED_PIN, OUTPUT); // pin configured as an output
pinMode(BTN_PIN, INPUT); // pin configured as an input
An output pin can source or sink current (supply it or absorb it) to drive a component.
digitalWrite(LED_PIN, HIGH); // LED on (3.3 V on the pin)
digitalWrite(LED_PIN, LOW); // LED off (0 V)
Maximum current per ESP32-S3 pin: ~40 mA. In practice, limit it to 12 mA with a series resistor.
Calculating the resistor for a red LED (Vf, the LED’s forward voltage, ≈ 2 V; I = 10 mA):
Standard value: 150 Ω (E12 series).
LED series resistor calculator
R ≈ 130 Ω recommended E12 value: 150 Ω
By default, OUTPUT configures the pin as push-pull: two internal transistors make it possible to actively pull the pin HIGH or LOW. This is the mode used so far.
There is a second mode, open-drain: the pin can only pull LOW (a single transistor, on the ground side). To read or produce a HIGH, a pull resistor (internal or external) is needed to “release” the pin towards HIGH when nobody pulls it down.
pinMode(PIN, OUTPUT_OPEN_DRAIN); // the pin can only pull towards 0 V
digitalWrite(PIN, LOW); // actively pulls to 0 V
digitalWrite(PIN, HIGH); // releases the pin (high impedance)
Push-pull (OUTPUT) |
Open-drain (OUTPUT_OPEN_DRAIN) |
|
|---|---|---|
| Can pull HIGH | Yes, actively | No, needs a pull-up |
| Can pull LOW | Yes, actively | Yes, actively |
| Typical use | LED, motor, classic digital output | Shared bus (I2C), interfacing with a different logic level |
An input pin measures the voltage level present on the pin.
int state = digitalRead(BTN_PIN); // returns HIGH or LOW
An input pin connected to nothing is in a floating state: its voltage is undefined, it can read HIGH or LOW randomly depending on electromagnetic interference.
flowchart TD
A["pinMode(PIN, INPUT)\nwithout pull-up or pull-down"] --> B["Pin in high impedance\n(almost no current can flow through it)"]
B --> C{"Does an external signal\nimpose a voltage?"}
C -->|"No - nothing connected"| D["Floating voltage\npicks up the slightest ambient noise"]
D --> E["Unpredictable digitalRead()\nLOW, HIGH, or oscillates for no reason"]
C -->|"Yes - through a pull resistor"| F["Defined and stable voltage\nLOW or HIGH guaranteed at rest"]
A pin in high impedance has a very high input resistance (several megohms): it absorbs almost no current, so nothing brings it back to 0 V or 3.3 V. The slightest surrounding electromagnetic interference (a nearby cable, the 50 Hz mains, your own hand coming closer) is then enough to shift its voltage and flip its reading. Hence the term “floating”: nothing fixes its voltage at a precise value.
A pull resistor forces the pin to a defined level when no signal is applied.
The resistor connects the pin to +3.3 V. At rest, the pin reads HIGH. When the button is pressed (connection to GND), it goes LOW.
graph LR
V33["3.3 V"] -->|"R (10 kΩ)"| NODE(("GPIO pin"))
NODE -->|"Button"| GND["GND"]
The resistor connects the pin to GND. At rest, the pin reads LOW. When the button is pressed (connection to +3.3 V), it goes HIGH.
graph LR
GND["GND"] -->|"R (10 kΩ)"| NODE(("GPIO pin"))
NODE -->|"Button"| V33["3.3 V"]
The ESP32-S3 has internal pull-up (and pull-down) resistors, which can be enabled by software: no need for an external resistor in most cases.
pinMode(BTN_PIN, INPUT_PULLUP); // internal pull-up enabled (~45 kΩ)
// or
pinMode(BTN_PIN, INPUT_PULLDOWN); // internal pull-down enabled
With INPUT_PULLUP: the button must connect the pin to GND to be detected (inverted logic: LOW = pressed).
When you press a mechanical button, the contact is not made cleanly: the metal blades bounce for a few milliseconds, generating a burst of HIGH/LOW before settling. A naive digitalRead() then sees several presses where you made only one.
The simplest fix is in software: after a change of state, readings are ignored for a short delay (20 to 50 ms), using millis() (the number of milliseconds elapsed since start-up), the time it takes for the contact to settle.
const unsigned long DEBOUNCE = 30; // ms
int lastState = HIGH;
unsigned long lastChange = 0;
void loop() {
int state = digitalRead(BTN_PIN); // INPUT_PULLUP: LOW = pressed
if (state != lastState) {
lastChange = millis(); // the signal has just moved
lastState = state;
}
if (millis() - lastChange > DEBOUNCE && state == LOW) {
// button steadily pressed for 30 ms
// (true at every pass as long as the button stays pressed)
}
}
Polling a button at every pass of loop() works, but can miss a press if the loop is busy elsewhere. An interrupt runs a small function as soon as the pin changes, without waiting (see Architecture of a microcontroller).
volatile bool pressed = false;
void IRAM_ATTR onPress() { pressed = true; } // ISR: as short as possible
void setup() {
pinMode(BTN_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BTN_PIN), onPress, FALLING);
}
The ISR (Interrupt Service Routine) just raises a flag; the real processing (and the debouncing) is then done calmly in loop().
Beyond digitalRead()/digitalWrite(), a pin can be handled by a peripheral: UART, SPI, I2C, PWM (LEDC)… On many microcontrollers, each function is hard-wired to specific pins (fixed “alternate function”).
The ESP32-S3 is more flexible: an internal GPIO matrix makes it possible to route almost any digital peripheral signal to almost any pin, by software.
Serial1.begin(115200, SERIAL_8N1, D2, D3); // UART1 routed to the XIAO's D2 (RX) / D3 (TX)
// another pair of pins would work just as well
If no peripheral fits, you can still drive the pins by hand, bit after bit, to emulate a bus: this is bit-banging, slower and more demanding in processor time (see Communication buses).
Despite the flexibility of the GPIO matrix, some pins have a reserved role at boot or in the internal operation of the ESP32-S3. Using them as ordinary GPIO can prevent the board from booting, being flashed or communicating over USB.
| Pins | Reserved role | Precaution |
|---|---|---|
| GPIO 0, 3, 45, 46 | Strapping: read at reset to choose the boot mode | Avoid, or do not force any level on them at boot |
| GPIO 19, 20 | Native USB (D− / D+) | Avoid if you use USB (flashing, serial port) |
| GPIO 43, 44 | Default UART0 (TX / RX of the serial monitor) | Free if the serial port is not used, otherwise avoid |
| GPIO 26–32 | Connected to the internal SPI Flash | Never use |
| GPIO 33–37 | Used by the PSRAM on “octal” modules | Avoid on modules fitted with PSRAM |
On the course’s XIAO ESP32S3, this sorting has already been done by the board: only 11 pins are broken out (D0 to D10) and none of the critical pins is among them. GPIO0 is connected to the BOOT button, GPIO19/20 to the USB-C connector, the Flash and the PSRAM stay internal. Two special cases:
| Limit | ESP32-S3 value |
|---|---|
| Max current per pin | 40 mA (recommended: ≤ 12 mA) |
| Max input voltage | 3.3 V (3.6 V absolute maximum) |
A motor, an LED strip or a relay need far more than a pin’s 12 mA. The pin therefore does not supply the power: it controls a transistor, which supplies it from a separate power supply.
This is the low-side circuit: an N-channel MOSFET cuts or restores the load’s ground.
analogWrite() on the gate, PWM sets the motor’s speed (see ADC & PWM).| Parameter | What it is | Target |
|---|---|---|
| $V_{GS(th)}$ | Gate voltage at which it starts conducting | Well below 3.3 V |
| $R_{DS(on)}$ | Resistance once on: it heats up | Given at 2.5 V, a few tens of mΩ |
| $I_D$ | Maximum current | At least 2 × the load’s |
| $V_{DS}$ | Maximum voltage | Above the load’s supply |
With 3.3 V on the gate, you need a “logic level” MOSFET, whose datasheet gives $R_{DS(on)}$ at 2.5 V: AO3400 (SMD) or IRLB8721 (through-hole), for example. A classic IRF540 expects 10 V on its gate: at 3.3 V, it barely conducts and heats up.
A coil (motor, relay, electromagnet) opposes any sudden change in current. When the MOSFET switches off, it produces a spike of several tens of volts that destroys the transistor. The fix is a diode placed across the load, cathode on the + side: it lets the current die out by circulating in the coil.
The ESP32-S3’s GPIO use 3.3 V logic: applying 5 V to an input can damage the chip (see the warning above). To talk to a 5 V component, you need to shift the level:
HIGH; otherwise, a level shifter is required.1. An unconnected input reads random values. How do you fix it?
2. With INPUT_PULLUP, what level is read when the button is pressed?
3. Why does a digitalRead() sometimes count several presses for a single one?
INPUT) or an output (OUTPUT).0 V (LOW) or 3.3 V (HIGH); ~40 mA absolute maximum, 12 mA at most in practice.INPUT_PULLUP / INPUT_PULLDOWN enable the ESP32-S3’s internal resistor.LOW when pressed.millis()) so as not to count several presses.attachInterrupt) reacts to a pin change without polling; the ISR stays minimal.