一、課程目標:
安裝的程式庫位於「C:\使用者\電腦名稱\文件\Arduino\libraries」資料夾中。
三、接線圖
"+" vcc 極接5V,v
"-" Gnd 極接GND,
data pin接上要輸入的Pin腳=>接在13 (D7)
四、程式碼
點選「範例」->SimpleDHT->DHT11Default
#include <SimpleDHT.h> // for DHT11, // VCC: 5V or 3V // GND: GND // DATA: 2 int pinDHT11 = 13;//data PIN 在D7=>也就是13 SimpleDHT11 dht11(pinDHT11); void setup() { Serial.begin(115200); } void loop() { // start working... Serial.println("================================="); Serial.println("Sample DHT11..."); // read without samples. byte temperature = 0; byte humidity = 0; int err = SimpleDHTErrSuccess; if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) { Serial.print("Read DHT11 failed, err="); Serial.println(err);delay(1000); return; } Serial.print("Sample OK: "); Serial.print((int)temperature); Serial.print(" *C, "); Serial.print((int)humidity); Serial.println(" H"); // DHT11 sampling rate is 1HZ. delay(1500); }
伍、執行結果
六、程式碼2
#include <SimpleDHT.h> #include <Wire.h> #include <esp_LiquidCrystal_I2C.h> esp_LiquidCrystal_I2C mylcd(0x27, 16, 2); // for DHT11, // VCC: 5V or 3V // GND: GND // DATA: 13 int pinDHT11 = 13;//di mini D7 DHT11腳位在D7 , 也就是13 SimpleDHT11 dht11(pinDHT11); int no = 0;//計數器 void setup() { Serial.begin(115200); mylcd.init(); mylcd.backlight(); mylcd.setCursor(0, 0); mylcd.print("Welcome to"); mylcd.setCursor(0, 1); mylcd.print("DHT 11"); } void loop() { // start working... Serial.println("================================="); Serial.println("Sample DHT11..."); // read without samples. byte temperature = 0; byte humidity = 0; int err = SimpleDHTErrSuccess; if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) { Serial.print("Read DHT11 failed, err="); Serial.println(err);delay(1000); mylcd.setCursor(0, 0); mylcd.print("Read DHT11 failed"); return; } Serial.print("Sample OK: "); Serial.print((int)temperature); Serial.print(" *C, "); Serial.print((int)humidity); Serial.println(" H"); mylcd.clear(); mylcd.setCursor(0, 0); mylcd.print("NO:"); mylcd.print(no); mylcd.setCursor(0, 1); mylcd.print(String("T:") + String(temperature) + String("C")); mylcd.setCursor(9, 1); mylcd.print(String("H:") + String(humidity) + String("%")); no++; delay(3000);//每三秒重新測量 }
沒有留言:
張貼留言