我在我的arduino uno中使用DHT11传感器。传感器发送的数据格式为:8位积分RH数据+8位十进制RH数据+8位积分T数据+8位十进制T数据+8位校验和。
我使用的代码是:
//KY015 DHT11 Temperature and humidity sensor
int DHpin = 8;
byte dat [5];
byte read_data () {
byte data;
for (int i = 0; i < 8; i ++) {
if (digitalRead (DHpin) == LOW) {
while (digitalRead (DHpin) == LOW); // wait for 50us
delayMicroseconds (30); // determine the duration of the high level to determine the data is '0 'or '1'
if (digitalRead (DHpin) == HIGH)
data |= (1 << (7-i)); // high front and low in the post
while (digitalRead (DHpin) == HIGH); // data '1 ', wait for the next one receiver
}
}
return data;
}
void start_test () {
digitalWrite (DHpin, LOW); // bus down, send start signal
delay (30); // delay greater than 18ms, so DHT11 start signal can be detected
digitalWrite (DHpin, HIGH);
delayMicroseconds (40); // Wait for DHT11 response
pinMode (DHpin, INPUT);
while (digitalRead (DHpin) == HIGH);
delayMicroseconds (80); // DHT11 response, pulled the bus 80us
if (digitalRead (DHpin) == LOW);
delayMicroseconds (80); // DHT11 80us after the bus pulled to start sending data
for (int i = 0; i < 4; i ++) // receive temperature and humidity data, the parity bit is not considered
dat[i] = read_data ();
pinMode (DHpin, OUTPUT);
digitalWrite (DHpin, HIGH); // send data once after releasing the bus, wait for the host to open the next Start signal
}
void setup () {
Serial.begin (9600);
pinMode (DHpin, OUTPUT);
}
void loop () {
start_test ();
Serial.print ("Current humdity =");
Serial.print (dat [0], DEC); // display the humidity-bit integer;
Serial.print ('.');
Serial.print (dat [1], DEC); // display the humidity decimal places;
Serial.println ('%');
Serial.print ("Current temperature =");
Serial.print (dat [2], DEC); // display the temperature of integer bits;
Serial.print ('.');
Serial.print (dat [3], DEC); // display the temperature of decimal places;
Serial.println ('C');
delay (700);
}这段代码可能不会是我的最后一段代码,但我正在试图了解传感器的数据是怎么回事。我不明白为什么整数数据和十进制数据都被打印为DEC,我也不明白为什么会有“。就在那儿。此代码取自传感器的wiki页面。此外,我总是在湿度和温度的数据部分得到0。
发布于 2016-05-08 16:25:24
数据转换就像湿度一样,
Humidity Value = ((float)((integral RH data << 8) + 8bit decimal RH data)/(float)10.0)对于温度来说,
Temperature Value = ((float)((8bit integral T data << 8) + 8bit decimal T data)/(float)10.0)例如:
8bit integral T data = 0x01 and 8bit decimal T data = 0x32
The real measured temperature value = ((0x01 << 8) + 0x32) /10
= (256 + 50 )/10 = 30.6 degree c 同样的方法,湿度也将用%来表示。请注意,我对DHT22的步骤大小(分辨率为.1度数)对于DHT11,它似乎是1度。
引用您的代码
Serial.print ("Current humdity =");
Serial.print (dat [0], DEC); // display the humidity-bit integer;
Serial.print ('.');
Serial.print (dat [1], DEC); // display the humidity decimal places;
Serial.println ('%');
Serial.print ("Current temperature =");
Serial.print (dat [2], DEC); // display the temperature of integer bits;
Serial.print ('.');
Serial.print (dat [3], DEC); // display the temperature of decimal places;
Serial.println ('C');由于缺少转换,我认为它不会显示正确的值。
https://stackoverflow.com/questions/36664784
复制相似问题