Đo Nhiệt Độ và Độ Ẩm sử dụng M5Stack Core 2 với DHT11 và DHT22 - Robojax
Hướng dẫn này trình bày cách kết nối cảm biến nhiệt độ và độ ẩm DHT11 hoặc DHT22 với M5Stack Core2 và hiển thị số liệu trên màn hình cảm ứng tích hợp. M5Stack Core2 là một bo mạch phát triển mạnh mẽ dựa trên ESP32 với màn hình LCD 2 inch, là nền tảng lý tưởng để tạo một trạm giám sát môi trường độc lập. Dự án này phù hợp cho nhiều ứng dụng khác nhau, chẳng hạn như:
- Xây dựng trạm thời tiết để bàn để theo dõi môi trường tại nhà hoặc văn phòng của bạn.
- Tạo thiết bị ghi nhiệt độ và độ ẩm cho nhà kính, hồ cạn hoặc chuồng nuôi bò sát.
- Phát triển một nút giám sát môi trường đơn giản cho một dự án IoT lớn hơn.
- Học cách giao tiếp với dòng cảm biến DHT phổ biến bằng mô-đun hiển thị hiện đại.
Linh kiện phần cứng
- M5Stack Core2 (bo mạch phát triển ESP32 có màn hình)
- Cảm biến DHT11 (cảm biến trần kèm điện trở 10kΩ hoặc mô-đun PCB)
- Cảm biến DHT22 (AM2302) (mô-đun màu trắng hoặc mô-đun đỏ/đen)
- Breadboard và dây jumper
Hướng dẫn đấu dây
Việc đấu dây khá đơn giản vì M5Stack Core2 cung cấp cổng tương thích Grove tiện lợi. Video minh họa kết nối trên cổng mở rộng của M5Stack Core2. (trong video tại 03:52)
Dưới đây là tóm tắt kết nối cho tất cả các loại cảm biến:
- Chân dữ liệu: Kết nối chân dữ liệu/out của cảm biến với GPIO 27 trên M5Stack Core2. (trong video tại 03:58)
- Chân nguồn (VCC/+): Kết nối chân nguồn của cảm biến với chân 3.3V trên M5Stack Core2. (trong video tại 04:11)
- Chân đất (GND/-): Kết nối chân đất của cảm biến với chân GND trên M5Stack Core2. (trong video tại 04:17)
Nếu bạn sử dụng cảm biến DHT11 hoặc DHT22 trần, bạn phải thêm một điện trở kéo lên (thường từ 5.6kΩ đến 10kΩ) giữa chân dữ liệu và chân 3.3V. (trong video tại 04:50)
Giải thích mã
Mã được thiết kế để có thể cấu hình cao, cho phép bạn dễ dàng thay đổi loại cảm biến, chế độ hiển thị, màu sắc và tốc độ làm mới. Dưới đây là phân tích các phần chính mà người dùng có thể cấu hình:
Cài đặt cảm biến và màn hình
Ở đầu mã, bạn có thể xác định loại cảm biến và chế độ hiển thị. Để chọn cảm biến của bạn, bạn phải comment hoặc bỏ comment dòng #define chính xác. (trong video tại 13:43)
#define DHTPIN 27
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
Ví dụ: để sử dụng DHT11, bạn sẽ comment dòng #define DHTTYPE DHT22 và bỏ comment dòng #define DHTTYPE DHT11.
Biến unit kiểm soát dữ liệu nào được hiển thị trên màn hình. (trong video tại 09:33)
unsigned int unit=3;//
// 0=C độ C
// 1=F độ F
// 2=Độ ẩm
// 3=C và độ ẩm
// 4=F và độ ẩm
// 5=C và F
Bạn có thể đặt giá trị này từ 0 đến 5 để hiển thị độ C, độ F, độ ẩm hoặc kết hợp chúng.
Biến refresh đặt tần suất đọc cảm biến và cập nhật màn hình, tính bằng mili giây. (trong video tại 09:15)
const int refresh=3000;//đọc mỗi 3 giây
Tùy chọn tùy chỉnh
Mã cũng cho phép bạn tùy chỉnh văn bản và màu sắc trên màn hình. Các mảng title và unitText lưu trữ nhãn và đơn vị hiển thị trên màn hình. (trong video tại 10:01)
char *title[]={"Nhiệt độ","Nhiệt độ","Độ ẩm"};
char *unitText[]={"C","F","%"};
Bạn có thể thay đổi các chuỗi này để hiển thị văn bản bằng ngôn ngữ của riêng bạn. Các biến màu như backgroundColor, temperatureTitleColor và humidityValueColor có thể được đặt thành bất kỳ giá trị màu TFT được xác định trước nào hoặc giá trị hex tùy chỉnh của riêng bạn. (trong video tại 10:21)
Trình diễn dự án trực tiếp
Video cung cấp một trình diễn toàn diện về dự án. Nó cho thấy màn hình hoạt động với cảm biến DHT22 ở nhiều chế độ khác nhau, bao gồm độ F, chỉ độ ẩm, độ C kèm độ ẩm và cuối cùng là độ C và độ F. (trong video tại 21:03) Trình diễn cũng bao gồm thử nghiệm với cảm biến DHT11, cả với mô-đun PCB và cảm biến trần có điện trở kéo lên. (trong video tại 22:22) Cuối cùng, video cho thấy điều gì xảy ra khi một dây bị ngắt kết nối, dẫn đến chỉ số "None" trên màn hình. (trong video tại 22:38)
Chương mục video
- [00:00] Giới thiệu và Tổng quan dự án
- [00:55] Tính năng M5Stack Core2
- [02:22] Thông số kỹ thuật cảm biến DHT11 so với DHT22
- [03:52] Hướng dẫn đấu dây
- [07:40] Cài đặt trình điều khiển bo mạch và thư viện
- [09:01] Giải thích mã
- [21:03] Trình diễn trực tiếp
- [22:53] Kết luận và Tổng kết
/*
* file: M5Stack_DHT11_DHT22
Measure Temperature and Humidity using DHT11, DHT22, DHT21 Sensor and display on M5Stack Core 2 ESP32 IoT board
* Written by Ahmad Shamshiri on April 22, 2021 in Ontario, Canada
* Watch video instruction https://youtu.be/ZDZm_QOlCJo
www.Robojax.com YouTube http://www.youtube.com/robojaxTV
*
*
* This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact. *
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <M5Core2.h>
const int refresh=3000;//read every 3 seconds
unsigned int unit=3;//
// 0=C celsius
// 1=F fahrenheit
// 2=Humidity
// 3=C and Humidity
// 4=F and Humidity
// 5=C and F
char *title[]={"Temperature","Temperature","Humidity"};
char *unitText[]={"C","F","%"};
//set more color. see https://docs.m5stack.com/#/en/api/lcd
unsigned int backgroundColor= YELLOW;
unsigned int temperatureTitleColor= BLACK;
unsigned int temperatureValueColor= RED;
unsigned int humidityTitleColor= BLACK;
unsigned int humidityValueColor= RED;
unsigned int background2Color= TFT_CYAN;
uint8_t titleV,unitV,degree;
uint8_t line1TitleY=3;
uint8_t line1ValueY=40;
uint8_t line2TitleY=110;
uint8_t line2ValueY=150;
#include "DHT.h"
#define DHTPIN 27
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
float temperatureCValue,temperatureFValue, humidityValue,myValue1,myValue2;//
// ****** DHT settings end (Robojax.com )
void readTemperature();
void displayTemperature(uint8_t line );
void setup() {
M5.begin();
M5.Lcd.setTextSize(4);
Serial.begin(9600);//default is 115200
Serial.println("M5Stack DHT Test ");
// Robojax.com code for ESP32 DHT11 DHT22
dht.begin();
delay(1000);
}
void loop() {
readTemperature();//reads the temperature and humidity
displayMyValue();//displays temperature or humidity
//printOnSerial();
delay(refresh);
}
/*
* www.robojax.com
* see video https://youtu.be/ZDZm_QOlCJo
* for details
*/
void readTemperature()
{
temperatureCValue = dht.readTemperature();// Read temperature as Celsius (the default)
humidityValue = dht.readHumidity();// Reading humidity
temperatureFValue = dht.readTemperature(true);// Read temperature as Fahrenheit (isFahrenheit = true)
}//readTemperature() end
void displayMyValue()
{
switch (unit) {
case 1:
titleV = 1;
unitV =1;
myValue1= temperatureFValue;
displayTemperature( 1 , true);
break;
case 2:
titleV = 2;
unitV =2;
myValue1= humidityValue;
displayTemperature( 1, false );
break;
case 3:
titleV = 0;
unitV =0;
myValue1= temperatureCValue;
displayTemperature( 1 , true);
titleV = 2;
unitV =2;
myValue2= humidityValue;
displayTemperature( 2, false );
break;
case 4:
titleV = 1;
unitV =1;
myValue1= temperatureFValue;
displayTemperature( 1 , true);
titleV = 2;
unitV =2;
myValue2= humidityValue;
displayTemperature( 2 , false);
break;
case 5:
titleV = 0;
unitV =0;
myValue1= temperatureCValue;
displayTemperature( 1, true);
titleV = 1;
unitV =1;
myValue2= temperatureFValue;
displayTemperature( 2 , true);
break;
default:
titleV = 0;
unitV =0;
myValue1= temperatureCValue;
displayTemperature(1, true);
break;
}
}//displayMyValue() end
/*
* displayTemperature(uint8_t line, bool degree)
* Displays temperature on Core2 screen
* See video for details
* https://youtu.be/ZDZm_QOlCJo
*/
void displayTemperature(uint8_t line, bool degree)
{
uint8_t Y_lineTitle,Y_lineValue;
float valueToDisplay;
//M5.Lcd.fillScreen(backgroundColor);
M5.Lcd.setTextColor(temperatureTitleColor);
M5.Lcd.setTextSize(4);
if(line ==1){
Y_lineTitle =line1TitleY;
Y_lineValue =line1ValueY;
valueToDisplay=myValue1;
//M5.Lcd.fillRect(0, 0, 340, line2TitleY, backgroundColor);
M5.Lcd.fillScreen(backgroundColor);
}else{
//M5.Lcd.drawRect(0, line2TitleY, 340, 240-line2TitleY, background2Color);
M5.Lcd.fillRect(0, line2TitleY, 340, 240-line2TitleY, background2Color);
Y_lineTitle =line2TitleY;
Y_lineValue =line2ValueY;
valueToDisplay=myValue2;
}
M5.Lcd.setCursor(3,Y_lineTitle);
M5.Lcd.print(title[titleV]);
M5.Lcd.setTextColor(temperatureValueColor);
M5.Lcd.setTextSize(7);
M5.Lcd.setCursor(3,Y_lineValue);
M5.Lcd.printf("%.1f", valueToDisplay);
if(degree){
printDegree();//print degree symbol if needed
}
M5.Lcd.print(unitText[unitV]);
}
/*
* displayHumidity()
* displays humidity from DHT11 or DHT22 on Core 2
* written by Ahmad Shamshiri
* on April 21, 2021 in Ajax, Ontario, Canada
* www.robojax.com
*/
void displayHumidity()
{
M5.Lcd.setTextColor(humidityTitleColor);
M5.Lcd.setTextSize(4);
M5.Lcd.setCursor(3,110);
M5.Lcd.print(title[2]); //humidity title
M5.Lcd.setTextColor(humidityValueColor);
M5.Lcd.setTextSize(7);
M5.Lcd.setCursor(3,150);
M5.Lcd.printf("%.1f", humidityValue);
M5.Lcd.print(unitText[2]);
}//displayHumidity() end
/*
* printDegree()
* prints degree symbol
* Written by Ahmad Shamshiri
* on April 2, 2021 at 23:53
* www.robojax.com
*/
void printDegree()
{
M5.Lcd.setTextSize(3);
M5.Lcd.print("o");
M5.Lcd.setTextSize(7);
M5.Lcd.setCursor(M5.Lcd.getCursorX()+15,M5.Lcd.getCursorY());
}//printDegree() end
/*
written by Ahmad Shamshiri for Robojax, Robojax.com
on April 21, 2021 in Ajax, Ontario, Canada
*/
void printOnSerial()
{
Serial.print(title[unit]);
Serial.print(" ");
Serial.print(myValue1);
Serial.print(unitText[unit]);
Serial.println();
}//printOnSerial() end
Tài nguyên & tài liệu tham khảo
-
Bên ngoàiM5Stack Core2 Documentation Pagedocs.m5stack.com
-
Bên ngoàiM5Stack Core2 Product Pagem5stack-store.myshopify.com
Tập tin📁
Không có tệp nào khả dụng.