这是一个 Arduino 和 esp8266 库,用于 DHT12 系列超低成本温度/湿度传感器(低于 1 美元),可与 i2c 或单线连接配合使用。
AI 读到有时似乎需要校准,但我有这个树并获得与 DHT22 非常相似的值。如果你有这个问题,在 github 上打开问题,我添加实现。
要下载,请单击右上角的“下载”按钮,将未压缩的文件夹重命名为 DHT12。检查 DHT 文件夹是否包含DHT12.cpp
和DHT12.h
。将 DHT 库文件夹放在您的arduinosketchfolder>/libraries/
文件夹中。如果它是您的第一个库,您可能需要创建库子文件夹。重新启动 IDE。
这个库尝试模拟标准 DHT 库传感器的行为(并复制了很多代码),我也以同样的方式添加了管理 I2C 的代码。方法同DHT库sensor ,只是增加了露点功能。
要与 i2c 一起使用,构造函数是:
DHT12 dht12;
并为 SDA SCL 引脚取默认值。(可以使用 ESP8266 的指定构造函数重新定义,但这不是最好的方法)。要么
DHT12 dht12(uint8_t addressOrPin)
addressOrPin -> address
改变地址,布尔值是选择oneWire或I2C模式。
使用一根电线:
DHT12 dht12(uint8_t addressOrPin, true)
addressOrPin -> pin
布尔值是选择 OneWire 还是 i2c 模式。
您可以将它与“隐式”、“简单阅读”或“完整阅读”一起使用:
// The read of sensor have 2secs of elapsed time, unless you pass force parameter
// Read temperature as Celsius (the default)
float t12 = dht12.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f12 = dht12.readTemperature(true);
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h12 = dht12.readHumidity();
// Compute heat index in Fahrenheit (the default)
float hif12 = dht12.computeHeatIndex(f12, h12);
// Compute heat index in Celsius (isFahreheit = false)
float hic12 = dht12.computeHeatIndex(t12, h12, false);
// Compute dew point in Fahrenheit (the default)
float dpf12 = dht12.dewPoint(f12, h12);
// Compute dew point in Celsius (isFahreheit = false)
float dpc12 = dht12.dewPoint(t12, h12, false);
// The read of sensor have 2secs of elapsed time, unless you pass force parameter
bool chk = dht12.read(); // true read is ok, false read problem
// Read temperature as Celsius (the default)
float t12 = dht12.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f12 = dht12.readTemperature(true);
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h12 = dht12.readHumidity();
// Compute heat index in Fahrenheit (the default)
float hif12 = dht12.computeHeatIndex(f12, h12);
// Compute heat index in Celsius (isFahreheit = false)
float hic12 = dht12.computeHeatIndex(t12, h12, false);
// Compute dew point in Fahrenheit (the default)
float dpf12 = dht12.dewPoint(f12, h12);
// Compute dew point in Celsius (isFahreheit = false)
float dpc12 = dht12.dewPoint(t12, h12, false);
// The read of sensor have 2secs of elapsed time, unless you pass force parameter
DHT12::ReadStatus chk = dht12.readStatus();
Serial.print(F("\nRead sensor: "));
switch (chk) {
case DHT12::OK:
Serial.println(F("OK"));
break;
case DHT12::ERROR_CHECKSUM:
Serial.println(F("Checksum error"));
break;
case DHT12::ERROR_TIMEOUT:
Serial.println(F("Timeout error"));
break;
case DHT12::ERROR_TIMEOUT_LOW:
Serial.println(F("Timeout error on low signal, try put high pullup resistance"));
break;
case DHT12::ERROR_TIMEOUT_HIGH:
Serial.println(F("Timeout error on low signal, try put low pullup resistance"));
break;
case DHT12::ERROR_CONNECT:
Serial.println(F("Connect error"));
break;
case DHT12::ERROR_ACK_L:
Serial.println(F("AckL error"));
break;
case DHT12::ERROR_ACK_H:
Serial.println(F("AckH error"));
break;
case DHT12::ERROR_UNKNOWN:
Serial.println(F("Unknown error DETECTED"));
break;
case DHT12::NONE:
Serial.println(F("No result"));
break;
default:
Serial.println(F("Unknown error"));
break;
}
// Read temperature as Celsius (the default)
float t12 = dht12.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f12 = dht12.readTemperature(true);
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h12 = dht12.readHumidity();
// Compute heat index in Fahrenheit (the default)
float hif12 = dht12.computeHeatIndex(f12, h12);
// Compute heat index in Celsius (isFahreheit = false)
float hic12 = dht12.computeHeatIndex(t12, h12, false);
// Compute dew point in Fahrenheit (the default)
float dpf12 = dht12.dewPoint(f12, h12);
// Compute dew point in Celsius (isFahreheit = false)
float dpc12 = dht12.dewPoint(t12, h12, false);
有示例,有连接图,使用正确的上拉电阻很重要。
感谢 Bobadas、dplasa 和 Adafruit,他们在 Github 上分享了代码(我在这里获取了一些代码和想法)。
DHT12 连接架构如下。
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
全部0条评论
快来发表一下你的评论吧 !