標(biāo)題: 為什么Arduino結(jié)果是nan [打印本頁(yè)]

作者: EFTHY    時(shí)間: 2023-11-27 22:52
標(biāo)題: 為什么Arduino結(jié)果是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秒讀取一次數(shù)據(jù)

  float temperature = dht.readTemperature();  // 讀取溫度
  float humidity = dht.readHumidity();  // 讀取濕度

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


作者: devcang    時(shí)間: 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庫(kù)github上的例子,雖然不同模塊,但原理相同 的。。。。nan就是沒有檢測(cè)到模塊。

作者: EFTHY    時(shí)間: 2023-11-28 22:49
devcang 發(fā)表于 2023-11-28 09:55
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, publi ...

這個(gè)代碼我也用過,在Arduino示例里面有,但是,還是不行,出現(xiàn)nan我應(yīng)該怎么辦呢?對(duì)了,謝謝你的回復(fù)

作者: EFTHY    時(shí)間: 2023-11-28 22:52
devcang 發(fā)表于 2023-11-28 09:55
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, publi ...

我的溫濕度傳感器是dht11的,最后我的代碼顯示沒法讀取傳感器,不知道應(yīng)該怎么辦了
作者: Hephaestus    時(shí)間: 2023-11-29 00:25
100個(gè)NaN有99個(gè)是除0,樓主檢查下更下面的代碼,有沒有判斷被除數(shù)是0的邏輯。
作者: lwq1947    時(shí)間: 2023-11-29 07:26
DHT.h庫(kù)雖i支持多種型號(hào),但兼容性差。給你發(fā)一個(gè)DH11專用庫(kù)試試。

Dht11.rar

2.53 KB, 下載次數(shù): 7


作者: glinfei    時(shí)間: 2023-11-29 10:52
程序沒看出問題,那只能是硬件或聯(lián)結(jié)問題吧?
作者: EFTHY    時(shí)間: 2023-11-29 22:37
Hephaestus 發(fā)表于 2023-11-29 00:25
100個(gè)NaN有99個(gè)是除0,樓主檢查下更下面的代碼,有沒有判斷被除數(shù)是0的邏輯。

下面沒有再寫代碼了
作者: EFTHY    時(shí)間: 2023-11-29 22:38
lwq1947 發(fā)表于 2023-11-29 07:26
DHT.h庫(kù)雖i支持多種型號(hào),但兼容性差。給你發(fā)一個(gè)DH11專用庫(kù)試試。

好的,我試試
作者: EFTHY    時(shí)間: 2023-11-29 22:39
glinfei 發(fā)表于 2023-11-29 10:52
程序沒看出問題,那只能是硬件或聯(lián)結(jié)問題吧?

不知道哦,但是我一開始有成功過,但是后面重新試了一下,就不行了
作者: EFTHY    時(shí)間: 2023-11-29 23:27
EFTHY 發(fā)表于 2023-11-29 22:38
好的,我試試

還是不行,但是還是謝謝你




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1