ผู้เขียน หัวข้อ: งานครั้งที่ 8 การเขียนโปรแกรมอ่านค่าอุณหภูมิและความชื้นด้วย DHT22 เบื้องต้น  (อ่าน 22274 ครั้ง)

admin

  • Administrator
  • Hero Member
  • *****
  • กระทู้: 706
    • ดูรายละเอียด
    • อีเมล์
เขียนโปรแกรมอ่านค่าอุณหภูมิและความชื้นด้วย DHT22 เบื้องต้น
ไลบรารี่ DHT เป็นไลบรารี่ที่ใช้ติดต่อกับชิพอ่านอุณหภูมิและความชื้นสามารถใช้กับชิพได้ 3 เบอร์ด้วยกันคือ DHT11, DHT22, DHT21 ซึ่งจำเป็นต้องติดตั้งเพิ่มเติม (ในกรณีที่ผู้ใช้งานยังไม่ได้ติดตั้งไว้ก่อนหน้านี้)

การติดตั้งไลบารี่(ต้องใช้ไลบรารี่ 2 ตัว)  โดยมีขั้นตอนดังนี้
-DHT.h https://github.com/adafruit/DHT-sensor-library
-Adafruit_Sensor.h https://github.com/adafruit/Adafruit_Sensor

การเพิ่มไลบารี่ลงในโปรแกรม Arduino IDE จากไฟล์ Zip (ดำเนินการทั้ง 2 ตัว)
1. ดาวน์โหลดไฟล์ Zip ดังรูป




2. ทำการเพิ่มไลบรารี่ลงในโปรแกรม Arduino IDE โดยการเพิ่มจากไฟล์ zip แล้วทำการหาไฟล์ zip ที่ได้จากการดาวน์โหลดในข้อ 1


โจทย์การทดลอง
-เขียนโปรแกรมอุณหภูมิและความชื้นด้วย DHT22 แสดงผลที่ Serial monitor
-เขียนโปรแกรมอุณหภูมิและความชื้นด้วย DHT22 แสดงผลที่ LCD i2c
-โปรแกรมรายละเอียดพิเศษรายกลุ่ม (แจ้งให้ทราบเมื่อถึงชั่วโมงเรียน)

วงจรที่ใช้ในการทดลอง (แสดงผลที่ Serial monitor)


ตัวอย่างโปรแกรม (แสดงผลที่ Serial monitor)
โค๊ด: [Select]
#include <DHT.h>
#define DHTPIN D4         //pin connect DHT
#define LED D0            //LED BUILTIN for NodeMCU
#define DHTTYPE DHT22     //if use DHT11 change to "DHT11"
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
  pinMode(LED,OUTPUT);
  Serial.begin(115200);
  dht.begin();
}
void loop()
{
  digitalWrite(LED,HIGH);
  delay(2500);
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(t) || isnan(h))
  {
    Serial.println("Error reading DHT!");
    return;
  }
    digitalWrite(LED,LOW);   
    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print("\t");
    Serial.print("Temp: ");
    Serial.println(t);
    Serial.println("...............................");   
}

วงจรที่ใช้ในการทดลอง (แสดงผลที่ LCD i2c)


วงจรที่ใช้ในการทดลองเมื่อใช้กับ DHT11
วงจรที่ใช้ในการทดลองกรณีใช้บอร์ด NodeMCU

วงจรที่ใช้ในการทดลองกรณีใช้บอร์ด WeMos D1 mini


ตัวอย่างโปรแกรม (แสดงผลที่ LCD i2c)
โค๊ด: [Select]
#include <DHT.h>
#include <Wire.h>               // Include I2C bus library
#include <LiquidCrystal_I2C.h>  // Include LCD-I2C bus library
#define DHTPIN D4               //pin connect DHT
#define LED D0                  //LED BUILTIN for NodeMCU
#define DHTTYPE DHT22           //if use DHT11 edit to "DHT11"
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup()
{
  pinMode(LED,OUTPUT);
  dht.begin();
  lcd.init();                  // Start LCD operation
  lcd.backlight();              // Turn on backlight of LCD
  lcd.setCursor(0, 0);          // Set LCD home position
  lcd.print("Humidity=");       // Print Humidity message
  lcd.setCursor(0, 1);          // Set LCD home position of 2nd line
  lcd.print("Temp=");           // Print Temperature message 
}
void loop()
{
  digitalWrite(D0,HIGH);
  delay(2500);
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  digitalWrite(D0,LOW);   
  lcd.setCursor(10, 0);         
  lcd.print(h + String("%"));       // Show humidity value in %RH
  lcd.setCursor(6, 1);           
  lcd.print(t + String("C"));        // Show temperature value in Celcuis   
  delay(2500);
}
<a href="https://www.youtube.com/v/mAlloYf6Rwg" target="_blank" class="new_win">https://www.youtube.com/v/mAlloYf6Rwg</a>
« แก้ไขครั้งสุดท้าย: ธันวาคม 08, 2021, 11:54:59 AM โดย admin »