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.
Connection
Connect the DHT22 to the + 3.3v of the orange pi zero and for example to the PA06 pin as shown in the diagram.
Code
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
gpio.init()
# 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))
time.sleep(2)
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.
Hello,
when I try this linked guide “https://f1atb.fr/index.php/2020/10/03/dht22-and-orange-pi-zero/” and test I get as a return
”
root@orangepizero2:~/tryCode# python3 testLed.py
Segmentation fault
”
can you help me about it?
The exemple I presented, is for Orange Pi Zero. I don’t know if it works with an Orange Pi Zero2. Refer to the note at the bottom of the page : “The pyA20 cannot be used with all Orange PI models. If you use the OPI.GPIO library ….”
I see, thank you. I read your “Orange PI Zero 2 H616 GPIO” article. Is there any source of codes for controlling gpio out/input directly like that. Otherwise, OPI.GPIO Liblary is C and I like to use python in my project.
I mean wiringOP not OPI.GPIO. OPI.GPIO also avaible Python. wiringOp not support Python.
I have no library for DHT22 and OrangePi zero2.
You can use sysfs file system to access the GPIO but it’s too slow for DHT22.
My problem is get analog input with one of the liblaries. Such as using Dht22. Do you have any Documentation for Orange PI Zero 2 H616 GPIO with dht22.
I see, thank you. I read your “Orange PI Zero 2 H616 GPIO” article. Is there any source of codes for controlling gpio out/input directly like that. Otherwise, wiringOP Liblary is C and I like to use python in my project. And for analog input I couldnt find Doc wiringOP neither OPI.GPIO (fix)