找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1561|回復: 10
打印 上一主題 下一主題
收起左側(cè)

為什么Arduino結(jié)果是nan

[復制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:1101498 發(fā)表于 2023-11-27 22:52 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
#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(" %");
}

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

沙發(fā)
ID:277550 發(fā)表于 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就是沒有檢測到模塊。
回復

使用道具 舉報

板凳
ID:1101498 發(fā)表于 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 ...

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

使用道具 舉報

地板
ID:1101498 發(fā)表于 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)該怎么辦了
回復

使用道具 舉報

5#
ID:883242 發(fā)表于 2023-11-29 00:25 | 只看該作者
100個NaN有99個是除0,樓主檢查下更下面的代碼,有沒有判斷被除數(shù)是0的邏輯。
回復

使用道具 舉報

6#
ID:997011 發(fā)表于 2023-11-29 07:26 | 只看該作者
DHT.h庫雖i支持多種型號,但兼容性差。給你發(fā)一個DH11專用庫試試。

Dht11.rar

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

回復

使用道具 舉報

7#
ID:844772 發(fā)表于 2023-11-29 10:52 | 只看該作者
程序沒看出問題,那只能是硬件或聯(lián)結(jié)問題吧?
回復

使用道具 舉報

8#
ID:1101498 發(fā)表于 2023-11-29 22:37 | 只看該作者
Hephaestus 發(fā)表于 2023-11-29 00:25
100個NaN有99個是除0,樓主檢查下更下面的代碼,有沒有判斷被除數(shù)是0的邏輯。

下面沒有再寫代碼了
回復

使用道具 舉報

9#
ID:1101498 發(fā)表于 2023-11-29 22:38 | 只看該作者
lwq1947 發(fā)表于 2023-11-29 07:26
DHT.h庫雖i支持多種型號,但兼容性差。給你發(fā)一個DH11專用庫試試。

好的,我試試
回復

使用道具 舉報

10#
ID:1101498 發(fā)表于 2023-11-29 22:39 | 只看該作者
glinfei 發(fā)表于 2023-11-29 10:52
程序沒看出問題,那只能是硬件或聯(lián)結(jié)問題吧?

不知道哦,但是我一開始有成功過,但是后面重新試了一下,就不行了
回復

使用道具 舉報

11#
ID:1101498 發(fā)表于 2023-11-29 23:27 | 只看該作者

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

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表