Last Updated: November 21, 2025
ESP32
Powerful Wi-Fi & Bluetooth microcontroller
IoT
Hardware
GPIO Pins (ESP32 DevKit)
GPIO 0-39
40 GPIO pins total (some input-only)
Safe GPIO Pins
GPIO 2, 4, 5, 12-19, 21-23, 25-27, 32-33 (safe for I/O)
Input Only: 34-39
GPIO 34, 35, 36(VP), 39(VN) - no pull-up/down resistors
Strapping Pins
GPIO 0, 2, 5, 12, 15 - affect boot mode (use carefully)
GPIO 1 & 3
TX0 and RX0 (serial) - avoid unless necessary
GPIO 6-11
Connected to flash - DO NOT USE
Built-in LED
Usually GPIO 2 (varies by board)
pinMode(pin, mode)
Set pin mode: INPUT, OUTPUT, INPUT_PULLUP, INPUT_PULLDOWN
digitalWrite(pin, val)
Write HIGH or LOW to digital pin
digitalRead(pin)
Read digital value from pin (HIGH or LOW)
ADC (Analog to Digital Converter)
ADC1: GPIO 32-39
ADC1 channels (can use with Wi-Fi)
ADC2: GPIO 0,2,4,12-15,25-27
ADC2 channels (can't use when Wi-Fi active)
12-bit Resolution
Default 0-4095 (can configure 9-12 bits)
analogRead(pin)
Read analog value from ADC pin
analogReadResolution(bits)
Set ADC resolution (9-12 bits)
analogSetAttenuation(atten)
Set voltage range: ADC_0db (1V), ADC_2_5db (1.5V), ADC_6db (2.2V), ADC_11db (3.3V)
DAC (Digital to Analog Converter)
DAC1: GPIO 25
Digital to analog converter 1
DAC2: GPIO 26
Digital to analog converter 2
8-bit Resolution
Output 0-255 (0V to 3.3V)
dacWrite(pin, value)
Write 0-255 value to DAC pin
Touch Sensors
Touch Pins
T0(GPIO4), T2(GPIO2), T3(GPIO15), T4(GPIO13), T5(GPIO12), T6(GPIO14), T7(GPIO27), T8(GPIO33), T9(GPIO32)
touchRead(pin)
Read capacitive touch value (lower = touched)
touchAttachInterrupt(pin, fn, threshold)
Attach interrupt on touch event
Typical Threshold
Set threshold ~60-70% of untouched reading
Arduino IDE Setup
Install Board Manager
File > Preferences > Additional Boards: https://dl.espressif.com/dl/package_esp32_index.json
Install ESP32 Boards
Tools > Board > Boards Manager > Search "ESP32" > Install
Select Board
Tools > Board > ESP32 Arduino > ESP32 Dev Module (or your board)
Select Port
Tools > Port > Select your COM/USB port
Upload Speed
Tools > Upload Speed > 921600 (fast) or 115200 (stable)
Flash Frequency
Tools > Flash Frequency > 80MHz (default)
Partition Scheme
Tools > Partition Scheme > Default 4MB with spiffs
Wi-Fi Basics
#include <WiFi.h>
Include Wi-Fi library
WiFi.mode(WIFI_STA)
Set to station mode (client)
WiFi.begin(ssid, password)
Connect to Wi-Fi network
WiFi.status()
Check connection status (WL_CONNECTED, WL_DISCONNECTED)
WiFi.localIP()
Get assigned IP address
WiFi.disconnect()
Disconnect from Wi-Fi
WiFi.RSSI()
Get signal strength in dBm
WiFi.macAddress()
Get MAC address
Wi-Fi Connection Example
#include <WiFi.h>
const char* ssid = "YourSSID";
const char* password = "YourPassword";
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected!");
Serial.print("IP: ");
Serial.println(WiFi.localIP());
}
void loop() {
// Your code here
}
Wi-Fi Access Point Mode
WiFi.mode(WIFI_AP)
Set to access point mode
WiFi.softAP(ssid, password)
Create Wi-Fi access point
WiFi.softAPIP()
Get AP IP address (default 192.168.4.1)
WiFi.softAPgetStationNum()
Get number of connected clients
Bluetooth Classic
#include "BluetoothSerial.h"
Include Bluetooth Serial library
BluetoothSerial SerialBT
Create Bluetooth Serial object
SerialBT.begin("ESP32")
Start Bluetooth with device name
SerialBT.available()
Check if data available
SerialBT.read()
Read byte from Bluetooth
SerialBT.write(data)
Send data over Bluetooth
BLE (Bluetooth Low Energy)
#include <BLEDevice.h>
Include BLE library
BLEDevice::init("ESP32")
Initialize BLE with device name
createServer()
Create BLE server
createService(UUID)
Create BLE service with UUID
createCharacteristic(UUID, props)
Create characteristic (READ, WRITE, NOTIFY)
startAdvertising()
Start advertising BLE service
Deep Sleep
esp_sleep_enable_timer_wakeup(us)
Wake after microseconds (e.g., 60e6 = 60 seconds)
esp_sleep_enable_ext0_wakeup(pin, level)
Wake on single GPIO (HIGH or LOW)
esp_sleep_enable_ext1_wakeup(mask, mode)
Wake on multiple GPIOs
esp_sleep_enable_touchpad_wakeup()
Wake on touch sensor
esp_deep_sleep_start()
Enter deep sleep mode
esp_sleep_get_wakeup_cause()
Get reason for wakeup
RTC_DATA_ATTR int var
Variable persists through deep sleep
PWM (LED Control)
ledcSetup(channel, freq, resolution)
Configure PWM channel (0-15), frequency (Hz), resolution (1-16 bits)
ledcAttachPin(pin, channel)
Attach GPIO pin to PWM channel
ledcWrite(channel, dutyCycle)
Set duty cycle (0-255 for 8-bit)
ledcDetachPin(pin)
Detach pin from PWM
Interrupts
attachInterrupt(pin, ISR, mode)
Attach interrupt: RISING, FALLING, CHANGE, LOW, HIGH
detachInterrupt(pin)
Detach interrupt from pin
IRAM_ATTR void ISR()
Interrupt service routine must use IRAM_ATTR
portENTER_CRITICAL(&mux)
Enter critical section (disable interrupts)
portEXIT_CRITICAL(&mux)
Exit critical section
Timers
hw_timer_t *timer
Hardware timer pointer
timerBegin(num, prescaler, countUp)
Initialize timer (0-3), prescaler (80 = 1MHz), count direction
timerAttachInterrupt(timer, &ISR, true)
Attach interrupt to timer
timerAlarmWrite(timer, value, reload)
Set alarm value and auto-reload
timerAlarmEnable(timer)
Enable timer alarm
Pro Tips:
- Avoid ADC2 with Wi-Fi: Use ADC1 pins (32-39) when Wi-Fi is active; ADC2 won't work
- Strapping Pins: GPIO 0, 2, 5, 12, 15 affect boot mode - avoid pull-up/down on these
- Deep Sleep Power: Deep sleep uses ~10uA vs ~80mA active; use for battery projects
- Flash Memory: Never use GPIO 6-11; they're connected to SPI flash
- Serial Debugging: Use GPIO 1 (TX) and 3 (RX) for Serial Monitor (115200 baud)
- Touch Sensitivity: Read untouched value first, then set threshold to 60-70% of that
- Wi-Fi Reconnect: Always add WiFi.setAutoReconnect(true) for reliable connections
- Watchdog Timer: Long operations? Disable WDT or use yield() to prevent resets