The popularity of Espressif‘s ESP32 chip continues rising thanks to its high performance, versatility, and low cost. For full-stack developers building IoT solutions, the ESP32 serves as highly capable microcontroller packed with advanced peripherals optimized for WiFi applications.

With 48 total GPIO pins available from the chip, leveraging the flexible input/output options can empower developers to interface with diverse sensors, displays, storage devices, and other programmable components to bring custom electronic devices to life.

This definitive ESP32 pin reference guide examines the chip‘s exposed IO from a technical perspective to provide full-stack developers key insights for architecting ESP32 projects. We’ll explore the inner workings and capabilities of key peripheral pin groupings like GPIO, ADC, DAC, PWM while highlighting example use cases.

Guide Sections:

  • Overview of ESP32 Chip Architecture
  • ESP32 Pin Capabilities by Function
  • Visual Pin Mapping References
  • Leveraging Key Peripheral Pins in Sample Projects
  • Architecting ESP32 Projects for IoT, Home Automation, Robotics

Let‘s start by demystifying the internal architecture of the ESP32 chip…

Internal Architecture Overview

The ESP32 packs an immense amount of functionality into the diminutive SiP (System-in-Package) chip measuring just 5mm x 5mm.

ESP32 Chip

Image Source: Espressif

What‘s inside? A highly advanced chipset comprised of the following key components connected internally:

  • Xtensa Dual-Core 32-bit LX6 Microprocessor – Clockable up to 240 Mhz, the dual core architecture allows for simultaneous processing, useful for IoT applications needing WiFi alongside complex sensor monitoring.
  • Ultra Low Power Co-processor – Manages minimal basic functions like timers, interrupts, WiFi sleep when dual core processor powered off for energy conservation.
  • 802.11 b/g/n HT40 Wi-Fi Transceiver – Enables high speed data transfers over WiFi networks.
  • Bluetooth 4.2 Baseband Chip – Facilitates streaming of data over Bluetooth connections.
  • 520 KB SRAM – Provides memory for storing and accessing data quickly.
  • 4 MB Flash Memory– Stores firmware code internally.
  • Peripheral Interfaces – Include capacitive touch, ADCs, DACs, SPI, UART, I2C, I2S and more. Allows interfacing with wide array of external components for data acquisition, control.
  • Security Hardware Accelerators – Dedicated AES, SHA encryption components enable secure data encryption.

This architecture offers immense flexibility – the dual core microprocessor in tandem with the rich peripheral feature set make the compact ESP32 chip an extremely versatile solution for a diversity of application requirements from WiFi/Bluetooth connected applications to more demanding edge computing use cases.

Now let‘s explore how we can access and leverage these integrated components through the exposed ESP32 pins.

ESP32 Pinout by Function

Out of the 48 pins available from the chip, only certain GPIO, power and ground pins are broken out to the ESP32 module‘s pin headers. This includes 30-36 pins depending on board model.

We‘ll now provide an overview of key pin functions available, their capabilities, and example usage scenarios to provide full context.

Power Supply Pins

Like any electronic device, the ESP32 must be powered. This is done through separate pins used for input power and common ground.

Vin – External power can be supplied to this pin. Recommended voltage range of 7-12V. Useful if wanting to power ESP32 from LiPo battery pack rather than just USB.

5V – If wanting to supply regulated 5V input (some boards may not have this pin).

3V3 – The 3.3V output from the on-board buck converter can be used to power external sensors/chips.

GND – Ground pins used for power and signals.

By default, power the ESP32 through the USB port or Vin pin. The on-board regulator will efficiently convert to the needed 3.3V logic level powering the ESP32.

GPIO (General Purpose Input/Output) Pins

The GPIO pins present the most flexible interface options on the ESP32. These pins can dynamically switch between acting as digital inputs or outputs. This empowers developers to toggle switch states, drive LEDs, read button presses, interface with complex sensors, communicate serially, produce PWM outputs and much more.

Here‘s a quick reference for which GPIO can act as inputs/outputs:

GPIO 0: PWM output  
GPIO 1-3: UART0 pins (TXD, RXD)
GPIO 4: ADC1, TOUCH
GPIO 5: PWM capable   

// Pins 6-11 used for flash memory interface

GPIO 12-19: PWM output capable
GPIO 21-22: I2C pins  
GPIO 23: MOSI (VSPI)

// Remaining GPIO provide input/output

