Step-by-step guides, copyable code, and deployment strategies for the world's most versatile IoT microcontroller.
Choose your toolchain. Arduino IDE is best for beginners, MicroPython for Python developers, and ESP-IDF for production-grade firmware.
Add the ESP32 board manager URL and install the core in under 2 minutes.
https://espressif.github.io/arduino-esp32/package_esp32_index.json
esp32 and install esp32 by Espressif Systems.Flash the firmware and run Python natively on the ESP32.
esptool.py --chip esp32 --port /dev/ttyUSB0 write_flash -z 0x1000 ESP32_GENERIC-20240602-v1.23.0.bin
.bin firmware for your board.esptool: pip install esptoolProfessional C/C++ framework with FreeRTOS and advanced debugging.
./install.sh esp32
. ./export.sh
idf.py set-target esp32
idf.py build flash monitor
install.sh.export.sh.Each project includes required parts, step-by-step instructions, and copyable code. Click any card to expand the full tutorial.
Display real-time temperature and humidity on an OLED screen and push data to ThingSpeak every 30 seconds.[reference:0]
ESP32 DHT22
3.3V ──── VCC
GND ──── GND
GPIO4 ───[10k]─ DATA
ESP32 SSD1306 OLED
3.3V ──── VCC
GND ──── GND
GPIO21 ─── SDA
GPIO22 ─── SCL
DHT sensor library, Adafruit SSD1306, Adafruit GFX Library.#include <WiFi.h>
#include <DHT.h>
#include <Adafruit_SSD1306.h>
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
Adafruit_SSD1306 display(128, 64, &Wire, -1);
void setup() {
Serial.begin(115200);
dht.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
WiFi.begin("SSID", "PASSWORD");
while (WiFi.status() != WL_CONNECTED) delay(500);
}
void loop() {
float t = dht.readTemperature();
float h = dht.readHumidity();
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print("Temp: "); display.print(t); display.println(" C");
display.print("Hum: "); display.print(h); display.println(" %");
display.display();
delay(30000);
}
Capture CO₂, LPG, and smoke levels with MQ135 and MQ2 sensors, then visualize the Air Quality Index on ThingSpeak.[reference:1]
#define MQ135_PIN 34
#define MQ2_PIN 35
void setup() {
Serial.begin(115200);
analogReadResolution(12);
}
void loop() {
int co2 = analogRead(MQ135_PIN);
int gas = analogRead(MQ2_PIN);
Serial.print("CO2: "); Serial.print(co2);
Serial.print(" | Gas: "); Serial.println(gas);
delay(15000);
}
Detect motion and distance, publish to MQTT, and visualize on a Node-RED dashboard with local RGB LED alerts.[reference:2]
#include <WiFi.h>
#include <PubSubClient.h>
#include <NewPing.h>
#define PIR_PIN 21
#define TRIG_PIN 27
#define ECHO_PIN 14
#define LED_R 15
#define LED_G 32
#define LED_B 33
NewPing sonar(TRIG_PIN, ECHO_PIN, 200);
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
pinMode(PIR_PIN, INPUT);
pinMode(LED_R, OUTPUT); pinMode(LED_G, OUTPUT); pinMode(LED_B, OUTPUT);
WiFi.begin("SSID", "PASS");
while (WiFi.status() != WL_CONNECTED) delay(500);
client.setServer("test.mosquitto.org", 1883);
}
void loop() {
int motion = digitalRead(PIR_PIN);
int distance = sonar.ping_cm();
if (motion) { digitalWrite(LED_R, HIGH); }
else { digitalWrite(LED_R, LOW); }
client.publish("esp32/alarm", String(distance).c_str());
delay(1000);
}
Host a responsive web page from the ESP32 and toggle LEDs from any browser on your local network.[reference:3]
#include <WiFi.h>
#include <WebServer.h>
WebServer server(80);
const int ledPin = 2;
void handleRoot() {
String html = "<html><body>";
html += "<h1>ESP32 Web Server</h1>";
html += "<a href='/on'><button>ON</button></a>";
html += "<a href='/off'><button>OFF</button></a>";
html += "</body></html>";
server.send(200, "text/html", html);
}
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
WiFi.begin("SSID", "PASS");
while (WiFi.status() != WL_CONNECTED) delay(500);
server.on("/", handleRoot);
server.on("/on", []() { digitalWrite(ledPin, HIGH); server.send(200); });
server.on("/off", []() { digitalWrite(ledPin, LOW); server.send(200); });
server.begin();
}
void loop() {
server.handleClient();
}
Create a wireless serial connection between your phone and the ESP32 using Bluetooth Classic.[reference:4]
#include <BluetoothSerial.h>
BluetoothSerial SerialBT;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32_Bridge");
Serial.println("Bluetooth ready. Pair with ESP32_Bridge.");
}
void loop() {
if (Serial.available()) SerialBT.write(Serial.read());
if (SerialBT.available()) Serial.write(SerialBT.read());
}
Reduce power consumption to under 10 µA and extend battery life to months by using deep sleep and RTC memory.[reference:5]
#define uS_TO_S_FACTOR 1000000
#define TIME_TO_SLEEP 300
RTC_DATA_ATTR int bootCount = 0;
void setup() {
Serial.begin(115200);
bootCount++;
Serial.println("Boot number: " + String(bootCount));
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
esp_deep_sleep_start();
}
void loop() {}
Control a standard servo motor with the ESP32Servo library. Perfect for robotics and automation prototypes.[reference:6]
#include <ESP32Servo.h>
Servo myservo;
void setup() {
myservo.attach(18);
}
void loop() {
for (int pos = 0; pos <= 180; pos += 1) {
myservo.write(pos);
delay(15);
}
for (int pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(15);
}
}
Over-the-air updates let you push firmware wirelessly. The ESP32 supports ArduinoOTA and ESP-IDF HTTPS OTA out of the box.
Wireless firmware updates from the Arduino IDE over WiFi.
#include <ArduinoOTA.h>
void setup() {
ArduinoOTA.begin();
}
void loop() {
ArduinoOTA.handle();
}
ArduinoOTA library.ArduinoOTA.begin() in setup.ArduinoOTA.handle() in loop.Production-grade OTA with rollback and secure HTTPS.
esp_https_ota_config_t ota_config = {
.http_config = &config,
};
esp_err_t ret = esp_https_ota(&ota_config);
if (ret == ESP_OK) {
esp_restart();
}
esp_https_ota component.esp_https_ota() and reboot on success.Use this command to flash any compiled .bin file to the ESP32 over USB.
esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 460800 write_flash -z 0x1000 firmware.bin