โค้ด Arduino และวิดีโอสำหรับเซนเซอร์วัดอุณหภูมิและความชื้น DHT22
ในบทช่วยสอนนี้ เราจะเรียนรู้วิธีการใช้เซนเซอร์วัดอุณหภูมิและความชื้น DHT22 ร่วมกับ Arduino เซนเซอร์ DHT22 หรือที่รู้จักในชื่อ AM2302 สามารถวัดอุณหภูมิได้ตั้งแต่ -40 ถึง 80 องศาเซลเซียส และความชื้นตั้งแต่ 0 ถึง 99 เปอร์เซ็นต์ โดยการทำตามคำแนะนำนี้ คุณจะสามารถแสดงอุณหภูมิในหน่วยเซลเซียส ฟาเรนไฮต์ หรือเคลวิน รวมถึงระดับความชื้นได้

เราจะใช้ไลบรารีเซนเซอร์ DHT เพื่ออ่านข้อมูลจากเซนเซอร์ได้อย่างง่ายดาย ไลบรารีนี้ช่วยลดความซับซ้อนในการสื่อสารกับ DHT22 และช่วยให้เราสามารถเข้าถึงค่าอุณหภูมิและความชื้นได้ด้วยโค้ดเพียงไม่กี่บรรทัด สำหรับคำอธิบายเชิงลึกเพิ่มเติม ฉันแนะนำให้คุณชมวิดีโอที่เกี่ยวข้อง (ในวิดีโอที่ 00:00)
อธิบายฮาร์ดแวร์
ส่วนประกอบหลักในโปรเจกต์นี้คือโมดูลเซนเซอร์ DHT22 ซึ่งเป็นเซนเซอร์วัดอุณหภูมิและความชื้นแบบดิจิทัล เซนเซอร์นี้ใช้องค์ประกอบตรวจจับความชื้นแบบ capacitive และเทอร์มิสเตอร์ในการวัดอากาศโดยรอบ ผลลัพธ์ที่ได้คือสัญญาณดิจิทัลที่ Arduino สามารถอ่านได้
DHT22 ทำงานในช่วงแรงดันไฟฟ้า 3.3 ถึง 5 โวลต์ และสื่อสารผ่านอินเทอร์เฟซสายเดียว ทำให้ง่ายต่อการรวมเข้ากับโปรเจกต์ของคุณ นอกจากนี้ยังมีระยะการส่งข้อมูลที่ยาวไกลถึง 20 เมตร ทำให้มีความยืดหยุ่นในการวางตำแหน่งเซนเซอร์
รายละเอียดจาก Datasheet
| ผู้ผลิต | Aosong |
|---|---|
| หมายเลขชิ้นส่วน | DHT22 (AM2302) |
| แรงดันไฟฟ้าลอจิก/IO | 3.3 - 5 V |
| แรงดันไฟฟ้าจ่าย | 3.3 - 5 V |
| ช่วงการวัด (อุณหภูมิ) | -40 ถึง +80 °C |
| ช่วงการวัด (ความชื้น) | 0 ถึง 99 % |
| ความแม่นยำ (อุณหภูมิ) | ±0.5 °C |
| ความแม่นยำ (ความชื้น) | ±2 % ที่ 25 °C |
| ความละเอียด | 0.1 °C / 0.1 % |
| ระยะการส่งข้อมูล | สูงสุด 20 ม. |
| แพ็คเกจ | โมดูล 4 ขา |
- ตรวจสอบให้แน่ใจว่าแหล่งจ่ายไฟอยู่ระหว่าง 3.3V ถึง 5V อย่างเหมาะสม
- ใช้ตัวต้านทาน pull-up ขนาด 10K ระหว่างขาข้อมูลและแหล่งจ่ายไฟ
- รักษาสายเซนเซอร์ให้สั้นเพื่อการอ่านค่าที่แม่นยำ
- หลีกเลี่ยงการเรียกอ่านค่าถี่เกินไป ควรเว้นระยะเวลาระหว่างการอ่านแต่ละครั้ง
- ระวังปัจจัยด้านสิ่งแวดล้อมที่อาจส่งผลต่อการอ่านค่า
คำแนะนำการเดินสายไฟ

