调整Raspberry Pi Zero上DHT22的温度输出值技术求助
Fixing DHT22 Temperature Offset from Raspberry Pi Zero Heat
Got it, let's tackle that temperature offset problem you're facing with your DHT22 sensor. Since the Pi Zero's heat is making the sensor read 5°C higher than the actual ambient temperature, we just need to add a simple correction to subtract that 5-degree bias from the raw reading.
Here's the modified script with the fix, plus a few defensive checks to handle cases where the sensor might fail to read (to avoid runtime errors):
streamer = Streamer(bucket_name=BUCKET_NAME, bucket_key=BUCKET_KEY, access_key=ACCESS_KEY) while True: humidity, temp_c = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 4) # Apply temperature correction: subtract 5°C to account for Pi Zero heat if temp_c is not None: corrected_temp_c = temp_c - 5.0 else: corrected_temp_c = temp_c # Keep None value if sensor read fails if METRIC_UNITS: streamer.log(SENSOR_LOCATION_NAME + " Temperature(C)", corrected_temp_c) else: # Use corrected Celsius value to calculate accurate Fahrenheit temp_f = format(corrected_temp_c * 9.0 / 5.0 + 32.0, ".2f") streamer.log(SENSOR_LOCATION_NAME + " Temperature(F)", temp_f) # Add defensive check for humidity before formatting if humidity is not None: humidity = format(humidity,".2f") streamer.log(SENSOR_LOCATION_NAME + " Humidity(%)", humidity) streamer.flush() time.sleep(60*MINUTES_BETWEEN_READS)
Key Changes Explained:
- Temperature Correction: We added a check to ensure
temp_cisn'tNone(sinceread_retrycan sometimes fail to get a valid reading) before subtracting 5°C. This prevents aTypeErrorif the sensor returns no data. - Consistent Unit Adjustment: The Fahrenheit calculation now uses the corrected Celsius value, so both metric and imperial outputs will reflect the accurate ambient temperature.
- Humidity Safety Check: Added a quick validation for
humidityto avoid formatting errors if the humidity reading fails — it's a small defensive tweak to make the script more robust.
That should give you an accurate ambient temperature, accounting for the heat from your Pi Zero's case-mounted sensor!
内容的提问来源于stack exchange,提问作者Peter Kold
相关产品推荐
相关产品推荐

