- Raspberry Pi
- DHT22, DHT11 or AM2302
- Power supply
- Female-female breaker wires
- Install Raspberry as described in another post.
- Update Raspberry:
sudo apt-get update, sudo apt-get upgrade.
- Install necessary tools:
sudo apt-get install build-essential python3-dev python3-openssl python3-setuptools git
- Download Adafruit DHT library:
cd ~ git clone https://github.com/adafruit/Adafruit_Python_DHT.git cd Adafruit_Python_DHT
- Install Adafruit library:
sudo python3 setup.py install
Before connecting anything to Raspberry, I prefer to power it down:
sudo shutdown now.
Connect DHT22 pins 1 (VCC), 2 (data) and 4 (GND) to Raspberry pins 3.3 V, GPIO 4 and GND respectively.
Note that for DHT22 module (bare PCB), an additional pull-up resistor of a few kOhm is required between pins 1 and 2.
For AM2302, red wire is Vcc (3.3 or 5 V), yellow is data, black is ground.
Once everything is connected, power up Raspberry.
Command line interface:
cd ~/Adafruit_Python_DHT/examples sudo ./AdafruitDHT.py 22 4
Here 22 is the DHT model (for DHT11 and AM2302, it would be “11” and “2302”), 4 is the GPIO pin of Raspberry we used previously.
The above command will print the measured temperature and humidity.
# For other sensors, replace "DHT22" with "DHT11" or "AM2302" # (By) Kotya Karapetyan, 2019-01-25. from Adafruit_DHT import read_retry, DHT22 from time import sleep PIN = 4 while True: h, t = read_retry(DHT22, PIN) print("Humidity: %.1f, temperature: %.1f" % (h, t)) sleep(5) # sleep 5 s (allowed min 2 s)
python3 test.py to output humidity and temperature every 5 seconds. Note that these sensors cannot output data more frequently than each 2 seconds (0.5 Hz).