[HowTo] Raspberry Pi Temperature and Humidity Sensor (DHT22/DHT11/AM2302)

Difficulty: ★★☆☆☆

There is some misleading advice out there, suggesting the need for a 10k ohm resistor. But the the three lead DHT22 comes with an inbuilt resistor, which seems to do the job, also the majority of advice is for RaspberryPI OS. I thought I would share my experience:

  1. I suggest connecting to pin 1 for 3.3 volt supply (RaspberryPi3/4), pin 11 for gpio17 and pin 9 for gnd.

  2. You should also install software from Adafruit, use pacman to install pip and gcc if needed.

    sudo pip3 install Adafruit_DHT
    
  3. You need a python script to access the sensor

    import Adafruit_DHT
    
    DHT_SENSOR = Adafruit_DHT.AM2302
    DHT_PIN = 17
    
    while True:
        humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
    
        if humidity is not None and temperature is not None:
            print("Temp={0:0.1f}*C  Humidity={1:0.1f}%".format(temperature, humidity))
        else:
            print("Failed to retrieve data from humidity sensor")
    
  4. Unfortunately you need root permission to access the sensor, therefore I:

    • copied 60-rpi.gpio-common.rules from RaspberryOS to /lib/udev/rules.d
      SUBSYSTEM=="bcm2835-gpiomem", KERNEL=="gpiomem", GROUP="dialout", MODE="0660"
      SUBSYSTEM=="gpio", KERNEL=="gpiochip*", ACTION=="add", PROGRAM="/bin/sh -c 'chown root:dialout /sys/class/gpio/export /sys/class/gpio/unexport ; chmod 220 /sys/class/gpio/export /sys/class/gpio/unexport'"
      SUBSYSTEM=="gpio", KERNEL=="gpio*", ACTION=="add", PROGRAM="/bin/sh -c 'chown root:dialout /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value ; chmod 660 /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value'"
      
    • created a dialout group and added myself as user, and now I don’t need root privileges.
      groupadd dialout
      usermod --append dialout $USER
      
1 Like

5 posts were split to a new topic: How to install pip on ARM