Run DHT22 with RasPi + Python

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.

Module installation

Install MyPyDHT according to the web below.

https://github.com/freedom27/MyPyDHT

Python code

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")

result

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

Run DHT22 with RasPi + Python
Run prepDE.py with python3
Run Blender with python
Run iperf with python
Run python with PyCharm (Windows)
Run Python with CloudFlash (arm926ej-s)
Let's run Excel with Python
Run Rotrics DexArm with python API
Run mruby with Python or Blender
Run XGBoost with Cloud Dataflow (Python)
Run Aprili from Python with Orange
Run python3 Django1.9 with mod_wsgi (deploy)
Until you run python with apache
FizzBuzz with Python3
Scraping with Python
Run servo with Python on ESP32 (Windows)
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Run a Python web application with Docker
Excel with Python
Microcomputer with Python
Cast with python
Python> Run with run-time arguments> Use import argparse
Run VMware vSphere 6 vSphere API with Python script (pyvmomi)
Run Flask on CentOS with python3.4, Gunicorn + Nginx.
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Scraping with Python + PhantomJS
Drive WebDriver with python
[Python] Redirect with CGIHTTPServer
Voice analysis with python
Think yaml with python