I connected a module called "DSD TECH DHT22 Temperature / Humidity Sensor Module AM2302 Evolved Version of DHT11 for Arduino Raspberry Pi with Chip" sold by Amazon to a Raspberry Pi and measured the temperature.
Install MyPyDHT according to the web below.
https://github.com/freedom27/MyPyDHT
The exception below was occurring, but it seems that it can be avoided by setting "use_cache = True" to "MyPyDHT.sensor_read".
MyPyDHT.DHTException: A timeout occurred while attempting to read the sensor!
Python version:
$ python3 -V
Python 3.7.3
In the example below, the DAT terminal is connected to GPIO No. 4, and 5V is applied to VCC. In addition, the sensor is monitored every second and output to the terminal.
import time
import RPi.GPIO as GPIO
import MyPyDHT
DHT_PIN = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(DHT_PIN, GPIO.OUT)
try:
while True:
try:
humidity, temperature = MyPyDHT.sensor_read(MyPyDHT.Sensor.DHT22, DHT_PIN, use_cache=True)
if humidity is not None and temperature is not None:
print(f"temperature={temperature} / humidity={humidity}")
except MyPyDHT.DHTException as e:
print(f"err: {str(e)}")
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()
print("finish")
I measured it indoors with the air conditioner set to 16 degrees, so the results should be correct.
temperature=16.7 / humidity=44.1
temperature=16.8 / humidity=44.8
temperature=16.8 / humidity=44.8
Recommended Posts