找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4360|回复: 2
收起左侧

arduino控制的DHT11温湿度传感器程序

[复制链接]
ID:164146 发表于 2019-4-17 09:49 | 显示全部楼层 |阅读模式
Arduino源程序:
  1. #define DHT11_PIN 0
  2. int Led = 8;
  3. int Buzzer = 7;
  4. byte read_dht11_dat()
  5. {
  6.         byte i = 0;
  7.         byte result = 0;
  8.         for (i = 0; i<8; i++)
  9.         {
  10.                 while (!(PINC&_BV(DHT11_PIN)));
  11.                 delayMicroseconds(30);
  12.                 if (PINC&_BV(DHT11_PIN))
  13.                         result |= (1 << (7 - i));
  14.                 while ((PINC&_BV(DHT11_PIN)));
  15.         }
  16.         return result;
  17. }
  18. void setup()
  19. {
  20.         DDRC |= _BV(DHT11_PIN);
  21.         PORTC |= _BV(DHT11_PIN);
  22.         pinMode(Led, OUTPUT);
  23.         pinMode(Buzzer, OUTPUT);
  24.         Serial.begin(19200);
  25.         Serial.println("Ready");
  26. }
  27. void loop()
  28. {
  29.         byte dht11_dat[5];
  30.         byte dht11_in;
  31.         byte i;
  32.         PORTC &= ~_BV(DHT11_PIN);
  33.         delay(18);
  34.         PORTC |= _BV(DHT11_PIN);
  35.         delayMicroseconds(40);
  36.         DDRC &= ~_BV(DHT11_PIN);
  37.         delayMicroseconds(40);
  38.         dht11_in = PINC & _BV(DHT11_PIN);
  39.         if (dht11_in)
  40.         {
  41.                 Serial.println("dht11 start condition 1 not met");
  42.                 return;
  43.         }
  44.         delayMicroseconds(80);
  45.         dht11_in = PINC & _BV(DHT11_PIN);
  46.         if (!dht11_in)
  47.         {
  48.                 Serial.println("dht11 start condition 2 not met");
  49.                 return;
  50.         }
  51.         delayMicroseconds(80);
  52.         for (i = 0; i<5; i++)
  53.                 dht11_dat[i] = read_dht11_dat();
  54.         DDRC |= _BV(DHT11_PIN);
  55.         PORTC |= _BV(DHT11_PIN);
  56.         byte dht11_check_sum = dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3];
  57.         if (dht11_dat[4] != dht11_check_sum)
  58.         {
  59.                 Serial.println("DHT11 checksum error");
  60.         }
  61.         Serial.print("Current humdity= ");
  62.         Serial.print(dht11_dat[0], DEC);
  63.         Serial.print(".");
  64.         Serial.print(dht11_dat[1], DEC);
  65.         Serial.print("%");
  66.         Serial.print("temperature = ");
  67.         Serial.print(dht11_dat[2], DEC);
  68.         Serial.print(".");
  69.         Serial.print(dht11_dat[3], DEC);
  70.         Serial.println("C");
  71.         if (dht11_dat[0] == 25)
  72.                 digitalWrite(Led, HIGH);
  73.         else
  74.                 digitalWrite(Led, LOW);

  75.         if (dht11_dat[0] == 28)
  76.                 digitalWrite(Buzzer, LOW);
  77.         else
  78.                 digitalWrite(Buzzer, HIGH);
  79.         delay(2000);
  80. }
复制代码

全部资料51hei下载地址:
arduino控制的DHT11温湿度传感器程序.zip (889 Bytes, 下载次数: 33)
回复

使用道具 举报

ID:1 发表于 2019-4-17 14:42 | 显示全部楼层
本帖需要重新编辑补全电路原理图,源码,详细说明与图片即可获得100+黑币(帖子下方有编辑按钮)
回复

使用道具 举报

ID:694033 发表于 2020-3-19 06:32 | 显示全部楼层
下个学习
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表