Temperature sensor on Orange Pi with Python code

In my previous IoT project on the Orange Pi I showed how to read a DHT22 or a DHT11 temperature and humidity sensor. That project involved connecting the sensor to the GPIO pins and writing C code to read the sensor. In this example I will show you how do the same, but using Python code instead.

Again, you will need:

Have a look at the previous post to see how to connect the sensor to the GPIO pins of the Orange Pi. Now we want to use Python to read the sensor. The solution is based on Vitalik-Samets’ code for the DHT11 on the Orange Pi, which in turn uses the Allwinner H3 GPIO Python library written by duxingkei33. To make the code work with the DHT22, I changed the readings returned by the sensors from these:

return DHT11Result(DHT11Result.ERR_NO_ERROR,

To these:

return DHT11Result(DHT11Result.ERR_NO_ERROR, 
(((the_bytes[2] & 0x7F)<<8)+the_bytes[3])/10.00,

After that I renamed the DHT11 variable names to DHT22. I also changed the data type of the readings from integer to float. To install the library, first clone and install the H3 GPIO Python git repository:

git clone https://github.com/duxingkei33/orangepi_PC_gpio_pyH3
cd orangepi_PC_gpio_pyH3
sudo python setup.py install

Then clone the DHT22 library:

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

To use the example code:

cd DHT22-Python-library-Orange-PI
sudo python dht22_example.py

If you have a DT11 sensor, simply clone the original repository.

In noticed that I can only use the pyA20 library if I run Python as sudo. The example code that reads the sensor values:

from pyA20.gpio import gpio
from pyA20.gpio import port

#import RPi.GPIO as GPIO
import dht22
import time
import datetime

# initialize GPIO
PIN2 = port.PA6

# read data using pin 14
instance = dht22.DHT22(pin=PIN2)

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


The code above should output something like this:

Store and visualize sensor data in SQLite
Personal cloud storage on the Orange Pi


  1. Hi..
    I tried your code for DHT22 sensor..it working.
    But did u realised that it do not know to read (show) negativ value?
    For example every reading below zero it do the absolute value of number and giving positive value.
    Can u fix the reading?

  2. I changed the script..
    If someone interested he can find the solution described on next link:

  3. is there a way to run this on the OPI.GPIO Library?

Leave a Reply

Your email address will not be published / Required fields are marked *