2016年1月24日 星期日

Arduino的教學10- 利用DHT11抓取溫度和濕度,並顯示於2x16 I2C LCD上

一、學習目標:
學習使用DHT11溫溼度感應器來測量溫度,並顯示於 I2C 2X16

二、重點說明
DHT11功能說明:
1、濕度測量範圍:20~90%RH;
2、濕度測量精度:±5%RH;
3、溫度測量範圍:0~50℃
4、溫度測量精度:±2℃
5、電源供應範圍: 3~5V
6、頻率不可超過:0.5Hz (每2秒一次)

三、接線圖
"+" vcc 極接5V,v
"-" Gnd 極接GND,
data pin接上要輸入的Pin腳=>接在2

四、程式碼
DHT11的程式碼1

#include <dht.h>    
#define dht_dpin 2 //定義訊號要從Pin 2 進來  
 
dht DHT; 
  
void setup(){   
Serial.begin(9600);   
delay(300);             //Let system settle   
Serial.println("Humidity and temperature\n\n");   
delay(700);             //Wait rest of 1000ms recommended delay before   
                        //accessing sensor   
}
  
void loop(){   
DHT.read11(dht_dpin);   //去library裡面找DHT.read11  
Serial.print("Humidity = ");   
Serial.print(DHT.humidity);   
Serial.print("% ");   
Serial.print("temperature = ");   
Serial.print(DHT.temperature);   
Serial.println("C ");   
delay(1000);  //每1000ms更新一次  
}

DHT11 + I2C LCD 程式碼
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

#include <dht.h>    
#define dht_dpin 2 //定義訊號要從Pin 2 進來  
dht DHT; 

int no=0;//計數器
void setup()
{
  lcd.init();                      // initialize the lcd 
Serial.begin(9600);
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("I2C+DHT11");
  Serial.println("Humidity and temperature\n\n");   
  delay(1000);  
}

void loop()
{
  no++;
  DHT.read11(dht_dpin);   //去library裡面找DHT.read11 
  //序列埠輸出
  Serial.print("Humidity = ");   
  Serial.print(DHT.humidity);   
  Serial.print("% ");   
  Serial.print("temperature = ");   
  Serial.print(DHT.temperature);   
  Serial.println("C "); 

  //資料顯示在LCD
  lcd.setCursor(0, 0);
  lcd.print("No.");
  lcd.print(no);
  lcd.setCursor(8, 1);
  lcd.print("H=");
  lcd.print(DHT.humidity);   
  lcd.setCursor(0, 1);
  lcd.print("T=");
  lcd.print(DHT.temperature);   
  delay(500);            //每500ms更新一次  
  lcd.clear();
}
五、參考資料: http://ming-shian.blogspot.tw/2014/05/arduino19dht11.html

3 則留言:

  1. 有DHT11 + I2C LCD的接線圖嗎

    回覆刪除
  2. #include
    #include

    LiquidCrystal_I2C lcd(0x27,16,2);



    int number=0;
    #define TrigPIN 13
    #define EchoPIN 12
    #include
    #define dht_dpin 2 //定義訊號要從Pin 2 進來
    dht DHT;

    int no=0;//計數器
    void setup() {
    pinMode(TrigPIN, OUTPUT);
    pinMode(EchoPIN, INPUT);
    lcd.init(); // initialize the lcd

    // Print a message to the LCD.
    lcd.backlight();
    lcd.setCursor(1, 0);//第1列,空1格開始
    lcd.print("Hello,HC_SR04!");

    //序列埠除錯用
    Serial.begin(9600);
    Serial.println("Hello, HC_SR04!");
    delay(1000);

    lcd.init(); // initialize the lcd
    Serial.begin(9600);
    // Print a message to the LCD.
    lcd.backlight();
    lcd.setCursor(0, 0);
    lcd.print("I2C+DHT11");
    Serial.println("Humidity and temperature\n\n");
    delay(1000);
    }
    void loop() {

    float duration, distance;//時間、距離
    digitalWrite(TrigPIN, HIGH);
    delayMicroseconds(1000);
    digitalWrite(TrigPIN, LOW);
    duration = pulseIn (EchoPIN, HIGH);//pulseIn ( ) :讀取一個針腳的脈衝時間(HIGH或LOW)
    distance = (duration/2)/29;

    number = number +1;

    lcd.setCursor (0,0);
    lcd.print("No.");
    lcd.print (number);
    lcd.setCursor(0, 1);
    lcd.print ("Dis.");
    lcd.print(distance);
    lcd.print ("cm");


    Serial.print("No.");
    Serial.print (number);
    Serial.print (" Dis. ");
    Serial.print(distance);
    Serial.println (" cm");//換行

    delay(1000);
    lcd.clear();
    no++;
    DHT.read11(dht_dpin); //去library裡面找DHT.read11
    //序列埠輸出
    Serial.print("Humidity = ");
    Serial.print(DHT.humidity);
    Serial.print("% ");
    Serial.print("temperature = ");
    Serial.print(DHT.temperature);
    Serial.println("C ");

    //資料顯示在LCD
    lcd.setCursor(0, 0);
    lcd.print("No.");
    lcd.print(no);
    lcd.setCursor(8, 1);
    lcd.print("H=");
    lcd.print(DHT.humidity);
    lcd.setCursor(0, 1);
    lcd.print("T=");
    lcd.print(DHT.temperature);
    delay(500); //每500ms更新一次
    lcd.clear();
    }

    回覆刪除
  3. 請問DHT的凾式庫從哪裡取得?

    回覆刪除