dht22 and orenge pi zero


1 1

Recommended Posts

hi freinds
i'm trying to use a dht22  to read temperature and humidity ,
with Armbian 5.65 (stretch) 4.14.78-sunxi and orenge pi zero


orange pi state like this



#gpio sunxi
#gc2035 wire
i2c -dev

MIN SPEED=6000000



overlay prefix=sun8i-h3
overlays=usbhost2 usbhost3 12c0 wl-gpio
param wl_pin=PA10
param wl_pin

and im trying with this code

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)

dht22 work correct with raspberry and rasbian
also test ds18b20 with orange pi and work good but i need humidity sensor

i dont know what's the problem??!!

Thanks for any help

Link to post
Share on other sites

Donate and support the project!

You have a problem with the dht22 working. You never specify what the problem actually is. What's the error code you're getting?


Ima go ahead and take a wild guess that you don't initiate a configuration for that port.
put the following after gpio.init()


PIN2 =gpio.setcfg(PIN2, gpio.OUTPUT)

Link to post
Share on other sites

i dont have any error

the output is nothing and "time.sleep(1)" is runs.


the code is here



44 minutes ago, StuxNet said:

PIN2 =gpio.setcfg(PIN2, gpio.OUTPUT) 


defined in >>dht22.py

in class DHT22

import time
from pyA20.gpio import gpio
from pyA20.gpio import port
#import RPi

class DHT22Result:
    'DHT22 sensor result returned by DHT22.read() method'

    ERR_NO_ERROR = 0
    ERR_CRC = 2

    error_code = ERR_NO_ERROR
    temperature = -1
    humidity = -1

    def __init__(self, error_code, temperature, humidity):
        self.error_code = error_code
        self.temperature = temperature
        self.humidity = humidity

    def is_valid(self):
        return self.error_code == DHT22Result.ERR_NO_ERROR

class DHT22:
    'DHT22 sensor reader class for Raspberry'

    __pin = 0

    def __init__(self, pin):
        self.__pin = pin

    def read(self):
        gpio.setcfg(self.__pin, gpio.OUTPUT)

        # send initial high
        self.__send_and_sleep(gpio.HIGH, 0.05)

        # pull down to low
        self.__send_and_sleep(gpio.LOW, 0.02)

        # change to input using pull up
        #gpio.setcfg(self.__pin, gpio.INPUT, gpio.PULLUP)
	gpio.setcfg(self.__pin, gpio.INPUT)
	gpio.pullup(self.__pin, gpio.PULLUP)

        # collect data into an array
        data = self.__collect_input()

        # parse lengths of all data pull up periods
        pull_up_lengths = self.__parse_data_pull_up_lengths(data)

        # if bit count mismatch, return error (4 byte data + 1 byte checksum)
        if len(pull_up_lengths) != 40:
            return DHT22Result(DHT22Result.ERR_MISSING_DATA, 0, 0)

& ....


Link to post
Share on other sites

Got this same wrapper, as well as others to work on an NPiNeo and an OPiZ recently (defining outside ) but you're right it's prolly redundant.

I got it working using this wrapper (forked/updated within 1 year opposed to 2yr on the one u link to) https://github.com/jingl3s/DHT11-DHT22-Python-library-Orange-PI and the following script.

What happens when you print(instance) after it's defined?

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

import dht
import time
import datetime

# initialize GPIO
temPin = port.PG11
trigPin = port.PG7
gpio.setcfg(trigPin, gpio.OUTPUT)

# read data using pin 14
instance = dht.DHT(pin=temPin, sensor=22)

def tempOut():
    celToFar = result.temperature * 1.8 + 32
    print("Last valid input: " + str(datetime.datetime.now()))
    print("Celsius: {0:0.1f} C".format(result.temperature))
    print("Fahrenheit: {0:0.1f} F".format(celToFar))
    print("Humidity: {0:0.1f} %%".format(result.humidity))

def trigPinState(trigPin):    
    trigState = gpio.input(trigPin) # What is state of trigger pin?
    if trigState == 0: #Then it's low
        return 'Low: %s' % trigState
    elif trigState == 1: # then its high
        return 'High: %s' % trigState

while True:
    result = instance.read()
    if result.is_valid():
        if celToFar < 75:
            gpio.output(trigPin, gpio.HIGH)
        elif celToFar > 75:
            gpio.output(trigPin, gpio.LOW)



Link to post
Share on other sites

thanks your answer


i changed gpio like


# initialize GPIO
temPin = port.PA13
trigPin = port.PA14


when run program , say error

root@orangepizero:-/orangepi_PC_gpio_py113/MiT22-Python-library-Orange-PI# sudo python dht3 .py

File ''dht3 . py

a , line 32 print ( ' error ! )
Synt axError : EOL while scanning string literal


edit line 32 ('error!')


and then

root@orangepizero:-/DHT22-master# sudo python dht3.py

Last valid input: 2018-10-28 20:52:24.470819

Celsius: 26.5 C

Fahrenheit: 79.7 F

Humidity: 45.2 %%
Traceback (most recent call last):

File 'dht3.py', line 38, in <module>

if celToFar < 75: NameError: name 'celToFar' is not defined


i have question

what's trigPin and what to do this pin?

Link to post
Share on other sites

The script I gave you was a very loose example and it includes things I was using for my specific project. Things that you don't necessarily need to use for your own.


trigPin in this situation is just an 'empty' pin that I use as an LED trigger. If temperature is above X, turn on LED. If temp below X, turn LED off. 


If you just want a temp and humidity readout, remove every function, set pins how you'd like and just print tempOut()

Then go here: https://www.codecademy.com/learn/learn-python
Or here: https://www.codewars.com/?language=python

Or here: https://www.youtube.com/watch?v=apACNr7DC_s


Link to post
Share on other sites

This topic is now closed to further replies.

1 1