please choosego to mobile | Continue to access the PC version
View: 13048|Reply: 2

Orange Pi Zero and DHT11 Sensor

[Copy link]

5

threads

12

posts

214

credits

Intermediate member

Rank: 3Rank: 3

credits
214
Published in 2017-2-12 03:43:59 | Show all floors |Read mode
Hello everyone,
I'm the beginner in programming. I need use gpio pins in my Orange Pi Zero. I found the pyA20 library: orangepi_PC_gpio_pyH3: https://github.com/duxingkei33/orangepi_PC_gpio_pyH3 ... I bought the DHT11 sensor, but I could not get it to work in any way. Is there anyone working on this subject? I love it if there are sample codes?
Thanks

10

threads

218

posts

2040

credits

Gold member

Rank: 6Rank: 6

credits
2040
Published in 2017-2-12 12:45:04 | Show all floors
Edited by nopnop2002 at 2017-2-12 13:10

A sample code of RPI+RPi.GPIO is here.

https://www.raspberrypi.org/forums/viewtopic.php?t=69427&p=505235


Transplantation  from RPi.GPIO to orangepi_PC_gpio_pyH3 is the following procedure.

**RPi.GPIO**
# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import time

COUNT = 3
PIN = 4
#GPIO.setmode(GPIO.BOARD)
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN,GPIO.OUT)

for _ in xrange(COUNT):
    GPIO.output(PIN,True)
    time.sleep(1.0)
    GPIO.output(PIN,False)
    time.sleep(1.0)

GPIO.cleanup()


**orangepi_PC_gpio_pyH3**
# -*- coding: utf-8 -*-
from pyA20.gpio import gpio
from pyA20.gpio import port
import time

COUNT = 3
PIN = port.PA6
gpio.init()
gpio.setcfg(PIN, gpio.OUTPUT)

for _ in xrange(COUNT):
    gpio.output(PIN,True)
    time.sleep(1.0)
    gpio.output(PIN,False)
    time.sleep(1.0)

#gpio.cleanup()

BTW
When you use WiringPi-Python-OP, the same cord as RPI works with OPI.

https://github.com/lanefu/WiringPi-Python-OP

$ git clone https://github.com/zhaolei/WiringOP.git -b h3
$ cd WiringOP/
$ sudo ./build
$ cd $HOME
$ git clone --recursive https://github.com/lanefu/WiringPi-Python-OP.git
$ sudo apt-get install python-dev python-setuptools swig
$ cd WiringPi-Python-OP
$ swig2.0 -python wiringpi.i
$ sudo python setup.py install
$ cd tests
$ sudo python test.py

5

threads

12

posts

214

credits

Intermediate member

Rank: 3Rank: 3

credits
214
 Author| Published in 2017-2-12 17:11:03 | Show all floors
Edited by phyesix at 2017-2-13 18:32

I tried to translate the sample code in the forum you sent. For example:

gpio.setup(PIN, gpio.IN, pull_up_down=gpio.PUD_UP)

For example, how to translate this code block? My code block:

  1. #!/usr/bin/env python
  2. from pyA20.gpio import gpio
  3. from pyA20.gpio import port
  4. from pyA20.gpio import connector
  5. from time import sleep

  6. def bin2dec(string_num):
  7.     return str(int(string_num, 2))
  8.    
  9. data = []

  10. #GPIO.setmode(GPIO.BCM)
  11. PIN = port.PA6

  12. gpio.init()
  13. gpio.setcfg(PIN, gpio.OUTPUT)

  14. gpio.output(PIN,gpio.HIGH)
  15. sleep(0.025)
  16. gpio.output(PIN,gpio.LOW)
  17. sleep(0.02)

  18. gpio.setup(PIN, gpio.IN, pull_up_down=gpio.PUD_UP)

  19. for i in range(0,500):
  20.     data.append(gpio.input(PIN))
  21.    
  22.    
  23. bit_count = 0
  24. tmp = 0
  25. count = 0
  26. HumidityBit = ""
  27. TemperatureBit = ""
  28. crc = ""


  29. try:
  30.    while data[count] == 1:
  31.       tmp = 1
  32.       count = count + 1
  33.         

  34.    for i in range(0, 32):
  35.       bit_count = 0
  36.       
  37.       while data[count] == 0:
  38.          tmp = 1
  39.          count = count + 1

  40.       while data[count] == 1:
  41.          bit_count = bit_count + 1
  42.          count = count + 1

  43.       if bit_count > 3:
  44.          if i>=0 and i<8:
  45.             HumidityBit = HumidityBit + "1"
  46.          if i>=16 and i<24:
  47.             TemperatureBit = TemperatureBit + "1"
  48.       else:
  49.          if i>=0 and i<8:
  50.             HumidityBit = HumidityBit + "0"
  51.          if i>=16 and i<24:
  52.             TemperatureBit = TemperatureBit + "0"
  53.             
  54. except:
  55.    print "ERR_RANGE"
  56.    exit(0)

  57.    
  58. try:
  59.    for i in range(0, 8):
  60.       bit_count = 0
  61.       
  62.       while data[count] == 0:
  63.          tmp = 1
  64.          count = count + 1

  65.       while data[count] == 1:
  66.          bit_count = bit_count + 1
  67.          count = count + 1

  68.       if bit_count > 3:
  69.          crc = crc + "1"
  70.       else:
  71.          crc = crc + "0"
  72. except:
  73.    print "ERR_RANGE"
  74.    exit(0)
  75.       
  76.       
  77. Humidity = bin2dec(HumidityBit)
  78. Temperature = bin2dec(TemperatureBit)

  79. if int(Humidity) + int(Temperature) - int(bin2dec(crc)) == 0:
  80.    print Humidity
  81.    print Temperature
  82. else:
  83.    print "ERR_CRC"
Copy code


In the meantime, as in the following photo:


You need to log in before you can reply login | Register

Points Rule

Quick reply Top Return list