Last Updated on Aug 16, 2021
Home automation projects often there is a need to measure the temperature and humidity of the air. This can be done with e.g. the DHT11 or the more advanced DHT22 sensors.
The AM2302 is a wired version of the DHT22, in a large plastic body. There is a 5.1K resistor inside the sensor connecting VCC and DATA so you do not need any additional pullup resistors. Here’s how to make it work.
cd ~ git clone https://github.com/adafruit/Adafruit_Python_DHT.git cd Adafruit_Python_DHT sudo apt-get update sudo apt-get install build-essential python-dev python-openssl sudo python setup.py install
Note that the library doesn’t use the WiringPi numbering, nor the physical, but it uses the BCM numbering!
Handy tool to check it http://pinout.xyz/
My AM2302 Data wire is wired to physical PIN 3, which is WiringPi pin 8, and BCM pin 2 :D So we put in 2 as a variable here.
cd examples sudo ./AdafruitDHT.py 2302 2
create a symlink in /usr/bin so “AdafruitDHT” becomes a command in the shell:
sudo ln -s /home/pi/Adafruit_Python_DHT/examples/AdafruitDHT.py /usr/bin/AdafruitDHT
It should look like this:
pi@433v4:~ $ ls -la /usr/bin/AdafruitDHT lrwxrwxrwx 1 root root 52 Jun 8 20:47 /usr/bin/AdafruitDHT -> /home/pi/Adafruit_Python_DHT/examples/AdafruitDHT.py
Then you can test the newly created command. Note that you still need to use sudo!
pi@433v4:~ $ AdafruitDHT 2302 2 Traceback (most recent call last): File "/usr/bin/AdafruitDHT", line 41, in humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) File "build/bdist.linux-armv7l/egg/Adafruit_DHT/common.py", line 90, in read_retry File "build/bdist.linux-armv7l/egg/Adafruit_DHT/common.py", line 77, in read File "build/bdist.linux-armv7l/egg/Adafruit_DHT/Raspberry_Pi_2.py", line 34, in read RuntimeError: Error accessing GPIO. Make sure program is run as root with sudo!
We need sudo!
pi@433v4:~ $ sudo AdafruitDHT 2302 2 Temp=24.4* Humidity=43.6%
It works, enjoy!