ในการเดินสายไฟเซนเซอร์ DHT22 ให้เริ่มต้นโดยเชื่อมต่อขาไฟเลี้ยง (ขา 1) ของเซนเซอร์เข้ากับเอาต์พุต 5V บน Arduino จากนั้นเชื่อมต่อขากราวด์ (ขา 4) เข้ากับขา GND ขาใดขาหนึ่งบน Arduino ขาข้อมูล (ขา 2) ควรเชื่อมต่อกับขาดิจิทัล 2 บน Arduino เพื่อการสื่อสาร นอกจากนี้ ให้วางตัวต้านทาน 10K ระหว่างขาข้อมูลและขาไฟเลี้ยงเพื่อให้แน่ใจว่าการอ่านค่ามีความเสถียร
ในกรณีที่คุณใช้ขาอื่นสำหรับข้อมูล อย่าลืมอัปเดตโค้ดให้สอดคล้องกันโดยเปลี่ยนค่า #define DHTPIN ให้ตรงกับขาที่เลือก การตั้งค่าควรทำได้ง่ายดาย การเชื่อมต่อตามคำแนะนำนี้จะช่วยให้เซนเซอร์ของคุณทำงานได้อย่างถูกต้อง
ตัวอย่างโค้ดและคำอธิบาย
โค้ดต่อไปนี้เริ่มต้นการทำงานของเซนเซอร์ DHT22 และอ่านค่าอุณหภูมิและความชื้น ขั้นแรก เราจะรวมไลบรารี DHT และกำหนดขาที่เชื่อมต่อกับเซนเซอร์:
#include "DHT.h"
#define DHTPIN 2 // ขาดิจิทัลที่เราเชื่อมต่อ
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
ที่นี่ ตัวแปร DHTPIN ถูกตั้งค่าเป็น 2 ซึ่งระบุว่าขาข้อมูลของเซนเซอร์เชื่อมต่อกับขาดิจิทัล 2 ของ Arduino DHTTYPE กำหนดประเภทของเซนเซอร์ที่ใช้ ซึ่งในกรณีนี้คือ DHT22
ถัดไป ในฟังก์ชัน setup() เราจะเริ่มต้นการทำงานของเซนเซอร์และซีเรียลมอนิเตอร์:
void setup() {
Serial.begin(9600);
dht.begin();
}
โค้ดส่วนนี้เริ่มต้นการสื่อสารแบบซีเรียลและเตรียมเซนเซอร์ DHT สำหรับการอ่านค่า Serial.begin(9600) ตั้งค่าอัตราบอดสำหรับการสื่อสารแบบซีเรียล
ในฟังก์ชัน loop() เราสามารถอ่านและแสดงค่าอุณหภูมิและความชื้นได้:
Serial.print("อุณหภูมิ: ");
Serial.print(getTemp("c")); // เซลเซียส
Serial.print(" *C ");
Serial.print(getTemp("h")); // ความชื้น
Serial.println(" % ");
โค้ดส่วนนี้พิมพ์อุณหภูมิในหน่วยเซลเซียสและเปอร์เซ็นต์ความชื้นไปยังซีเรียลมอนิเตอร์ ฟังก์ชัน getTemp() ใช้เพื่อดึงข้อมูลที่ต้องการตามพารามิเตอร์ที่ส่งเข้าไป
การสาธิต / สิ่งที่คาดหวัง
เมื่อตั้งค่าทุกอย่างเรียบร้อยและอัปโหลดโค้ดแล้ว คุณควรเห็นค่าอุณหภูมิและความชื้นแสดงบนซีเรียลมอนิเตอร์ DHT22 อาจใช้เวลาสักครู่เพื่อให้ค่าคงที่ ดังนั้นควรเว้นระยะเวลาสองสามวินาทีระหว่างการอ่านแต่ละครั้ง (ในวิดีโอที่ 15:00) หากมีปัญหาเกี่ยวกับการอ่านค่า ให้ตรวจสอบการเดินสายไฟและตรวจสอบให้แน่ใจว่าการเชื่อมต่อแน่นหนา ข้อผิดพลาดทั่วไป ได้แก่ การกำหนดขาไม่ถูกต้องหรือปัญหาแหล่งจ่ายไฟ
ไทม์สแตมป์ในวิดีโอ
- 00:00 - แนะนำเซนเซอร์ DHT22
- 01:30 - การเดินสายไฟเซนเซอร์
- 03:00 - คำอธิบายโค้ด
- 04:30 - การสาธิตโค้ด
- 06:00 - บทสรุป
++
/*
* This is the Arduino code for the DHT22 module to read temperature and humidity.
* This code can display temperature in:
* getTemp("c") is used to get Celsius.
* getTemp("f") is used to get Fahrenheit.
* getTemp("k") is used for Kelvin.
* getTemp("hif") is used to get Fahrenheit.
* getTemp("hic") is used to get Celsius.
* getTemp("h") is used to get humidity.
* Watch the video https://youtu.be/oi_GPSLjgBY
* Other Arduino library and videos https://robojax.com
*
* Written/updated by Ahmad Shamshiri for Robojax.com Video
* Date: Jan 08, 2018, in Ajax, Ontario, Canada
* Permission granted to share this code given that this
* note is kept with the code.
* Disclaimer: This code is "AS IS" and for educational purposes only.
*
* original source code : https://github.com/adafruit/DHT-sensor-library
*/
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
#include "DHT.h"
#define DHTPIN 2 // what digital pin we're connected to
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster processors.
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("DHTxx Robojax test!");
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Robojax.com test video
Serial.print("Temperature: ");
Serial.print(getTemp("c"));
Serial.print(" *C ");
Serial.print(getTemp("f"));
Serial.println (" *F");
Serial.println("-----------------");
Serial.print("Heat index: ");
Serial.print(getTemp("hic"));
Serial.print(" *C ");
Serial.print(getTemp("hif"));
Serial.println(" *F");
Serial.print(getTemp("k"));
Serial.println(" *K");
Serial.println("-----------------");
Serial.print("Humidity: ");
Serial.print(getTemp("h"));
Serial.println(" % ");
Serial.println("===========================");
}
/*
* getTemp(String req)
* returns the temperature related parameters
* req is string request
* This code can display temperature in:
* getTemp("c") is used to get Celsius.
* getTemp("f") is used to get Fahrenheit.
* getTemp("k") is used for Kelvin.
* getTemp("hif") is used to get Fahrenheit.
* getTemp("hic") is used to get Celsius.
* getTemp("h") is used to get humidity.
*/
float getTemp(String req)
{
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor).
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return 0.0;
}
// Compute heat index in Kelvin
float k = t + 273.15;
if(req =="c"){
return t;//return Celsius
}else if(req =="f"){
return f;// return Fahrenheit
}else if(req =="h"){
return h;// return humidity
}else if(req =="hif"){
return hif;// return heat index in Fahrenheit
}else if(req =="hic"){
return hic;// return heat index in Celsius
}else if(req =="k"){
return k;// return temperature in Kelvin
}else{
return 0.000;// if no request found, return 0.000
}
}
แหล่งข้อมูลและเอกสารอ้างอิง
ไฟล์📁
ไลบรารี Arduino (zip)
-
DHT22 PCB module red
DHT22-module-red.fzpz0.01 MB
ไฟล์ Fritzing
-
DHT22 Humidity and Temperature Sensor
DHT22 Humidity and Temperature Sensor.fzpz0.01 MB -
DHT22 PCB module red
DHT22-module-red.fzpz0.01 MB
คู่มือผู้ใช้
-
DHT22 Temperature and Humidity sensor user's manual
robojax-DHT22_manual.pdf0.36 MB