#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h> // Thêm thư viện DHT
#define DHTPIN 15 // Định nghĩa chân kết nối với cảm biến DHT
#define DHTTYPE DHT22 // Định nghĩa loại cảm biến DHT (DHT22)
DHT dht(DHTPIN, DHTTYPE);
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
dht.begin();
// initialize OLED display with I2C address 0x3C
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
oled.clearDisplay(); // clear display
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
delay(300);
oled.setTextSize(1); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(0, 0); // set position to display (x,y)
oled.print("nhiet do");
oled.setCursor(0, 10); // set position to display (x,y)
oled.print(t);
oled.setCursor(0,20 ); // set position to display (x,y)
oled.print("do am");
oled.setCursor(0,30); // set position to display (x,y)
oled.print(h);
oled.display();
}