读取DHT22温湿度时返回NaN的问题排查求助
Hey there! Sorry to hear you're stuck getting nothing but NaN when trying to read your DHT22 sensor—let's walk through the most likely issues and how to fix them.
1. Wiring Issues (The #1 Culprit)
Even with a wiring diagram, small mistakes can throw everything off:
- Missing or incorrect pull-up resistor: DHT22 requires a 4.7kΩ to 10kΩ pull-up resistor between the DATA pin and VCC. Without this, the sensor can't send stable signals, leading to failed reads.
- Loose or misconnected wires: Check that VCC is hooked to 3.3V or 5V (both work, but 3.3V is safer for boards like Raspberry Pi), GND is properly grounded, and DATA is connected to the correct pin in your code. Double-check pin labels—DHT22 pins are usually: 1=VCC, 2=DATA, 3=NC, 4=GND.
- Bad cable quality: Cheap Dupont wires can have hidden breaks or poor contacts. Try swapping out the DATA wire for a new one.
2. Hardware Failures
Sometimes the sensor itself is the problem:
- Damaged sensor: If it's been dropped, exposed to extreme temperatures, or gotten wet beyond its waterproof rating, it might be dead.
- Pin oxidation: Over time, sensor pins can corrode, leading to bad connections. Clean the pins with a small amount of isopropyl alcohol and a toothpick if needed.
- Unstable power: If your board's power supply is flaky (like a cheap phone charger for Raspberry Pi), the sensor might not get consistent voltage to operate.
Fix: Try testing with a brand-new DHT22 if you have one. Use a multimeter to check that VCC is delivering the correct voltage (3.3V or 5V) to the sensor.
3. Code & Library Problems
Your code might be missing key details or using the wrong tools:
- Wrong sensor type in code: If you initialized the library for DHT11 instead of DHT22, it'll fail to parse the sensor's signals. Double-check lines like
#define DHTTYPE DHT22(Arduino) orsensor = Adafruit_DHT.DHT22(Python). - Incorrect pin number: Mixing up board pin numbering schemes (like BCM vs. BOARD on Raspberry Pi) is super common. Make sure the pin in your code matches the physical pin you connected DATA to.
- Outdated or broken library: Old versions of libraries like Adafruit DHT can have bugs. Update your library via the Arduino Library Manager or run
pip install --upgrade Adafruit_DHTfor Python. - Timing conflicts: DHT22 is picky about read timing. If your code has long delays, multithreading, or other heavy operations between reads, it can mess up the sensor's communication.
Fix: Start with a minimal test code to eliminate variables. Here are examples for common platforms:
Arduino Minimal Test Code
#include <DHT.h> #define DHTPIN 2 // Replace with your DATA pin #define DHTTYPE DHT22 // Make sure this matches your sensor DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); dht.begin(); } void loop() { delay(2000); // Wait at least 2 seconds between reads float humidity = dht.readHumidity(); float temp = dht.readTemperature(); if (isnan(humidity) || isnan(temp)) { Serial.println("Failed to read from DHT sensor!"); return; } Serial.print("Humidity: "); Serial.print(humidity); Serial.print(" % | "); Serial.print("Temperature: "); Serial.print(temp); Serial.println(" °C"); }
Raspberry Pi Python Minimal Test Code
import Adafruit_DHT sensor = Adafruit_DHT.DHT22 pin = 4 # Replace with your BCM pin number # read_retry will attempt up to 15 reads before giving up humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) if humidity is not None and temperature is not None: print(f"Temp: {temperature:.1f}°C | Humidity: {humidity:.1f}%") else: print("Failed to get valid readings from the sensor.")
4. System-Level Issues (For Single-Board Computers Like Raspberry Pi)
- GPIO permission problems: Some libraries require root access to control GPIO pins. Run your Python script with
sudo(e.g.,sudo python3 dht_test.py). - GPIO pin conflicts: Check if the pin you're using is already occupied by another service (like I2C, SPI, or a built-in LED). Disable unused interfaces via
raspi-configif needed. - Electromagnetic interference: If the sensor is near motors, routers, or other high-noise devices, signal interference can cause failed reads. Move the sensor away from these sources or use a shielded cable for DATA.
Final Checks
If you've tried all the above, go back to your wiring diagram and verify every connection again—sometimes a tiny mistake (like swapping VCC and GND) is easy to miss.
内容的提问来源于stack exchange,提问作者Augusto Paiva




