2012-08-05 47 views
2

我在這裏買了這humitid /溫度傳感器: https://www.sparkfun.com/products/10167我的arduino傳感器程序有什麼問題?

而且我在這裏閱讀Datasheed: http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/Weather/RHT03.pdf

我試圖用一些基本的從我的C++類編程,但我不知道什麼是問題?它看起來像傳感器的數據輸入沒有發送任何東西。在數據表中,我需要發送5V輸入信號(20-40ms),然後等待傳感器的響應並存儲傳入的比特。傳感器數據始終是0.我不明白爲什麼。

/* Code for the RHT03 Sensor 8/5/12 */ 

int i=0; 
int SensorVccPin = 13; 
int datapin = 7; 
int bitcheck1; 
int bitcheck2; 
int temp[16]; 
int humidity[16]; 
int sensorcheck[8]; 

void setup() 
{ 
    pinMode(SensorVccPin, OUTPUT); 
    pinMode(datapin, INPUT); 
} 

void loop() 
{ 
    // This is the STEP #1 
    digitalWrite(SensorVccPin, LOW); 
    delayMicroseconds(10); 

    digitalWrite(SensorVccPin, HIGH); 
    delayMicroseconds(30); 
    digitalWrite(SensorVccPin, LOW); 
    /* NEED TO DO BIG NESTED IF STATEMENTS!!!!*/ 

    digitalRead(datapin); 

    if (digitalRead(datapin) == LOW) // FIRST BIG IF 
    while(digitalRead(datapin) == LOW) 
    { 
     delayMicroseconds(2); 
    } 

    while(digitalRead(datapin) == HIGH) 
    { 
     delayMicroseconds(2); 
    } 

    // STEP #2 
    for (int i = 0; i<16; i++) 
    { 
    while(digitalRead(datapin) == LOW) 
    { 
     delayMicroseconds(2); 
    } 

    delayMicroseconds(10); 

    bitcheck1 = digitalRead(datapin); // maybe store as an array 
    delayMicroseconds(17);  // 50 sec pause // 27us = 0 bit, 70us is 1 bit data 
    delayMicroseconds(15); 
    bitcheck2 = digitalRead(datapin); 

    if (bitcheck1==bitcheck2) 
    { 
     temp[i]=1; 
     delayMicroseconds(28); 
    } else { 
     temp[i]=0; 
    } 
    } 

    for (int i = 0; i<16; i++) 
    { 
    while(digitalRead(datapin) == LOW) 
    { 
     delayMicroseconds(2); 
    } 

    delayMicroseconds(10); 
    bitcheck1 = digitalRead(datapin); // maybe store as an array 
    delayMicroseconds(17);  // 50 sec pause // 27us = 0 bit, 70us is 1 bit data 
    delayMicroseconds(15); 
    bitcheck2 = digitalRead(datapin); 


    if (bitcheck1==bitcheck2) 
    { 
     humidity[i]=1; 
     delayMicroseconds(28); 
    } else { 
     humidity[i]=0; 
    } 
    } 


    for (int i = 0; i<8; i++) 
    { 
    while(digitalRead(datapin) == LOW) 
    { 
     delayMicroseconds(2); 
    } 

    delayMicroseconds(10); 

    bitcheck1 = digitalRead(datapin); // maybe store as an array 
    delayMicroseconds(17);  // 50 sec pause // 27us = 0 bit, 70us is 1 bit data 
    delayMicroseconds(15); 
    bitcheck2 = digitalRead(datapin); 

    if (bitcheck1==bitcheck2) 
    { 
     sensorcheck[i]=1; 
     delayMicroseconds(28); 
    } else { 
     sensorcheck[i]=0; 
    } 
    } 
} 
+0

如果我正確地閱讀了數據表,所有的操作都在同一個引腳上。 SensorVccPin和datapin是否物理連接到相同的傳感器引腳?如果你在初始轉換之後使用了相同的引腳並將其輸出切換到輸入模式,那麼它會不會更簡單,更不容易出錯? – fvu 2012-08-05 21:02:26

+0

是的,它是有道理的,但我不明白的東西。傳感器上有一個Vcc引腳和一個數據引腳,我認爲Vcc必須「發送一個20-40us的脈衝」,然後從數據引腳讀取數據,不是嗎? 對不起,我不主要是英語,這是我在EE的第二年。 感謝您的回答 – EEstud 2012-08-05 21:09:07

+1

不,Vcc是恆定的,整個脈衝串必須施加到DATA引腳(引腳2)或從DATA引腳讀取。 – fvu 2012-08-05 21:11:58

回答

5

該數據表說:

MCU將拉低數據總線和這個過程必須超越至少 1〜10毫秒,確保RHT03可以檢測MCU的信號

您的代碼表示;

digitalWrite(SensorVccPin, LOW); 
delayMicroseconds(10); 

digitalWrite(SensorVccPin, HIGH); 

您似乎延遲10μs而不是10ms。