标题:
arduino控制的DHT11温湿度传感器程序
[打印本页]
作者:
maomao12
时间:
2019-4-17 09:49
标题:
arduino控制的DHT11温湿度传感器程序
Arduino源程序:
#define DHT11_PIN 0
int Led = 8;
int Buzzer = 7;
byte read_dht11_dat()
{
byte i = 0;
byte result = 0;
for (i = 0; i<8; i++)
{
while (!(PINC&_BV(DHT11_PIN)));
delayMicroseconds(30);
if (PINC&_BV(DHT11_PIN))
result |= (1 << (7 - i));
while ((PINC&_BV(DHT11_PIN)));
}
return result;
}
void setup()
{
DDRC |= _BV(DHT11_PIN);
PORTC |= _BV(DHT11_PIN);
pinMode(Led, OUTPUT);
pinMode(Buzzer, OUTPUT);
Serial.begin(19200);
Serial.println("Ready");
}
void loop()
{
byte dht11_dat[5];
byte dht11_in;
byte i;
PORTC &= ~_BV(DHT11_PIN);
delay(18);
PORTC |= _BV(DHT11_PIN);
delayMicroseconds(40);
DDRC &= ~_BV(DHT11_PIN);
delayMicroseconds(40);
dht11_in = PINC & _BV(DHT11_PIN);
if (dht11_in)
{
Serial.println("dht11 start condition 1 not met");
return;
}
delayMicroseconds(80);
dht11_in = PINC & _BV(DHT11_PIN);
if (!dht11_in)
{
Serial.println("dht11 start condition 2 not met");
return;
}
delayMicroseconds(80);
for (i = 0; i<5; i++)
dht11_dat[i] = read_dht11_dat();
DDRC |= _BV(DHT11_PIN);
PORTC |= _BV(DHT11_PIN);
byte dht11_check_sum = dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3];
if (dht11_dat[4] != dht11_check_sum)
{
Serial.println("DHT11 checksum error");
}
Serial.print("Current humdity= ");
Serial.print(dht11_dat[0], DEC);
Serial.print(".");
Serial.print(dht11_dat[1], DEC);
Serial.print("%");
Serial.print("temperature = ");
Serial.print(dht11_dat[2], DEC);
Serial.print(".");
Serial.print(dht11_dat[3], DEC);
Serial.println("C");
if (dht11_dat[0] == 25)
digitalWrite(Led, HIGH);
else
digitalWrite(Led, LOW);
if (dht11_dat[0] == 28)
digitalWrite(Buzzer, LOW);
else
digitalWrite(Buzzer, HIGH);
delay(2000);
}
复制代码
全部资料51hei下载地址:
arduino控制的DHT11温湿度传感器程序.zip
(889 Bytes, 下载次数: 34)
2019-4-17 09:49 上传
点击文件名下载附件
DHT11
下载积分: 黑币 -5
作者:
admin
时间:
2019-4-17 14:42
本帖需要重新编辑补全电路原理图,源码,详细说明与图片即可获得100+黑币(帖子下方有编辑按钮)
作者:
秋雨枫叶
时间:
2020-3-19 06:32
下个学习
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1