The ESP32 is a low-cost microcontroller with integrated Wi-Fi and Bluetooth connectivity, ideal for IoT, control, and automation projects.
Key Features
- CPU: Dual-core Tensilica Xtensa LX6 (up to 240 MHz).
- RAM: 520 KB of SRAM.
- Connectivity: Wi-Fi 802.11 b/g/n and Bluetooth 4.2.
- GPIOs: Up to 36 programmable pins.
- ADC: 18 channels of 12 bits.
- DAC: 2 channels of 8 bits.
- Interfaces: I2C, SPI, UART, PWM, I2S.
Initial Setup
Install the ESP32 Board in the Arduino IDE
To start programming the ESP32 with the Arduino IDE, you first need to add the board support.
- Go to
File > Preferences
. - In “Additional Board Manager URLs”, add the following URL:
https://dl.espressif.com/dl/package_esp32_index.json
- Go to
Tools > Board > Board Manager
and search forESP32
. - Install the board.
Select the Board and Port
Once the board is installed, select the model of ESP32 you are using.
Tools > Board > ESP32 Dev Module
Select the USB port to which the ESP32 is connected:
Tools > Port > (Select the correct port)
First Program (Blink)
Code to blink an LED on the ESP32
void setup() {
pinMode(2, OUTPUT); // Set GPIO 2 as output
}
void loop() {
digitalWrite(2, HIGH); // Turn on the LED
delay(1000); // Wait 1 second
digitalWrite(2, LOW); // Turn off the LED
delay(1000); // Wait 1 second
}
Peripheral Management
GPIO (Digital Input and Output)
Configure a pin as input or output
pinMode(pin, OUTPUT); // Set a pin as output
pinMode(pin, INPUT); // Set a pin as input
digitalWrite(pin, HIGH); // Set the pin value to HIGH or LOW
int state = digitalRead(pin); // Read the state of a pin
PWM (Pulse Width Modulation)
Generate PWM signal on ESP32
const int ledPin = 2;
const int frequency = 5000;
const int pwmChannel = 0;
const int resolution = 8;
int dutyCycle = 128; // 50% duty cycle
void setup() {
ledcSetup(pwmChannel, frequency, resolution);
ledcAttachPin(ledPin, pwmChannel);
}
void loop() {
ledcWrite(pwmChannel, dutyCycle); // Adjust the PWM duty cycle
}
ADC (Analog-to-Digital Converter)
Read an analog signal on ESP32
int value = analogRead(34); // Read a value from ADC pin 34
Serial.println(value);
I2C
Configure I2C on ESP32
#include <Wire.h>
void setup() {
Wire.begin(21, 22); // SDA on pin 21, SCL on pin 22
}
void loop() {
Wire.beginTransmission(0x3C); // Start communication with device at address 0x3C
Wire.write(0x00); // Send data
Wire.endTransmission();
}
Wi-Fi Connectivity
Connect to a Wi-Fi Network
Code to connect the ESP32 to a Wi-Fi network
#include <WiFi.h>
const char* ssid = "network_name";
const char* password = "password";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}
Serial.println("Connected to Wi-Fi");
}
void loop() {
// Main code
}
Create an Access Point (AP)
Code to set up the ESP32 as a Wi-Fi access point
#include <WiFi.h>
void setup() {
WiFi.softAP("ESP32_AP", "AP_password");
Serial.begin(115200);
Serial.println("Access point configured.");
}
void loop() {
// Application logic
}
Bluetooth on ESP32
Classic Bluetooth
Enable Classic Bluetooth and pairing
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); // Bluetooth device name
Serial.println("Bluetooth ready to pair");
}
void loop() {
if (SerialBT.available()) {
char incomingChar = SerialBT.read();
Serial.write(incomingChar); // Display received data via Bluetooth
}
}
Bluetooth Low Energy (BLE)
Set up the ESP32 as a BLE server
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
void setup() {
BLEDevice::init("ESP32_BLE");
BLEServer *pServer = BLEDevice::createServer();
}
void loop() {
// Logic for BLE communication
}
Power Management
Low Power Modes
Set the ESP32 in deep sleep mode
void setup() {
esp_sleep_enable_timer_wakeup(10 * 1000000); // Wake up in 10 seconds
esp_deep_sleep_start(); // Enter deep sleep mode
}
void loop() {
// Not executed in deep sleep
}
OTA (Over-the-Air Update)
Upload Code via Wi-Fi
Set up OTA on the ESP32
#include <WiFi.h>
#include <ArduinoOTA.h>
void setup() {
WiFi.begin("network_name", "password");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
ArduinoOTA.begin();
}
void loop() {
ArduinoOTA.handle(); // Process OTA update requests
}