// Thêm bộ thư viện LCD.
// Add the LCD library.
#include <LiquidCrystal_I2C.h>
// Thêm bộ thư viện cảm biến DS18B20.
// Add the DS18B20 sensor library.
#include <OneWire.h>
#include <DallasTemperature.h>
// Chọn chân đọc cảm biến.
// Select the pin to read the sensor.
#define SIG_PIN 9
// Cấu hình chân kết nối tín hiệu cho cảm biến DS18B20.
// Configure the signal connection pins for the DS18B20 sensor.
OneWire oneWire(SIG_PIN);
DallasTemperature ds(&oneWire);
// Chọn chân Digital điều khiển Còi.
// Select the Digital pin to control Buzzer.
#define BUZ_PIN 10
// Khởi tạo "LiquidCrystal_I2C" cho màn hình LCD với cấu hình sau.
// Initialize "LiquidCrystal_I2C" for the monitor LCD with the following config.
LiquidCrystal_I2C lcd(
0x27, // Cài đặt địa chỉ I2C là 0x27.
16, // With 16 columns.
2 // With 2 rows.
);
void setup()
{
// Khởi động kết nối Serial UART ở tốc độ 9600 để truyền dữ liệu lên máy tính.
// Start the Serial UART connection at 9600 to transfer data to the computer.
// Serial.begin(200);
// Serial.begin(38400);
Serial.begin(9600);
// Khởi động thư viện.
// Start up the library.
ds.begin();
// Khởi động thư viện LCD.
// Start up the LCD library.
lcd.init();
// Xóa màn hình, đảm bảo không còn nội dung cũ trước đó.
// Clear the screen, making sure there is no old content left before.
lcd.clear();
// Bật đèn nền màn hình.
// Turn on the screen backlight.
lcd.backlight();
// Tại vị trí cột 1 dòng 1, cho in nội dung...
// At column 1, row 1, print the content...
lcd.setCursor(0, 0);
lcd.print(" DAI HOC MO HN ");
// Tại vị trí cột 1 dòng 2, cho in nội dung...
// At column 1, row 2, print the content...
lcd.setCursor(0, 1);
lcd.print(" KHOA CNTT ");
// Chờ 5s.
// Wait 5s.
delay(5000);
// Xóa màn hình.
// Clear the screen.
lcd.clear();
// Cấu hình đây là chân Digital Output.
// Config this is Digital Output.
pinMode(BUZ_PIN, OUTPUT);
// Đảm bảo tắt Còi khi mới khởi động.
// Make sure to turn off the Buzzer when starting.
digitalWrite(BUZ_PIN, LOW);
}
void loop()
{
// Đo nhiệt độ từ cảm biến DS18B20.
// Measure temperature from DS18B20 sensor.
ds.requestTemperatures();
float temperatureC = ds.getTempCByIndex(0);
Serial.print(temperatureC);
Serial.print("|");
// Nếu nhiệt độ vượt quá 50 độ C, bật còi báo động.
// If temperature exceeds 50°C, turn on the alarm buzzer.
if (temperatureC > 50.0)
{
// Hiển thị "High Temp" và bật còi cảnh báo.
// Display "High Temp" and turn on the alarm buzzer.
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperatureC);
lcd.print((char)223); // Ký tự độ (°)
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("High Temp");
// Bật Còi trong 0,5s.
// Turn on Buzzer for 0,5 seconds.
digitalWrite(BUZ_PIN, HIGH);
delay(300);
// Tắt Còi trong 0,5s.
// Turn off Buzzer for 0,5 seconds.
digitalWrite(BUZ_PIN, LOW);
delay(300);
}
else if (temperatureC < 20.0)
{
// Hiển thị "Low Temp" và bật còi cảnh báo.
// Display "Low Temp" and turn on the alarm buzzer.
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperatureC);
lcd.print((char)223); // Ký tự độ (°)
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Low Temp");
// Bật Còi trong 0,5s.
// Turn on Buzzer for 0,5 seconds.
digitalWrite(BUZ_PIN, HIGH);
delay(500);
// Tắt Còi trong 0,5s.
// Turn off Buzzer for 0,5 seconds.
digitalWrite(BUZ_PIN, LOW);
delay(500);
}
else
{
// Hiển thị nhiệt độ và tắt còi cảnh báo.
// Display the temperature and turn off the alarm buzzer.
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperatureC);
lcd.print((char)223); // Ký tự độ (°)
lcd.print("C");
digitalWrite(BUZ_PIN, LOW);
}
// Chờ 0.5s mới đo lại.
// Wait 0.5s to measure again.
delay(500);
}
Loading
ds18b20
ds18b20