#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// LCD I2C (địa chỉ thường là 0x27 hoặc 0x3F)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Chân DS18B20
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
// Khởi tạo I2C cho LCD
Wire.begin(21, 22); // SDA = 21, SCL = 22
lcd.init();
lcd.backlight();
// Khởi tạo DS18B20
sensors.begin();
lcd.setCursor(0, 0);
lcd.print("Khoi dong...");
delay(1000);
}
void loop() {
sensors.requestTemperatures(); // Gửi lệnh đo
float tempC = sensors.getTempCByIndex(0); // Lấy nhiệt độ °C
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Nhiet do:");
lcd.setCursor(0, 1);
lcd.print(tempC, 1); // 1 số thập phân
lcd.print((char)223);
lcd.print("C");
delay(1000);
}