![]() |
DHT11 Thumb |
Xem thêm
Phần cứng cần thiết
- Arduino LCD 1602 Shield
- Cảm biến nhiệt độ – độ ẩm DHT11
- Module 2 Relay 5V
- Breadboard
- Arduino UNO R3 (hoặc tương đương)
LCD Keypad Pinout
![]() |
LCD keypad Pinout |
Kết nối với mạch Arduino
![]() |
Sơ đồ kết nối |
Thư viện
Để khởi chạy cảm biến của bạn, bạn sẽ cần cài đặt thư viện “DHT.h”. Thư viện này có sẵn từ trình quản lý thư viện Arduino.
Để cài đặt thư viện, hãy di chuyển đến Sketch > Include Library > Manage Libraries… Tìm kiếm và cài đặt thư viện DHT sensor library.
![]() |
Thư viện cảm biến nhiệt độ độ ẩm DHT |
Code
#include <LiquidCrystal.h>
#include "DHT.h"
#include <EEPROM.h>
int tempT = 0, tempH = 20;
const int DHTPIN = 3;
const int DHTTYPE = DHT11;
const int Q1 = 12;
const int Q2 = 13;
const int pin_RS = 8;
const int pin_EN = 9;
const int pin_d4 = 4;
const int pin_d5 = 5;
const int pin_d6 = 6;
const int pin_d7 = 7;
byte degree[8] = {
0B01110,
0B01010,
0B01110,
0B00000,
0B00000,
0B00000,
0B00000,
0B00000
};
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(pin_RS, pin_EN, pin_d4, pin_d5, pin_d6, pin_d7);
int page = 0, value;
int cd = 0;
char checkKeyPad()
{
int x;
x = analogRead(0);
if (x < 60)
{
return 'r';
}
else if (x < 200)
{
return 'u';
}
else if (x < 400)
{
return 'd';
}
else if (x < 600)
{
return 'l';
}
else if (x < 800)
{
return 's';
}
}
void button()
{
if (checkKeyPad() == 'l')
{
page --;
if (page > 3) page = 0;
if (page < 0) page = 3;
lcd.clear();
while (checkKeyPad() == 'l');
}
if (checkKeyPad() == 'r')
{
page ++;
if (page > 3) page = 0;
if (page < 0) page = 3;
lcd.clear();
while (checkKeyPad() == 'r');
}
}
void setup() {
lcd.createChar(1, degree);
pinMode(Q1, OUTPUT);
pinMode(Q2, OUTPUT);
lcd.begin(16, 2);
Serial.begin(9600);
dht.begin();
EEPROM.write(1, tempT);
delay(5);
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
tempT = EEPROM.read(5);
tempH = EEPROM.read(6);
if (isnan(t) || isnan(h)) {
Serial.println("DHT sensor error!");
lcd.setCursor(0, 0);
lcd.print("DHT sensor error!");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.clear();
return;
}
else {
switch (page)
{
case 0:
{
lcd.setCursor(0, 0);
lcd.print(" Thegioiic ");
lcd.setCursor(0, 1);
lcd.print("<");
lcd.setCursor(6, 1);
lcd.print("DHT11");
lcd.setCursor(15, 1);
lcd.print(">");
button();
break;
}
case 1:
{
lcd.setCursor(0, 1);
lcd.print("<");
lcd.setCursor(15, 1);
lcd.print(">");
lcd.setCursor(2, 0);
lcd.print(" T: ");
lcd.print(t);
lcd.write(1);
lcd.print("C");
lcd.setCursor(2, 1);
lcd.print(" H: ");
lcd.print(h);
lcd.print(" %");
button();
delay(500);
break;
}
case 2:
{
switch (cd)
{
case 0:
{
lcd.setCursor(0, 1);
lcd.print("<");
lcd.setCursor(15, 1);
lcd.print(">");
lcd.setCursor(5, 0);
lcd.print("Set T,H");
lcd.setCursor(1, 1);
lcd.print("T = ");
lcd.print(tempT);
lcd.setCursor(9, 1);
lcd.print("H = ");
lcd.print(tempH);
if (checkKeyPad() == 's')
{
cd++;
if (cd > 2)cd = 0;
while (checkKeyPad() == 's');
}
button();
break;
}
case 1:
{
setTemp();
if (checkKeyPad() == 'u')
{
tempT++;
if (tempT > 50)tempT = 0;
while (checkKeyPad() == 'u');
}
if (checkKeyPad() == 'd')
{
tempT--;
if (tempT < 0)tempT = 50;
while (checkKeyPad() == 'd');
}
if (checkKeyPad() == 's')
{
cd++;
if (cd > 2)cd = 0;
while (checkKeyPad() == 's');
}
EEPROM.write(5, tempT);
delay(5);
break;
}
case 2:
{
setHumi();
if (checkKeyPad() == 'u')
{
tempH++;
if (tempH > 95)tempH = 20;
while (checkKeyPad() == 'u');
}
if (checkKeyPad() == 'd')
{
tempH--;
if (tempH < 20)tempH = 95;
while (checkKeyPad() == 'd');
}
if (checkKeyPad() == 's')
{
cd++;
if (cd > 2)cd = 0;
while (checkKeyPad() == 's');
}
EEPROM.write(6, tempH);
delay(5);
break;
}
}
break;
}
case 3:
{
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(t);
lcd.write(1);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("H:");
lcd.print(h);
lcd.print(" %");
if (t > tempT) {
lcd.setCursor(10, 0);
lcd.print("Q1: CL");
digitalWrite(Q1,LOW);
} else {
lcd.setCursor(10, 0);
lcd.print("Q1: OP");
digitalWrite(Q1,HIGH);
}
if (h > tempH) {
lcd.setCursor(10, 1);
lcd.print("Q2: CL");
digitalWrite(Q2,LOW);
} else {
lcd.setCursor(10, 1);
lcd.print("Q2: OP");
digitalWrite(Q2,HIGH);
}
button();
break;
}
}
}
}
void setTemp()
{
lcd.setCursor(5, 0);
lcd.print("Set T,H");
lcd.setCursor(1, 1);
lcd.print("T = ");
lcd.print(tempT);
delay(150);
lcd.setCursor(5, 1);
lcd.print(" ");
lcd.setCursor(9, 1);
lcd.print("H = ");
lcd.print(tempH);
delay(150);
}
void setHumi()
{
lcd.setCursor(5, 0);
lcd.print("Set T,H");
lcd.setCursor(9, 1);
lcd.print("H = ");
lcd.print(tempH);
lcd.setCursor(1, 1);
lcd.print("T = ");
lcd.print(tempT);
delay(150);
lcd.setCursor(13, 1);
lcd.print(" ");
delay(150);
}
Video:
www.truongcongly.com