标题: 为什么Arduino结果是nan [打印本页]

作者: EFTHY    时间: 2023-11-27 22:52
标题: 为什么Arduino结果是nan
#include <DHT.h>

#define DHTPIN 4     // 温湿度传感器连接到ESP32的GPIO4引脚
#define DHTTYPE DHT11   // 使用DHT11传感器

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  dht.begin();
}

void loop() {
  delay(2000);  // 2秒读取一次数据

  float temperature = dht.readTemperature();  // 读取温度
  float humidity = dht.readHumidity();  // 读取湿度

  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.print(" °C, Humidity: ");
  Serial.print(humidity);
  Serial.println(" %");
}


作者: devcang    时间: 2023-11-28 09:55
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"

#define DHTPIN 2     // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#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 3 (on the right) of the sensor to GROUND (if your sensor has 3 pins)
// Connect pin 4 (on the right) of the sensor to GROUND and leave the pin 3 EMPTY (if your sensor has 4 pins)
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println(F("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 (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  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(F("Failed to read from DHT sensor!"));
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.print(F("°C "));
  Serial.print(f);
  Serial.print(F("°F  Heat index: "));
  Serial.print(hic);
  Serial.print(F("°C "));
  Serial.print(hif);
  Serial.println(F("°F"));
}

DHT库github上的例子,虽然不同模块,但原理相同 的。。。。nan就是没有检测到模块。

作者: EFTHY    时间: 2023-11-28 22:49
devcang 发表于 2023-11-28 09:55
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, publi ...

这个代码我也用过,在Arduino示例里面有,但是,还是不行,出现nan我应该怎么办呢?对了,谢谢你的回复

作者: EFTHY    时间: 2023-11-28 22:52
devcang 发表于 2023-11-28 09:55
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, publi ...

我的温湿度传感器是dht11的,最后我的代码显示没法读取传感器,不知道应该怎么办了
作者: Hephaestus    时间: 2023-11-29 00:25
100个NaN有99个是除0,楼主检查下更下面的代码,有没有判断被除数是0的逻辑。
作者: lwq1947    时间: 2023-11-29 07:26
DHT.h库虽i支持多种型号,但兼容性差。给你发一个DH11专用库试试。

Dht11.rar

2.53 KB, 下载次数: 7


作者: glinfei    时间: 2023-11-29 10:52
程序没看出问题,那只能是硬件或联结问题吧?
作者: EFTHY    时间: 2023-11-29 22:37
Hephaestus 发表于 2023-11-29 00:25
100个NaN有99个是除0,楼主检查下更下面的代码,有没有判断被除数是0的逻辑。

下面没有再写代码了
作者: EFTHY    时间: 2023-11-29 22:38
lwq1947 发表于 2023-11-29 07:26
DHT.h库虽i支持多种型号,但兼容性差。给你发一个DH11专用库试试。

好的,我试试
作者: EFTHY    时间: 2023-11-29 22:39
glinfei 发表于 2023-11-29 10:52
程序没看出问题,那只能是硬件或联结问题吧?

不知道哦,但是我一开始有成功过,但是后面重新试了一下,就不行了
作者: EFTHY    时间: 2023-11-29 23:27
EFTHY 发表于 2023-11-29 22:38
好的,我试试

还是不行,但是还是谢谢你




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1