2

我在这里买了这个湿度/温度传感器: https ://www.sparkfun.com/products/10167

我在这里阅读了数据表:http: //dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/Weather/RHT03.pdf

我尝试使用我的 c++ 类中的一些基础编程,但我不知道问题出在哪里?看起来来自传感器的数据输入没有发送任何内容。在数据表上,我需要发送一个 5V 的输入信号(持续 20-40 毫秒),然后等待传感器的响应并存储传入的位。传感器数据始终为 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;
    }
  }
}
4

1 回答 1

5

The datasheet says;

MCU will pull low data-bus and this process must beyond at least 1~10ms to ensure RHT03 could detect MCU's signal

Your code says;

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

digitalWrite(SensorVccPin, HIGH);  

You seem to be delaying 10µs instead of 10ms.

于 2012-08-05T21:07:21.697 回答