liminfo

ESP32 Reference

Free reference guide: ESP32 Reference

40 results

About ESP32 Reference

The ESP32 Reference is a hands-on cheat sheet for embedded firmware developers working with Espressif's ESP32 microcontroller using the ESP-IDF framework. It covers the eight core API categories used in production IoT firmware: GPIO configuration and interrupt handling, WiFi station and access point modes, Bluetooth Low Energy (BLE) advertising and GATT server setup, ADC sampling with calibration, hardware timers and deep sleep, SPI and I2C peripheral bus initialization, and the ESP-IDF build toolchain and FreeRTOS primitives.

This reference is used by IoT firmware engineers, embedded systems developers, makers, and electronics students who build connected devices with the ESP32. Each entry shows the exact C function call or struct initialization pattern with realistic code snippets, making it easy to copy a working starting point for gpio_config(), spi_bus_initialize(), or xTaskCreate() without hunting through the official documentation.

The reference is organized into eight categories: GPIO (direction, level, config struct, ISR handler), WiFi (STA mode, AP mode, HTTP server and client), BLE (GAP advertising, GATT server, device scanning, NimBLE stack), ADC (raw read, calibration with esp_adc_cal, DAC output, LEDC PWM), Timer (esp_timer, gptimer, vTaskDelay, watchdog, deep sleep), SPI (bus init, device transmit), I2C (master init, write/read), and ESP-IDF (idf.py CLI, NVS storage, SPIFFS filesystem, OTA updates, logging, FreeRTOS tasks and queues, MQTT client).

Key Features

  • GPIO configuration with gpio_config_t struct — pin mask, mode, pull-up/down, interrupt type
  • WiFi STA and AP mode setup with esp_wifi_set_config and authmode options
  • BLE GAP advertising, GATT server registration, BLE scan, and notify/indicate API
  • ADC 12-bit raw read with esp_adc_cal voltage calibration and DAC output
  • LEDC PWM timer config for LED dimming and motor control at custom frequencies
  • SPI bus initialization with DMA and I2C master read/write to peripheral devices
  • FreeRTOS task creation, queue messaging, and watchdog timer reset patterns
  • NVS key-value storage, SPIFFS file system mount, and HTTPS OTA firmware update

Frequently Asked Questions

What is ESP-IDF and how is it different from Arduino for ESP32?

ESP-IDF (Espressif IoT Development Framework) is the official native SDK for ESP32, providing full access to all hardware peripherals, FreeRTOS, and advanced features like secure boot and flash encryption. Arduino-ESP32 is a compatibility layer that wraps ESP-IDF with the familiar Arduino API. ESP-IDF gives more control and better performance but requires more boilerplate; use it for production firmware. Arduino is better for quick prototyping.

How do I configure a GPIO pin as input with a pull-up resistor on ESP32?

Use the gpio_config_t struct and set mode to GPIO_MODE_INPUT, pull_up_en to GPIO_PULLUP_ENABLE, pull_down_en to GPIO_PULLDOWN_DISABLE, and set pin_bit_mask to (1ULL << GPIO_NUM_X). Call gpio_config(&io_conf) to apply the configuration. For interrupt-driven input, also set intr_type to GPIO_INTR_POSEDGE or GPIO_INTR_NEGEDGE, then register a handler with gpio_isr_handler_add().

How do I connect ESP32 to a WiFi network in STA mode?

Initialize the network interface with esp_netif_init() and create the default WiFi STA netif. Fill a wifi_config_t struct with the ssid and password fields, call esp_wifi_set_mode(WIFI_MODE_STA), esp_wifi_set_config(WIFI_IF_STA, &wifi_config), esp_wifi_start(), and then esp_wifi_connect(). Listen for WIFI_EVENT_STA_GOT_IP using the event loop to know when you have an IP address.

What is the difference between esp_timer and gptimer in ESP-IDF?

esp_timer is a software timer built on top of a hardware timer, designed for one-shot or periodic callbacks with microsecond resolution. It is simple to use for application-level timing. gptimer is the low-level hardware General Purpose Timer API introduced in ESP-IDF v5, offering direct control over timer count direction, alarm events, and capture. Use esp_timer for most application code; use gptimer when you need precise hardware-level timing or capture functionality.

How do I reduce ESP32 power consumption using deep sleep?

Call esp_sleep_enable_timer_wakeup(us) to set a wakeup timer, or use esp_sleep_enable_ext0_wakeup() for GPIO wakeup. Then call esp_deep_sleep_start() to enter deep sleep immediately. The chip consumes around 10 µA in deep sleep. On wakeup, execution restarts from app_main(). Use RTC memory (RTC_DATA_ATTR) to preserve variables across deep sleep cycles.

How does NVS (Non-Volatile Storage) work on ESP32?

NVS stores key-value pairs in a dedicated flash partition. Open a namespace with nvs_open(), then use nvs_set_i32/str/blob() to write values and nvs_get_i32/str/blob() to read them. Always call nvs_commit() after writing to persist changes. NVS handles wear leveling automatically. It is ideal for storing WiFi credentials, configuration parameters, and counters that must survive power loss.

What is the difference between BLE classic Bluetooth and BLE on ESP32?

ESP32 supports both Classic Bluetooth (BR/EDR) for audio profiles like A2DP and SPP serial, and Bluetooth Low Energy (BLE) for low-power sensor and IoT applications. BLE uses the GAP layer for device advertising and discovery, and the GATT layer for defining services and characteristics. The ESP32 BLE stack supports two implementations: the default Bluedroid stack and the lighter-weight NimBLE stack, which uses significantly less RAM.

How do I perform OTA firmware updates on ESP32?

Use the esp_https_ota() function with an esp_http_client_config_t pointing to your HTTPS firmware binary URL. ESP-IDF handles downloading the firmware, writing it to the passive OTA partition, verifying the hash, and marking the new partition as bootable. On the next restart, the bootloader switches to the new firmware. Configure two OTA app partitions in your partition table (factory, ota_0, ota_1) to enable rollback on failure.

How do I use MQTT on ESP32 with ESP-IDF?

Initialize an esp_mqtt_client_config_t struct with the broker URI (e.g., mqtt://broker.example.com or mqtts:// for TLS). Call esp_mqtt_client_init() to get a handle, then esp_mqtt_client_start() to connect. Register an event handler for MQTT_EVENT_CONNECTED to subscribe to topics and MQTT_EVENT_DATA to receive messages. Use esp_mqtt_client_publish() to send messages and esp_mqtt_client_subscribe() to subscribe to topics.