我买了这个传感器:
http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/Weather/RHT03.pdf
输出为“MaxDetect 1-wire bus”,它是什么意思以及如何使用它?我试图将它连接到arduino并对其进行编程,但它无法正常工作,我不断收到1位数据。我猜我的代码错了。我是否需要将Wire-1库用于arduino?
int SensorVccPin = 8;
int datapin = 6;
int bitcheck1;
int bitcheck2;
int temp[16];
int humidity[16];
int sensorcheck[8];
void setup()
{
pinMode(SensorVccPin, OUTPUT);
pinMode(datapin, INPUT);
Serial.begin(9600);
}
void loop()
{
// This is the STEP #1
digitalWrite(SensorVccPin, HIGH);
delay(5000);
digitalWrite(SensorVccPin, LOW);
delay(15);
digitalWrite(SensorVccPin, HIGH);
delayMicroseconds(30);
digitalWrite(SensorVccPin, LOW);
while(digitalRead(datapin) == 0){
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;
}
delayMicroseconds(80);
for(int i=0; i<16; i++)
{
Serial.print(temp[i]);
}
Serial.print(" temp ");
for(int i=0; i<16; i++)
{
Serial.print(humidity[i]);
}
Serial.print(" hum ");
for(int i=0; i<8; i++)
{
Serial.print(sensorcheck[i]);
}
Serial.print(" check ");
}
}
答案 0 :(得分:1)
应始终将VCC引脚(引脚1)保持为高电平。事实上,理想情况下,它应该连接到电源。这是数据引脚(引脚2),你应该拉高和低与传感器通信,它是你应该读取的数据引脚。您必须在输入和输出模式之间切换数据引脚。确保数据引脚上有一个上拉电阻,因为传感器只能将其拉低。
答案 1 :(得分:0)
你可以看一下:
http://tienda.tdrobotica.co/download/RHT03_programa.pdf
最好的问候。