Lesson 2 / 1118%

Lesson 2

The Board and Its Pinout

18 min

Discover the development board, the GPIOs, the 3.3 V voltage and the pins to avoid.

Definition

The pinout is the diagram that shows the role of each pin on an electronic board: which ones are power, which ones are ground, and what functions each GPIO (General Purpose Input Output) can fulfill. Knowing how to read the pinout is essential before connecting any component.

The development board

The ESP32 chip on its own is tiny and hard to use. It is therefore bought mounted on a development board ("DevKit") that adds everything needed to get started. On a typical board, you will find:

  • The ESP32 module: the metal rectangle with a printed antenna area.
  • A USB port (micro-USB or USB-C) to power the board and upload programs.
  • A USB-to-serial chip (CP2102 or CH340) that links the computer and the ESP32.
  • A voltage regulator that converts the 5 V from USB into 3.3 V for the chip.
  • Two buttons: EN (or RST) to restart the board and BOOT for upload mode.
  • Rows of pins on each side, to connect your components.
Photo or annotated diagram: an ESP32 DevKit board with each element labeled (module, antenna, USB port, USB-to-serial chip, EN and BOOT buttons, LED, pins).

The role of the two buttons

The EN button (short for enable) restarts the chip: it is the equivalent of an Arduino's reset button, and your program starts again from the beginning. The BOOT button is connected to pinGPIO0. If you hold it down during the restart, the chip enters upload mode and waits for a new program.

  • Most boards switch to upload mode automatically: you then have nothing to do.
  • If the upload fails with a message like "Connecting...", hold the BOOT button down while the IDE tries to connect, then release it when the transfer starts.
  • After uploading, press EN once if the program doesn't start by itself.

GPIOs: the input/output pins

The programmable pins are called GPIOs ("General Purpose Input Output"), numbered for example GPIO4 or GPIO23. In code, you simply use their number. Unlike the Arduino Uno, the ESP32's pins are multifunction: the same pin can serve as a digital input, a PWM output, an analog input or a communication bus, depending on the configuration you choose.

On a 38-pin board, the numbering printed on the board corresponds directly to the GPIO number. So you writepinMode(23, OUTPUT)to use the pin labeled "23" (or "D23" depending on the board). There is no special prefix like the A of the Uno's analog inputs.

Diagram: the complete pinout of the ESP32 DevKit board used in the course, with GPIO numbers, power pins (3V3, 5V, GND) and the main functions color-coded.

The golden rule: 3.3 V

Warning: the pins do not tolerate 5 V

The ESP32's GPIOs operate at 3.3 V. Applying 5 V to them can permanently damage them, or even destroy the chip. If you want to connect a sensor that outputs 5 V to an input, you must lower the voltage (voltage divider, logic level converter). And no ESP32 pin is designed to be connected to mains power.

As for current, a pin must not supply much: stay under 10 to 12 mA per pin, which is enough to light an LED but not to power a motor or a relay directly.

Consequence for LEDs

An LED connected to a 3.3 V pin receives less voltage than on a 5 V Arduino. It therefore shines a bit less brightly with the same resistor. For a common red or green LED, a 220-ohm resistor is still a good choice: the current will be low but sufficient, and you stay well within the pin's limits.

Converting a 5 V signal to 3.3 V

If a sensor sends a 5 V signal to the ESP32, you can lower it with two resistors in series, called a voltage divider. With a 1 kΩ resistor on the signal side and a 2 kΩ one to GND, the middle point delivers about two thirds of the input voltage, i.e. 3.3 V for 5 V. For fast or bidirectional signals (an I2C bus, for example), it is better to use a small logic level converter module.

Diagram: a voltage divider with a 1-kilohm and a 2-kilohm resistor bringing a 5 V signal down to about 3.3 V before the ESP32's GPIO input.

Power pins

  • 3V3: 3.3 V output, to power compatible sensors and components.
  • 5V (sometimes labeled VIN): connected to USB, useful for components that require 5 V (but not for connecting them to the GPIOs).
  • GND: ground. There are several on the board, and they are all connected to each other.

The board's regulator can only supply limited current on the3V3 pin, and the ESP32 already uses a good part of it when Wi-Fi is active. So don't connect power-hungry components to this pin: a motor, an LED strip or a servo motor should be powered separately, with a common ground.

