• person rss_feed

    Timothée Jaussoin’s feed

    Blog

    • chevron_right

      Connecting the DHT22 humidity and temperature sensors to my RPi

      Timothée Jaussoin · Sunday, 30 July, 2023 - 12:25 edit · 2 minutes · 6 visibility

    I bought a DHT22 #sensors to measure #temperature and #humidity in my house. You can find it in France on the Compozan website for a few euros.

    The DHT22 sensors

    Here are the steps that I followed to connect and install properly the sensors on my RPi B 2+.

    Connection

    The #DHT22 sensors have 3 wires that can be connected to the GPIO board of your RPi. For mine the pinout looks like this:

    My RPi pinout

    First turn-off your RPi and proceed with the connection.

    I already had an IR sensors connected so I picked 3 others ground/3.3v and GPIO pins on the board. For me it was the pins 17, 15 (so GPIO 22) and 14 but you can pick any configurations you want if you respect the type of connectors.

    Get and compile the kernel module

    Raspbian, the Linux operating system that I am using only have kernel module for the DHT11 sensors but hopefully I was able to find a working module on Github: krepost/dht22.

    Clone the repository and build the module:

    git clone https://github.com/krepost/dht22.git
    cd dht22
    make
    

    You will then have a dht22.ko binary that you will have to install and load properly.

    Install the kernel module

    To install the module for your kernel, copy the .ko file in your current kernel module directory:

    cp dht22.ko cd /lib/modules/$(uname -r)/
    

    Then run depmod to put it in the kernel modules list:

    depmod -a
    

    Configure the kernel module

    To tell the Linux kernel how to find the DHT22 sensors on the GPIO board we need to add a small option when loading the module. To do so create a specific configuration file for it in /etc/modprobe.d (you can give any name to it, I choose to name it simply using the module name).

    nano /etc/modprobe.d/dht22.conf
    

    And in the file just specifiy the GPIO pin where the signal is received:

    options dht22 gpiopin=22
    

    Load automatically the module on boot

    I am using systemd so I am loading the modules using the /etc/modules-load.d/modules.conf file.

    Just add a new line with the module name in it.

    dht22
    

    Save the file and reboot your RPi.

    Allow any user to read the Temperature and Humidity

    For now only root can read the values available in /dev/dht22. To allow any users to have access to it you can use the 99-dht22.rules files available in the GIT repository with the following content.

    # udev rules file for dht22 device driver; to be put in /etc/udev/rules.d/.
    SUBSYSTEM=="dht22", KERNEL=="dht22", MODE="0444"
    

    Then reboot your #RaspberryPi.

    🍓pi@edhelas-pi:~$ cat /dev/dht22 
     1690720540,46.6,26.4
    

    First value is the timestamp, second is the humidity in percentage, last one is the temperature in Celsius.

    Et voilà !

    Bonus

    The small Munin plugin that I wrote to track the two values during time

    $ cat /etc/munin/plugins/weather 
    #!/bin/sh
    
    case $1 in
       config)
            cat <<'EOM'
    graph_title Weather
    graph_vlabel weather
    graph_category weather
    graph_args --lower-limit -10
    graph_args --upper-limit 100
    graph_scale no
    temperature.label temperature
    humidity.label humidity
    EOM
            exit 0;;
    esac
    
    lines=$(sed 's/,/\n/g' /dev/dht22)
    
    printf "temperature.value "
    echo "$lines" | sed -n 3p
    printf "humidity.value "
    echo "$lines" | sed -n 2p
    
    • Pictures 1 image

    • visibility
    • favorite

      4 Like

      roughnecks, Lyn, Tristan, Angelica

    • 3 Comments

    • 30 July, 2023 roughnecks

      hey, there's my name under the like, wondering if comment works too (nice how-to btw)

    • 30 July, 2023 Lyn

      good #diy stuff

    • 1 August, 2023 Angelica

      I used it in a greenhouse. But I used Python library.