ESP32-WROOM-32
Module Wi‑Fi + Bluetooth phổ biến nhất cho IoT, dựa trên chip ESP32‑D0WD. Dual‑core Xtensa LX6 240 MHz, 4 MB flash, 520 KB SRAM, 34 GPIOs.
📊 Thông số kỹ thuật
Specifications
| MCU | Xtensa dual-core 32-bit LX6 |
| Tần số | 240 MHz |
| Flash | 4 MB |
| PSRAM | — (optional) |
| SRAM | 520 KB |
| Wi‑Fi | 802.11 b/g/n (2.4 GHz) |
| Bluetooth | BLE 4.2 + Classic |
| GPIO | 34 |
| ADC | 18 channels (12‑bit) |
| DAC | 2 channels (8‑bit) |
| UART | 3 |
| SPI | 4 |
| I2C | 2 |
| I2S | 2 |
| PWM | 16 channels |
| Touch sensor | 10 |
| Điện áp hoạt động | 3.0 ~ 3.6 V |
| Dòng tiêu thụ | 80 (avg) mA |
| Deep sleep | ~10 µA |
| Nhiệt độ hoạt động | -40 ~ +85 °C |
| Kích thước | 18 × 25.5 × 3.1 mm |
📐 Sơ đồ chân
ESP32-WROOM-32 — Sơ đồ chân
Click/tap vào pin để xem chi tiếtESP32-WROOM-32
💻 Code mẫu ESP32
1. Blink LED (GPIO2)
Blink.ino
#define LED_PIN 2 // LED trên board
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
Serial.println("LED ON");
delay(1000);
digitalWrite(LED_PIN, LOW);
Serial.println("LED OFF");
delay(1000);
}
2. Đọc ADC (GPIO36)
ADC_Read.ino
const int ADC_PIN = 36; // GPIO36 = ADC1_CH0
void setup() {
Serial.begin(115200);
analogReadResolution(12); // 12-bit (0-4095)
}
void loop() {
int adcValue = analogRead(ADC_PIN);
float voltage = adcValue * (3.3 / 4095.0);
Serial.printf("ADC: %d, Voltage: %.2f V\n", adcValue, voltage);
delay(500);
}
3. PWM điều khiển LED
PWM_LED.ino
const int LED_PIN = 26; // GPIO26 có DAC
const int PWM_CHANNEL = 0;
const int PWM_FREQ = 5000; // 5 kHz
const int PWM_RESOLUTION = 8; // 8-bit (0-255)
void setup() {
ledcSetup(PWM_CHANNEL, PWM_FREQ, PWM_RESOLUTION);
ledcAttachPin(LED_PIN, PWM_CHANNEL);
}
void loop() {
for (int duty = 0; duty <= 255; duty++) {
ledcWrite(PWM_CHANNEL, duty);
delay(10);
}
for (int duty = 255; duty >= 0; duty--) {
ledcWrite(PWM_CHANNEL, duty);
delay(10);
}
}
4. I2C scan (OLED, sensor)
I2C_Scan.ino
#include <Wire.h>
void setup() {
Serial.begin(115200);
Wire.begin(21, 22); // SDA=GPIO21, SCL=GPIO22
}
void loop() {
byte error, address;
int nDevices = 0;
Serial.println("Scanning I2C...");
for (address = 1; address < 127; address++) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.printf("Found device at 0x%02X\n", address);
nDevices++;
}
}
if (nDevices == 0) Serial.println("No I2C devices found");
delay(5000);
}
5. Touch sensor (GPIO32)
Touch_Sensor.ino
const int TOUCH_PIN = 32; // Touch9
void setup() {
Serial.begin(115200);
delay(1000);
}
void loop() {
int touchValue = touchRead(TOUCH_PIN);
Serial.printf("Touch value: %d\n", touchValue);
if (touchValue < 30) {
Serial.println("TOUCHED!");
}
delay(200);
}
6. Wi‑Fi client
WiFi_Client.ino
#include <WiFi.h>
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected! IP: " + WiFi.localIP());
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Wi‑Fi OK");
} else {
Serial.println("Wi‑Fi lost");
}
delay(10000);
}
7. Deep Sleep (10µA)
Deep_Sleep.ino
#define uS_TO_S_FACTOR 1000000
#define TIME_TO_SLEEP 10 // sleep 10s
void setup() {
Serial.begin(115200);
Serial.println("ESP32 going to sleep for 10 seconds");
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
esp_deep_sleep_start();
}
void loop() {
// Never reached
}
8. BLE Server
BLE_Server.ino
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
void setup() {
Serial.begin(115200);
BLEDevice::init("ESP32_BLE");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setValue("Hello from ESP32");
pService->start();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->start();
Serial.println("BLE Server started");
}
void loop() {
delay(2000);
}
🔗 Kết nối với linh kiện
OLED I2C (0.96", SSD1306)
Chân kết nối:
- VCC → ESP32 3V3
- GND → ESP32 GND
- SCL → GPIO22
- SDA → GPIO21
Code:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(128, 64, &Wire, -1);
void setup() {
Wire.begin(21, 22); // SDA, SCL
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Hello CoreBox!");
display.display();
}
void loop() {}
Sản phẩm: OLED 0.96" I2C
DHT11 (Temperature/Humidity)
Chân kết nối:
- VCC → ESP32 3V3
- GND → ESP32 GND
- DATA → GPIO4 (có thể dùng GPIO khác)
Code:
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
Serial.printf("Temp: %.1f°C, Humidity: %.1f%%\n", t, h);
delay(2000);
}
Sản phẩm: DHT11
Relay 5V (2 kênh)
Chân kết nối:
- VCC → ESP32 5V (từ USB)
- GND → ESP32 GND
- IN1 → GPIO23
- IN2 → GPIO22
Code:
#define RELAY1 23
#define RELAY2 22
void setup() {
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
}
void loop() {
digitalWrite(RELAY1, HIGH); delay(1000);
digitalWrite(RELAY1, LOW); delay(1000);
digitalWrite(RELAY2, HIGH); delay(1000);
digitalWrite(RELAY2, LOW); delay(1000);
}
Sản phẩm: Relay Module 5V 2CH
HC‑SR04 Ultrasonic
Chân kết nối:
- VCC → ESP32 5V
- GND → ESP32 GND
- TRIG → GPIO18
- ECHO → GPIO19
Code:
#define TRIG 18
#define ECHO 19
void setup() {
Serial.begin(115200);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
}
void loop() {
digitalWrite(TRIG, LOW); delayMicroseconds(2);
digitalWrite(TRIG, HIGH); delayMicroseconds(10);
digitalWrite(TRIG, LOW);
long duration = pulseIn(ECHO, HIGH);
float distance = duration * 0.034 / 2;
Serial.printf("Distance: %.2f cm\n", distance);
delay(500);
}
Sản phẩm: HC‑SR04 Ultrasonic Sensor
📖 Tài liệu tham khảo
- ESP32 Datasheet (Espressif)
- Arduino‑ESP32 GitHub — Thư viện chính thức
- ESP‑IDF Programming Guide
- GPIO Basics (ESP32) — Cơ bản về GPIO, pull‑up/pull‑down