So in summary, GPIO 0-5, 12-19, 21-23, and 25-27 act as flexible digital inputs/outputs for interfacing with external components. While GPIO 32-33 are input-only pins.

Example projects showing some ways you can leverage these GPIO:

  • Read Water Flow Sensor – Use GPIO input to monitor pulses from water flow sensor, calculate volume over time.
  • Flash LEDs – Demonstrates GPIO as digital outputs by flashing external LEDs. Just connect anode to GPIO, cathode to ground through resistor.
  • Track Room Occupancy – Read digital output motion sensor through GPIO. Light up LED when motion detected.
  • Control Robot – Drive motor controller by sending PWM signals from GPIO outputs. Vary duty cycle to change motor speed & direction.
  • Record Button Press Time – Time duration between button presses read through GPIO input.

Analog Input Pins

While the GPIO discussed above operate solely on binary high/low signals, the ESP32 contains dedicated analog input pins allowing voltages between 0-3.3V to be read from analog sensors.

This data could represent light intensity, temperature, soil moisture, flex position, or other ranges.

The onboard 12-bit analog-to-digital converters transform that fluctuating voltage into a discrete digital value with a range of 0-4096 (2^12 steps) that can be analyzed in firmware.

ADC1 provides 8 analog input pins accessible via GPIO 32, 33, 34, 35, 36, 37, 38 and 39 respectively.

ADC2 contains channels tied to GPIO 0, 2, 4, 12-15, 25-27 able to be read for analog sampling.

Note ADC2 channels are disabled if WiFi in use.

To read the analog inputs from sensors:

// Read ADC1  
int sensorValue = analogRead(36); 

// Read ADC2
int sensor2 = analogRead(4); 

The return value will be between 0-4096 based on measured input voltage.

Example projects:

  • Soil moisture sensor monitoring for garden automation
  • Ambient light meter using photocell
  • Temperature logger with thermistor input
  • LiDAR/radar connected to ESP32 for object detection

Digital-to-Analog (DAC) Pins

In addition to sampling analog inputs, the ESP32 contains two 8-bit DACs (digital-to-analog converters).

This allows a digital value to represent an analog voltage output. Useful for recreating sensor signals when testing, or producing analog control voltages.

The DAC peripheral channels are connected internally to GPIO 25 and 26.

To output an analog value, write the 8-bit digital equivalant. Such as:

dacWrite(25, 255); // Max 3.3V

dacWrite(26, 128); // Half or 1.65V 

This allows emulating a range of sensor outputs without needing standalone DACs.

Example use cases:

  • Generate analog waveforms to inject test signals
  • Mimic sensor calibration procedures digitally
  • Control volume through voltage-adjustable amplifier

Pulse Width Modulation Pins

Similar to the DAC outputs, pulse width modulation (PWM) provides a means of representing analog values in the digital domain by modulating the duty cycle of a square wave output from the ESP32 GPIOs.

16 independent PWM channels allow produced analog voltages for use cases such as:

  • Dimming LED brightness
  • Motor speed control
  • Transformers/Power regulation

The PWM channels tie to the following GPIO: 0, 2, 4, 5, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23 and 27.

LED brightness control example:

// Change LED luminosity using PWM 
ledcAttachPin(19, 1); // GPIO 19 assigned to channel 1

// Fade LED brightness  
for(int duty=0; duty<=255; duty++) {
  ledcWrite(1, duty); // Update channel 1
  delay(50);
}

Additional example projects:

  • Control servo position based on input sensor
  • Motor speed regulation based on feedback readings
  • Generate analog clock waveforms
  • Tone sound generation

Touch Inputs

The ESP32 contains a 10-channel touch sensor system requiring no additional hardware. This controller scans on its own for changes in capacitance indicating a finger/object touch.

Useful for adding buttons, touchpads, sliders and touchscreens.

Detection can trigger events like wakeup from sleep, register touch actions.

Touch sensing channels attached to GPIO:

Touch 0: GPIO 4
Touch 1: GPIO 0
Touch 2: GPIO 2
Touch 3: GPIO 15

And so on through Touch 9 at GPIO 32.

Sample code detects touches on a given pin:

if(touchRead(4) < 50) {
  Serial.println("Touched!"); 
}

Example use cases:

  • Touch buttons for appliance control panels
  • Touch sliders for light dimming/fan speed
  • Large metal surfaces as proximity sensors
  • Touchscreen MP3 decoders