Pins to know (and pins to avoid)

Not all GPIOs are equal. A few quirks to remember about the classic ESP32:

  • GPIO6 to GPIO11: connected to the internal flash memory. Never connect anything to them.
  • GPIO34, 35, 36 and 39: inputs only. They cannot be used as outputs and have no internal pull resistor.
  • Boot pins (GPIO0, 2, 5, 12 and 15): their level at startup determines the boot mode. A component connected to them can prevent the board from starting up or being programmed.
  • ADC2 (GPIO0, 2, 4, 12 to 15, 25 to 27): the analog inputs in this group cannot be used while Wi-Fi is active.

To get started without trouble

The following pins are the "safest": GPIO4, 13, 16, 17, 18, 19, 21, 22, 23, 25, 26, 27, 32 and 33. For analog reading at the same time as Wi-Fi, prefer GPIO32 to 39.

Pins with a reserved function

Some pins have a default function that you should respect, because they are already used by the hardware or by libraries.

  • GPIO1 and GPIO3: these are TX0 and RX0, the serial link used by USB and the serial monitor. Using them disrupts uploading and display.
  • GPIO21 and GPIO22: the default I2C bus (SDA and SCL), used for OLED displays and many sensors.
  • GPIO23, 19, 18 and 5: the default SPI bus (MOSI, MISO, SCK and SS), used for SD cards and some displays.
  • GPIO25 and GPIO26: the two DAC outputs, able to produce a true analog voltage from 0 to 3.3 V.
  • GPIO4, 0, 2, 15, 13, 12, 14, 27, 33 and 32: the touch pins, sensitive to the mere touch of a finger.

Comparison with the Arduino Uno's pins

  • Numbering: on the Uno, analog inputs have a special name (A0 to A5). On the ESP32, you simply use the GPIO number.
  • Analog resolution: 12 bits on the ESP32 (0 to 4095), versus 10 bits on the Uno (0 to 1023).
  • Logic voltage: 3.3 V on the ESP32, 5 V on the Uno.
  • Flexibility: on the ESP32, a function like PWM or I2C can be assigned to almost any free pin.

The built-in LED

Many boards have a small LED connected to GPIO2. This is not universal: some boards don't have one, and others put it on a different pin. Check your board's documentation before counting on it for your first tests.

Good practice: naming your pins

Since GPIO numbers say nothing about their use, get into the habit of giving them a name in your program with const int. If you need to move a component to another pin, you only change one line, instead of searching through all the code.

named_pins.ino
// Only one line to change if you switch pins
const int ledPin = 23;
const int buttonPin = 4;
const int sensorPin = 34;   // input only: perfect for a sensor

void setup() {
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  int light = analogRead(sensorPin);   // value from 0 to 4095

  if (digitalRead(buttonPin) == LOW) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }

  Serial.println(light);
  delay(200);
}

In this example, the sensor is connected to GPIO34, a pin that can only read: it is a suitable choice. The button is on GPIO4 with the internal pull-up resistorINPUT_PULLUP, and the LED onGPIO23.

Key takeaways

  • The ESP32's GPIOs operate at 3.3 V: never apply 5 V to them.
  • Avoid GPIO6 to 11 (flash), and be careful with the boot pins (0, 2, 5, 12, 15).
  • Pins 34, 35, 36 and 39 can only be used as inputs.
  • With Wi-Fi active, the ADC2 group's analog inputs no longer work.
  • Always check the exact pinout diagram for your board before connecting anything.

Exercise

Find the pinout diagram for your board (search for its exact name followed by "pinout"). Locate the USB port, the 3V3 and GND pins, the EN and BOOT buttons, then color in or highlight the pins to avoid that are described in this lesson.

Exercise: choosing your pins

You want to connect an LED, a button, an analog light sensor and an I2C OLED display. Choose a suitable pin for each component and justify your choice (input only, boot pin, ADC2, default bus...), then write down your wiring plan in a table.

Going further

A sensor outputs a 5 V signal that you want to read with the ESP32. Calculate the values of two resistors forming a voltage divider that brings 5 V down to about 3.3 V (hint: the output voltage equalsVin × R2 / (R1 + R2)), then check your result with the typical values of 1 kΩ and 2 kΩ.