找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 425|回复: 10
打印 上一主题 下一主题
收起左侧

为什么Arduino结果是nan

[复制链接]
跳转到指定楼层
楼主
ID:1101498 发表于 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秒读取一次数据

  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空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:277550 发表于 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 发表于 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我应该怎么办呢?对了,谢谢你的回复
回复

使用道具 举报

地板
ID:1101498 发表于 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的,最后我的代码显示没法读取传感器,不知道应该怎么办了
回复

使用道具 举报

5#
ID:883242 发表于 2023-11-29 00:25 | 只看该作者
100个NaN有99个是除0,楼主检查下更下面的代码,有没有判断被除数是0的逻辑。
回复

使用道具 举报

6#
ID:997011 发表于 2023-11-29 07:26 | 只看该作者
DHT.h库虽i支持多种型号,但兼容性差。给你发一个DH11专用库试试。

Dht11.rar

2.53 KB, 下载次数: 7

回复

使用道具 举报

7#
ID:844772 发表于 2023-11-29 10:52 | 只看该作者
程序没看出问题,那只能是硬件或联结问题吧?
回复

使用道具 举报

8#
ID:1101498 发表于 2023-11-29 22:37 | 只看该作者
Hephaestus 发表于 2023-11-29 00:25
100个NaN有99个是除0,楼主检查下更下面的代码,有没有判断被除数是0的逻辑。

下面没有再写代码了
回复

使用道具 举报

9#
ID:1101498 发表于 2023-11-29 22:38 | 只看该作者
lwq1947 发表于 2023-11-29 07:26
DHT.h库虽i支持多种型号,但兼容性差。给你发一个DH11专用库试试。

好的,我试试
回复

使用道具 举报

10#
ID:1101498 发表于 2023-11-29 22:39 | 只看该作者
glinfei 发表于 2023-11-29 10:52
程序没看出问题,那只能是硬件或联结问题吧?

不知道哦,但是我一开始有成功过,但是后面重新试了一下,就不行了
回复

使用道具 举报

11#
ID:1101498 发表于 2023-11-29 23:27 | 只看该作者

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

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表