2017-06-22 37 views
0

我是低級編程新手,並試圖將DHT22傳感器連接到Adafruit M0 Lora上進行溫度讀數。到目前爲止,我只檢索NaN s。將adafruit m0與dht22連接

我設置的連接與this sketch相同,除了使用針腳13而不是針腳2作爲傳感器輸入/輸出。我意識到爲不同的羽毛板製作的草圖,儘管邏輯仍然應該與我所能理解的一致。

我正在使用Adafruit's DHT library

#include <Adafruit_Sensor.h> 
#include <DHT.h> 
#include <DHT_U.h> 

// pin connected to DH22 data line 
#define DATA_PIN 13 
DHT_Unified dht(DATA_PIN, DHT22); 

void setup() { 

    // start the serial connection 
    Serial.begin(9600); 

    // wait for serial monitor to open 
    while(! Serial); 

    // initialize dht22 
    dht.begin(); 

    // connect to io.adafruit.com 
    Serial.print("Connecting to Adafruit IO"); 

    // we are connected 
    Serial.println(); 

} 

void loop() { 

    sensors_event_t event; 
    dht.temperature().getEvent(&event); 

    float celsius = event.temperature; 
    float fahrenheit = (celsius * 1.8) + 32; 

    Serial.print("celsius: "); 
    Serial.print(celsius); 
    Serial.println("C"); 

    Serial.print("fahrenheit: "); 
    Serial.print(fahrenheit); 
    Serial.println("F"); 

    // save fahrenheit (or celsius) to Adafruit IO 

    dht.humidity().getEvent(&event); 

    Serial.print("humidity: "); 
    Serial.print(event.relative_humidity); 
    Serial.println("%"); 
    delay(5000); 
} 

會有人能夠幫助的我在做什麼錯誤呢?我嘗試了9600以外的其他波特,以及更改可編程引腳。任何幫助都將不勝感激。

回答

1

我不認爲這是一個代碼問題。 13號針是特別的。選擇一個不同的引腳。

具體來說:

注意:數字銷13是更難,因爲比其它數字引腳一個數字輸入以使用,因爲它具有連接到它的一個LED和電阻器,其真實焊接到電路板上的最板。如果啓用其內部20kΩ上拉電阻,則它會掛在1.7V左右,而不是預期的5V,因爲板載LED和串聯電阻會降低電壓電平,這意味着它總是返回LOW。如果必須使用引腳13作爲數字輸入,則將其pinMode()設置爲INPUT並使用外部下拉電阻。

Arduino documentation

+0

感謝您的回覆。我確實嘗試了第9針,不幸的是沒有成功。 – Hakon