Serial Communication Protocols

The ESP32 acts as a versatile microcontroller thanks to integrated hardware supporting several serial protocols used widely across IOT devices for chip-to-chip communication.

Let‘s explore some of the key protocols leveraged in ESP32 projects.

SPI

The Serial Peripheral Interface (SPI) protocol allows high speed synchronous serial communication with external components.

SPI uses 4 pins including SCLK, MOSI, MISO and SS. High data rates up to 80 MHz are supported.

External flash memory, sensors, SD cards, LCD displays often leverage SPI.

The ESP32 supports dual SPI buses which share the following pins:

Pin Name GPIO
MOSI GPIO 13, GPIO 23
MISO GPIO 12, GPIO 19
SCLK GPIO 14, GPIO 18
SS GPIO 15, GPIO 5

This facilitates connecting multiple SPI devices independently.

I2C

Philips I2C communication protocol only requires 2 pins for interfacing, allowing multiple devices to share the I2C bus. Useful for lower speed transactions rather than high bandwidth SPI.

Supported external devices include sensors, driver chips, and complex peripherals like WiFi/Bluetooth modules.

By default, dedicated I2C pins at GPIO 21 (SDA Data) and GPIO 22 (SCL Clock) are available. Can support speeds up to 1 MHz.

UART

For asynchronous serial communications, 3 separate dedicated UART controllers connect over the following GPIO:

UART0 TX: GPIO 1
UART0 RX: GPIO 3

UART1 TX: GPIO 10
UART1 RX: GPIO 9

UART2 TX: GPIO 17
UART2 RX: GPIO 16

Example external devices include GPS modules, fingerprint sensors, Bluetooth chips and other microcontrollers.

I2S

Finally, I2S provides serial streaming of PCM audio directly to a DAC or from a microphone.

Leverages the following pins:

  • GPIO 22 – LRCLK
  • GPIO 25 – DATA
  • GPIO 26 – BCLK

By understanding these integrated peripherals, developers can architect ESP32 solutions accommodating diverse external components across prototyping and production.

ESP32 Development Board Pin Mapping

While the functionality breakdown above calls out the ESP32 internal GPIO numbers, hardware development boards break out these integrated chip pins to numbered pin headers.

Here is an overview pinout guide of the popular ESP32-WROOM modules:

ESP32 Dev Board Pinout

Image Source: RandomNerdTutorials.com

Development boards may vary slightly in GPIO assignments, so consult your board pin mapping when developing projects.

This diagrams visualizes how internal ESP32 chip components are routed to available pin headers after factoring necessary external resistors, bootstrapping capacitors and support circuitry.

Sample ESP32 Projects

Now that you have a firm handle on the ESP32‘s capabilities and pin assignments, what can you build with it?

Here are some example project concepts highlighting ways certain hardware peripherals can be incorporated:

  • Digital Weather Display – Barometric pressure & humidity sensors provide analog input to ESP32 ADCs. Outdoor weather readings shown on connected OLED graphic display via I2C. WiFi enables periodic sync of conditions to cloud dashboard.

  • Home Automation Controller – Leverage ESP32 as central IoT hub for DIY smarthome. DHT22 sensor gives temperature/humidity readings via GPIO. Relays toggled by GPIO to actuate appliances. Blynk app provides mobile control interface over WiFi.

  • Robot Car Chassis – Remotely steer custom robot chassis using ESP32 over WiFi. PWM outputs drive DC motors controlling speed & direction. Ultrasonic distance pings provide object detection feedback for automatic collision avoidance.

  • GPS Asset Tracker – Battery powered ESP32 attached to asset uses onboard LiPo charging circuitry. Integrated GPS module provides location updates over serial. Periodically publish coordinates over MQTT via WiFi/LTE for cloud-based asset tracking dashboard.

Final Thoughts

As explored in this guide, the ESP32 chip provides tremendous flexibility thanks to its integrated dual core processor, WiFi/BT radios and rich peripheral set – all packed into a tiny SiP module for extreme affordability.

Whether you‘re getting started with IoT development or need a capable microcontroller to power ambitious connected device projects, understanding the ESP32 pin capabilities outlined here will enable you to fully leverage the hardware for your use case.

The documentation and sample projects aim to equip you to move from prototype to production by architecting customized ESP32 solutions.

Let us know what you end up building! We‘re always excited to see these powerful chips put to work in DIY innovations.

Similar Posts