#include <DHT.h>
#define DHTPIN 5 // Chân cảm biến DHT22
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define LED_PIN 4 // Chân GPIO điều khiển quạt (hoặc LED giả quạt)
void setup() {
Serial.begin(115200); // Bắt đầu Serial Monitor với tốc độ 115200
pinMode(LED_PIN, OUTPUT); // Đặt chân LED làm OUTPUT
dht.begin(); // Khởi động cảm biến DHT22
}
void loop() {
float temperature = dht.readTemperature(); // Đọc nhiệt độ từ cảm biến
if (isnan(temperature)) {
Serial.println("Failed to read temperature");
return;
}
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
// Điều chỉnh tốc độ quạt (hoặc độ sáng LED giả quạt) dựa trên nhiệt độ
int pwmValue = 0; // Khởi tạo giá trị PWM mặc định là 0
if (temperature < 20) {
pwmValue = 0; // Đèn tắt
}
else if (temperature >= 20 && temperature <= 25) {
pwmValue = 85; // Quạt cấp 1
}
else if (temperature > 25 && temperature <= 31) {
pwmValue = 170; // Quạt cấp 2
}
else if (temperature > 31) {
pwmValue = 255; // Quạt cấp 3 (mạnh nhất)
}
// Điều khiển PWM với giá trị pwmValue
analogWrite(LED_PIN, pwmValue);
// Hiển thị giá trị PWM và độ sáng đèn lên Serial Monitor
Serial.print("PWM Value: ");
Serial.println(pwmValue);
Serial.print("LED Brightness (0-255): ");
Serial.println(pwmValue); // Đây là độ sáng LED hoặc tốc độ quạt
delay(1000); // Đọc lại nhiệt độ sau mỗi giây
}