DHT22 and Orange Pi Zero

The DHT22 is a temperature and humidity sensor that can be connected to nano computers such as the Orange PI.


Connect the DHT22 to the + 3.3v of the orange pi zero and for example to the PA06 pin as shown in the diagram.


For the installation of the Armbian system, see the following article: https://f1atb.fr/index.php/2020/08/06/getting-started-orange-pi-zero/

First for python 3, you have to update and add the pip installer, setuptools and python3-dev.

apt-get update
apt-get upgrade
apt install python3-pip
apt-get install python3-setuptools
apt-get install python3-dev

Go to the folder you want to use to install the GPIO and DHT libraries. Add sudo to the commands if you are not root.

git clone https://github.com/nvl1109/orangepi_zero_gpio.git
cd orangepi_zero_gpio
python3 setup.py install

Note: Be careful with the pyA20 library included in the above package, on an Orange PI Zero H2, the PA15 and PA16 pins are not accessible.

Return to your installation folder and clone the DHT temperature sensor library.

git clone https://github.com/jingl3s/DHT11-DHT22-Python-library-Orange-PI.git

Put a copy of the dht.py file of the library in the folder where you want to run the test program for your DHT sensor on pin PA6.

cp DHT11-DHT22-Python-library-Orange-PI/dht.py dht.py

Create the source test.py with nano.

nano test.py
from pyA20.gpio import gpio
from pyA20.gpio import port

import dht
import time
import datetime

# initialize GPIO
PIN2 = port.PA6

# read data using pin
instance = dht.DHT(pin=PIN2)

while True:
    result = instance.read()
    if result.is_valid():
        print("Last valid input: " + str(datetime.datetime.now()))
        print("Temperature: {0:0.1f} C".format(result.temperature))
        print("Humidity: {0:0.1f} %".format(result.humidity))


Test the set with:

python3 test.py

The output:

Last valid result: 2020-10-03 04:17:58.325014
Temperature: 23.3 C
Humidity: 57.6 %

Note on DHT22

DHT22 uses a proprietary standard to transmit the data in serial. It takes around 4ms to transmit the 40 bits of the results (16 bits for humidity, 16 bits for temperature, 8 bits for a check sum). A 0 bit corresponds to a 0 level during 50us then a 1 during 27us. A 1 bit corresponds to a 0 level during 50us then a 1 during 70us. To decode this 40 bits frame, you need to sample the GPIO pin at around 20us. If you use a library as pyA20 written in C, it works as it is described here. The pyA20 cannot be used with all Orange PI models. If you use the OPI.GPIO library in Python (for an Orange PI One Plus for example), it uses the sysfs file system to access the GPIO. You can sample an input at only 120us. It’s too slow to decode the serial frame of a DHT22.