Arduino 和 AM2301(DHT21)温湿度传感器 应用

 

AM2301(DHT21)

AM2301,又或者叫DHT21的温湿度传感器,是一款含有已校准数字信号输出的温湿度复合传感器,利用单线串口输出温度和湿度的数据,应用范围环境监测,除湿器,家电,自动控制,数据记录,汽车,消费品,气象站,暖通空调,测试及检测设备,医疗等等。

 

湿度精度±5%

湿度分辨率 0.1

温度精度±1℃

温度分辨率 0.1

 

引脚说明

红色线 ------ VCC(接3V-5V电源)

黄色线 ------ DATA OUT 数据输出

黑色线 ------ GND(接地)

 

接线方式


程序实现

首先这程序的库资源是在这里找到的:
https://github.com/andyduino/temperature-and-humidity-sensor-AM2301

(再次膜拜大神,我只是知识的搬运工与翻译者)

点击 Download ZIP 下载资源,把文件解压到Arduino 的库文件夹里

个人感觉是学习如何写底层代码的好例子,继续研究

 

程序打包下载:https://u16460183.ctfile.com/fs/16460183-295332343

文件夹说明:
LS_AM3201 -- 主程序
temperature-and-humidity-sensor-AM2301-master -- DHT21库(库需要放在Arduino的库目录里) 

 

 

#include "DHT.h"

#define DHTPIN 10     // what pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11 
//#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
// NOTE: For working with a faster chip, like an Arduino Due or Teensy, you
// might need to increase the threshold for cycle counts considered a 1 or 0.
// You can do this by passing a 3rd parameter for this threshold.  It's a bit
// of fiddling to find the right value, but in general the faster the CPU the
// higher the value.  The default for a 16mhz AVR is a value of 6.  For an
// Arduino Due that runs at 84mhz a value of 30 works.
// Example to initialize DHT sensor for Arduino Due:
//DHT dht(DHTPIN, DHTTYPE, 30);

void setup() {
  Serial.begin(9600); 
  Serial.println("DHTxx test!");
 
  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit
  float f = dht.readTemperature(true);
  
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index
  // Must send in temp in Fahrenheit!
  float hi = dht.computeHeatIndex(f, h);

  Serial.print("Humidity: "); 
  Serial.print(h);
  Serial.print(" %");
  Serial.print("\t");
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.print(" *C ");
  //Serial.print(f);
 // Serial.print(" *F\t");
 // Serial.print("Heat index: ");
 // Serial.print(hi);
 // Serial.println(" *F");
}

 

 

 

 

 

 

 

实例效果

打开串口监视器,设置好相应的波特率,就会看到如下数据,T是温度,H是湿度。

 

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Arduino可以通过引用DHT库来读取DHT11湿度传感器的数据。首先,需要将DHT库添加到Arduino环境中。可以在库管理器中搜索"DHT"来找到适合的库。然后,选择合适的DHT库并安装它。 接下来,连接DHT11传感器到Arduino板。DHT11有三个引脚:VCC、OUT和GND。将DHT11的VCC引脚连接到Arduino的5V引脚,OUT引脚连接到Arduino的数字引脚(如D2引脚),而GND引脚连接到Arduino的GND引脚。 在Arduino IDE中,创建一个新的项目,并在代码中引用DHT库。使用DHT库提供的函数来读取湿度传感器的数据。首先,定义一个DHT对象,并指定DHT传感器的引脚和类型。例如,可以使用以下代码: #include <DHT.h> #define DHTPIN 2 // 定义DHT11引脚为数字引脚2 #define DHTTYPE DHT11 // 设置传感器类型为DHT11 DHT dht(DHTPIN, DHTTYPE); // 创建一个DHT对象 然后,在setup()函数中,初始化串口通信,并将DHT对象初始化。例如,可以使用以下代码: void setup() { Serial.begin(9600); // 初始化串口通信为9600波特率 dht.begin(); // 初始化DHT对象 } 最后,在loop()函数中,使用DHT库提供的函数读取湿度数据,并将其打印到串口监视器上。例如,可以使用以下代码: void loop() { float temperature = dht.readTemperature(); // 读取度值 float humidity = dht.readHumidity(); // 读取湿度值 Serial.print("度: "); Serial.print(temperature); Serial.print(" °C"); Serial.print("\t湿度: "); Serial.print(humidity); Serial.println(" %"); delay(2000); // 延迟2秒 } 这样,Arduino将会每隔2秒读取一次DHT11湿度传感器的数据,并将其通过串口通信输出到电脑上的串口监视器中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值