🏠 Dự Án 1: Hệ Thống Nhà Thông Minh
Mô tả: Điều khiển đèn, quạt, đo nhiệt độ/độ ẩm qua web hoặc app Blynk.
Linh kiện cần:
- ESP32 DevKit
- DHT11/DHT22 - Cảm biến nhiệt độ độ ẩm
- Relay module 2 kênh
- Bóng đèn/quạt 220V
- Breadboard + dây jumper
Smart Home - Code hoàn chỉnh
#include <WiFi.h> #include <WebServer.h> #include <DHT.h> // WiFi const char* ssid = "TenWiFi"; const char* password = "MatKhau"; // DHT Sensor #define DHTPIN 15 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); // Relay const int RELAY1 = 23; // Đèn const int RELAY2 = 22; // Quạt bool light = false; bool fan = false; WebServer server(80); String getHTML() { float temp = dht.readTemperature(); float humid = dht.readHumidity(); String html = "<!DOCTYPE html><html lang='vi'>"; html += "<head><meta charset='UTF-8'>"; html += "<meta name='viewport' content='width=device-width, initial-scale=1.0'>"; html += "<title>Nhà Thông Minh</title>"; html += "<style>"; html += "body{font-family:Arial;background:#f0f0f0;margin:0;padding:20px;}"; html += ".container{max-width:600px;margin:0 auto;background:white;padding:30px;border-radius:10px;box-shadow:0 2px 10px rgba(0,0,0,0.1);}"; html += "h1{color:#00979D;text-align:center;}"; html += ".sensor{background:#e8f4f8;padding:20px;border-radius:8px;margin:20px 0;}"; html += ".control{display:flex;justify-content:space-between;margin:15px 0;}"; html += "button{padding:15px 30px;font-size:16px;border:none;border-radius:5px;cursor:pointer;}"; html += ".on{background:#4CAF50;color:white;}.off{background:#f44336;color:white;}"; html += "</style></head><body>"; html += "<div class='container'>"; html += "<h1>🏠 HỆ THỐNG NHÀ THÔNG MINH</h1>"; // Hiển thị cảm biến html += "<div class='sensor'>"; html += "<h3>📊 Thông Số Môi Trường</h3>"; html += "<p>🌡️ Nhiệt độ: <b>" + String(temp, 1) + "°C</b></p>"; html += "<p>💧 Độ ẩm: <b>" + String(humid, 1) + "%</b></p>"; html += "</div>"; // Điều khiển đèn html += "<div class='control'>"; html += "<span>💡 Đèn: <b>" + String(light ? "BẬT" : "TẮT") + "</b></span>"; html += "<button class='" + String(light ? "off" : "on") + "' onclick=\"location.href='/light'\">"; html += light ? "TẮT" : "BẬT"; html += "</button></div>"; // Điều khiển quạt html += "<div class='control'>"; html += "<span>🌀 Quạt: <b>" + String(fan ? "BẬT" : "TẮT") + "</b></span>"; html += "<button class='" + String(fan ? "off" : "on") + "' onclick=\"location.href='/fan'\">"; html += fan ? "TẮT" : "BẬT"; html += "</button></div>"; html += "<script>setTimeout(function(){location.reload();},5000);</script>"; html += "</div></body></html>"; return html; } void setup() { Serial.begin(115200); pinMode(RELAY1, OUTPUT); pinMode(RELAY2, OUTPUT); digitalWrite(RELAY1, LOW); digitalWrite(RELAY2, LOW); dht.begin(); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nĐã kết nối! IP: " + WiFi.localIP().toString()); server.on("/", []() { server.send(200, "text/html", getHTML()); }); server.on("/light", []() { light = !light; digitalWrite(RELAY1, light); server.sendHeader("Location", "/"); server.send(303); }); server.on("/fan", []() { fan = !fan; digitalWrite(RELAY2, fan); server.sendHeader("Location", "/"); server.send(303); }); server.begin(); } void loop() { server.handleClient(); }
🤖 Dự Án 2: Robot Tránh Vật Cản
Mô tả: Robot 2 bánh tự động di chuyển và tránh vật cản bằng cảm biến siêu âm.
Linh kiện cần:
- Arduino Uno hoặc ESP32
- L298N Motor Driver
- 2x Động cơ DC + bánh xe
- HC-SR04 Ultrasonic Sensor
- Pin 12V + holder pin
- Khung xe robot
Robot Obstacle Avoidance
// Motor trái const int ENA = 9; const int IN1 = 7; const int IN2 = 8; // Motor phải const int ENB = 10; const int IN3 = 5; const int IN4 = 6; // Cảm biến siêu âm const int TRIG = 12; const int ECHO = 11; const int SPEED = 180; const int SAFE_DISTANCE = 20; // cm void setup() { Serial.begin(9600); pinMode(ENA, OUTPUT); pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(ENB, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT); pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT); Serial.println("Robot đã sẵn sàng!"); } void loop() { long distance = getDistance(); Serial.print("Khoảng cách: "); Serial.print(distance); Serial.println(" cm"); if (distance > SAFE_DISTANCE) { moveForward(); } else { stopRobot(); delay(300); moveBackward(); delay(500); stopRobot(); delay(200); // Quay phải hoặc trái ngẫu nhiên if (random(2) == 0) { turnRight(); } else { turnLeft(); } delay(600); } delay(50); } long getDistance() { digitalWrite(TRIG, LOW); delayMicroseconds(2); digitalWrite(TRIG, HIGH); delayMicroseconds(10); digitalWrite(TRIG, LOW); long duration = pulseIn(ECHO, HIGH); long distance = duration * 0.034 / 2; return distance; } void moveForward() { digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); analogWrite(ENA, SPEED); digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); analogWrite(ENB, SPEED); } void moveBackward() { digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); analogWrite(ENA, SPEED); digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH); analogWrite(ENB, SPEED); } void turnRight() { digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); analogWrite(ENA, SPEED); digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH); analogWrite(ENB, SPEED); } void turnLeft() { digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); analogWrite(ENA, SPEED); digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); analogWrite(ENB, SPEED); } void stopRobot() { digitalWrite(IN1, LOW); digitalWrite(IN2, LOW); analogWrite(ENA, 0); digitalWrite(IN3, LOW); digitalWrite(IN4, LOW); analogWrite(ENB, 0); }
🌡️ Dự Án 3: Trạm Thời Tiết IoT
Mô tả: Đo nhiệt độ, độ ẩm, áp suất và gửi dữ liệu lên cloud (ThingSpeak), hiển thị trên OLED.
Linh kiện:
- ESP32
- BME280 (nhiệt độ, độ ẩm, áp suất) qua I2C
- OLED 0.96" I2C
- Pin hoặc nguồn 5V
💡 Hướng dẫn:
- Tạo tài khoản ThingSpeak miễn phí
- Tạo Channel mới với 3 field: Temperature, Humidity, Pressure
- Lấy Write API Key
- Cài thư viện: Adafruit BME280, Adafruit SSD1306, ThingSpeak
📚 Thêm Ý Tưởng Dự Án
🚪 Khóa Cửa Thông Minh
RFID + Servo Motor + Blynk notification
🌱 Hệ Thống Tưới Cây Tự Động
Cảm biến độ ẩm đất + Relay + Bơm nước
🔔 Chuông Cửa Thông Minh
ESP32-CAM + Face Recognition + Telegram Bot
📊 Đồng Hồ Hiển Thị Đa Năng
RTC DS3231 + OLED + Weather API
🎮 Game Console Mini
ESP32 + OLED + Nút nhấn + Buzzer
🚨 Hệ Thống Báo Động
PIR Motion + Reed Switch + GSM Module
🎓 Lời Khuyên Khi Làm Dự Án
- Bắt đầu đơn giản: Test từng module riêng lẻ trước khi kết hợp
- Sơ đồ mạch: Vẽ sơ đồ kết nối trước khi đấu dây
- Debug từng bước: Dùng Serial Monitor để kiểm tra giá trị
- Quản lý nguồn: Đảm bảo nguồn đủ mạnh cho tất cả module
- Code module hóa: Chia thành các hàm nhỏ, dễ debug
- Ghi chú code: Comment giải thích logic, giúp sau này dễ hiểu
- Backup thường xuyên: Lưu nhiều phiên bản code