วัดอุณหภูมิและความชื้นโดยใช้ M5Stack Core 2 ร่วมกับ DHT11 และ DHT22 - Robojax
คู่มือนี้สาธิตวิธีการเชื่อมต่อเซนเซอร์วัดอุณหภูมิและความชื้น DHT11 หรือ DHT22 เข้ากับ M5Stack Core2 และแสดงค่าที่อ่านได้บนหน้าจอสัมผัสในตัว M5Stack Core2 เป็นบอร์ดพัฒนาที่ใช้ชิป ESP32 ที่ทรงพลังพร้อมจอ LCD ขนาด 2 นิ้ว ทำให้เป็นแพลตฟอร์มที่เหมาะสำหรับการสร้างสถานีตรวจสอบสภาพแวดล้อมแบบสแตนด์อโลน โปรเจกต์นี้เหมาะสำหรับการใช้งานที่หลากหลาย เช่น:
- สร้างสถานีตรวจอากาศบนโต๊ะทำงานเพื่อตรวจสอบสภาพแวดล้อมในบ้านหรือสำนักงานของคุณ
- สร้างเครื่องบันทึกอุณหภูมิและความชื้นสำหรับเรือนกระจก สวนขวด หรือกรงสัตว์เลื้อยคลาน
- พัฒนาโหนดตรวจสอบสภาพแวดล้อมอย่างง่ายสำหรับโปรเจกต์ IoT ขนาดใหญ่
- เรียนรู้วิธีการเชื่อมต่อกับเซนเซอร์ตระกูล DHT ยอดนิยมโดยใช้โมดูลจอแสดงผลที่ทันสมัย
อุปกรณ์ฮาร์ดแวร์
- M5Stack Core2 (บอร์ดพัฒนา ESP32 พร้อมจอแสดงผล)
- เซนเซอร์ DHT11 (แบบเซนเซอร์เปล่าพร้อมตัวต้านทาน 10kΩ หรือโมดูล PCB)
- เซนเซอร์ DHT22 (AM2302) (แบบโมดูลสีขาวหรือโมดูลสีแดง/ดำ)
- เบรดบอร์ดและสายจัมเปอร์
คู่มือการเดินสายไฟ
การเดินสายไฟทำได้ง่าย เนื่องจาก M5Stack Core2 มีพอร์ตที่เข้ากันได้กับ Grove ให้ความสะดวก วิดีโอสาธิตการเชื่อมต่อบนพอร์ตขยายของ M5Stack Core2 (ในวิดีโอที่ 03:52)
นี่คือสรุปการเชื่อมต่อสำหรับเซนเซอร์ทุกประเภท:
- พินข้อมูล: เชื่อมต่อพินข้อมูล/เอาต์พุตของเซนเซอร์เข้ากับ GPIO 27 บน M5Stack Core2 (ในวิดีโอที่ 03:58)
- พินไฟ (VCC/+): เชื่อมต่อพินไฟของเซนเซอร์เข้ากับพิน 3.3V บน M5Stack Core2 (ในวิดีโอที่ 04:11)
- พินกราวด์ (GND/-): เชื่อมต่อพินกราวด์ของเซนเซอร์เข้ากับพิน GND บน M5Stack Core2 (ในวิดีโอที่ 04:17)
หากคุณใช้เซนเซอร์ DHT11 หรือ DHT22 แบบเปล่า คุณต้องเพิ่มตัวต้านทานแบบดึงขึ้น (โดยทั่วไปคือ 5.6kΩ ถึง 10kΩ) ระหว่างพินข้อมูลและพิน 3.3V (ในวิดีโอที่ 04:50)
คำอธิบายโค้ด
โค้ดถูกออกแบบให้ปรับแต่งได้สูง ช่วยให้คุณเปลี่ยนประเภทเซนเซอร์ โหมดการแสดงผล สี และอัตราการรีเฟรชได้อย่างง่ายดาย นี่คือรายละเอียดของส่วนหลักที่ผู้ใช้สามารถกำหนดค่าได้:
การตั้งค่าเซนเซอร์และการแสดงผล
ที่ด้านบนของโค้ด คุณสามารถกำหนดประเภทเซนเซอร์และโหมดการแสดงผลได้ ในการเลือกเซนเซอร์ของคุณ คุณต้องคอมเมนต์หรือยกเลิกคอมเมนต์บรรทัด #define ที่ถูกต้อง (ในวิดีโอที่ 13:43)
#define DHTPIN 27
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
ตัวอย่างเช่น หากต้องการใช้ DHT11 คุณต้องคอมเมนต์ #define DHTTYPE DHT22 และยกเลิกคอมเมนต์ #define DHTTYPE DHT11
ตัวแปร unit ควบคุมข้อมูลที่จะแสดงบนหน้าจอ (ในวิดีโอที่ 09:33)
unsigned int unit=3;//
// 0=C องศาเซลเซียส
// 1=F องศาฟาเรนไฮต์
// 2=ความชื้น
// 3=C และความชื้น
// 4=F และความชื้น
// 5=C และ F
คุณสามารถตั้งค่านี้เป็นค่าใดก็ได้ตั้งแต่ 0 ถึง 5 เพื่อแสดงเซลเซียส ฟาเรนไฮต์ ความชื้น หรือการรวมกันของค่าเหล่านี้
ตัวแปร refresh กำหนดความถี่ในการอ่านเซนเซอร์และอัปเดตหน้าจอ เป็นหน่วยมิลลิวินาที (ในวิดีโอที่ 09:15)
const int refresh=3000;//อ่านทุก 3 วินาที
ตัวเลือกการปรับแต่ง
โค้ดยังช่วยให้คุณปรับแต่งข้อความและสีบนหน้าจอได้ อาร์เรย์ title และ unitText เก็บป้ายกำกับและหน่วยที่แสดงบนหน้าจอ (ในวิดีโอที่ 10:01)
char *title[]={"อุณหภูมิ","อุณหภูมิ","ความชื้น"};
char *unitText[]={"C","F","%"};
คุณสามารถเปลี่ยนสตริงเหล่านี้เพื่อแสดงข้อความในภาษาของคุณเอง ตัวแปรสี เช่น backgroundColor, temperatureTitleColor และ humidityValueColor สามารถตั้งเป็นค่าใดก็ได้จากค่าสี TFT ที่กำหนดไว้ล่วงหน้าหรือค่าเลขฐานสิบหกที่กำหนดเองของคุณ (ในวิดีโอที่ 10:21)
การสาธิตโปรเจกต์สด
วิดีโอให้การสาธิตโปรเจกต์อย่างครอบคลุม โดยแสดงการทำงานของจอแสดงผลกับเซนเซอร์ DHT22 ในหลายโหมด รวมถึงฟาเรนไฮต์ ความชื้นอย่างเดียว เซลเซียสพร้อมความชื้น และสุดท้ายเซลเซียสและฟาเรนไฮต์ (ในวิดีโอที่ 21:03) การสาธิตยังรวมถึงการทดสอบกับเซนเซอร์ DHT11 ทั้งแบบโมดูล PCB และแบบเซนเซอร์เปล่าพร้อมตัวต้านทานแบบดึงขึ้น (ในวิดีโอที่ 22:22) สุดท้าย วิดีโอแสดงสิ่งที่เกิดขึ้นเมื่อถอดสายไฟออก ซึ่งส่งผลให้หน้าจอแสดงค่า "None" (ในวิดีโอที่ 22:38)
บทของวิดีโอ
- [00:00] บทนำและภาพรวมโปรเจกต์
- [00:55] คุณสมบัติของ M5Stack Core2
- [02:22] ข้อมูลจำเพาะของเซนเซอร์ DHT11 เทียบกับ DHT22
- [03:52] คู่มือการเดินสายไฟ
- [07:40] การติดตั้งไดรเวอร์บอร์ดและไลบรารี
- [09:01] คำอธิบายโค้ด
- [21:03] การสาธิตสด
- [22:53] บทสรุปและปิดท้าย
/*
* 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
แหล่งข้อมูลและเอกสารอ้างอิง
-
ภายนอกM5Stack Core2 Documentation Pagedocs.m5stack.com
-
ภายนอกM5Stack Core2 Product Pagem5stack-store.myshopify.com
ไฟล์📁
ไม่มีไฟล์ที่พร้อมใช้งาน