Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Pure Python library for reading DHT sensor on Pycom boards

    Scheduled Pinned Locked Moved Discussion
    dht dht11 dth22
    57 Posts 8 Posters 29.5k Views 9 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • robert-hhR Offline
      robert-hh @Jurassic Pork
      last edited by

      @Jurassic-Pork Just one small change:
      The initial high period of 25 ms is too short. That had the effect that the first call failed, and it worked only from the second call on. I had to extend that to 500 ms. You can also set the Pin high in init() and skip the call.
      I also simplified __collect_input(), saving quite some memory.

          def __collect_input(self):
              m = bytearray(800)        # needs long sample size to grab all the bits from the DHT
              irqf = disable_irq()
              self.__pin(1)
              for i in range(len(m)):
                  m[i] = self.__pin()      ## sample input and store value
              enable_irq(irqf)
              return m
      

      I do not think that the LoPy is faster. I assume that the sensor I issues a different collections of 1s and 0s, either caused by the different coding or the environmental conditions.

      Jurassic PorkJ 2 Replies Last reply Reply Quote 0
      • Jurassic PorkJ Offline
        Jurassic Pork @robert-hh
        last edited by Jurassic Pork

        @robert-hh
        ok , thanks robert .
        i have made this changes in the dht.py :
        1 - in the init :
        def init(self, pin, sensor=0):
        self.__pin = pin
        self.__dhttype = sensor
        self.__pin(1)
        time.sleep(0.5)

        and also comment this in the read method :
        #self.__send_and_sleep(1, 0.025)
        2 - in the collect_input method :
        m = bytearray(800)

        bytearray(800) ---> 857 bytes
        [1]*800 --> 6464 bytes

        With my SiPy and my dth11 sensor no error in a loop of 100 measures.

        1 Reply Last reply Reply Quote 0
        • L Offline
          livius @Jurassic Pork
          last edited by

          @Jurassic-Pork
          I am realy do not understand why all do this in this way

          for i in range(len(m)):
                      m[i] = self.__pin()
          

          i know that time is critical
          but why not do this with really shorter buffer
          I know that time is critical here but inc should work fast
          this is not whole algorithm but:

          while  self.__pin():
             m[i]+=1
          while not self.__pin():
            m[i]-=1
          

          and result buffer will be e.g.

          8, -7, 14, ...
          

          think about people first, then computers

          robert-hhR 1 Reply Last reply Reply Quote 0
          • robert-hhR Offline
            robert-hh @livius
            last edited by robert-hh

            @livius The shortest event to be captured is represented by 3 times calling __pin(). What you suggest is in addition to the pin read and loop maintenance adding the load/inc/store operation, not to mention the bookkeeping for the index I between the loops. It still could be feasible, so it's to be tested, and not to mention the additional code required to avoid a lock-up situation.

            1 Reply Last reply Reply Quote 0
            • RobTuDelftR Offline
              RobTuDelft
              last edited by

              Great! Now working on the Lopy.
              Thanks a lot @Jurassic-Pork & @robert-hh!

              PYB: soft reboot                                                                                                        
              ==========Starting main.py==========                                                                                    
                                                                                                                                      
              <DTHResult object at 3ffe2ed0> <class 'DTHResult'>                                                                      
              True                                                                                                                    
              Temperature: 21.70                                                                                                      
              Humidity: 57.50   
              

              Without data you're just another person with an opinion.
              Hackster | About me

              G RobTuDelftR 2 Replies Last reply Reply Quote 0
              • Jurassic PorkJ Offline
                Jurassic Pork @robert-hh
                last edited by

                hello,
                @robert-hh said in Pure Python library for reading DHT sensor on Pycom boards:

                @Jurassic-Pork Just one small change:
                The initial high period of 25 ms is too short. That had the effect that the first call failed, and it worked only from the second call on. I had to extend that to 500 ms. You can also set the Pin high in init() and skip the call.

                why 500ms ? the only specification that i have seen for this is :
                When power is supplied to sensor, don't send any instruction to the sensor within one second to pass unstable status.

                For this a time.sleep of one second in the constructor of the class (init) ?
                and :
                Is the time.sleep of one second at the beginning of the read method is still necessary ?

                specification for the start signal of the AM2302 sensor : min : 0.8 ms typical : 1 ms max : 20 ms
                specification for the start signal of the DTH11 sensor : must be > 18 ms
                So i will change the start signal value with a value of 19 ms which does the job for the two sensors.

                robert-hhR 1 Reply Last reply Reply Quote 0
                • robert-hhR Offline
                  robert-hh @Jurassic Pork
                  last edited by

                  @Jurassic-Pork the 500 ms were just found by trial. It may be more. Just ensure that the data line is high during that period. If you have a 1s delay in the init, the delay in read is not required.
                  About the length of the start signal: Just give it a try. I did not check for the maximum length accepted by the AM2302. Only 40 ms was too long, and previous tests with 20ms worked. You could make it dependent on the sensor type, since that is specified at init time.

                  Jurassic PorkJ 1 Reply Last reply Reply Quote 1
                  • Jurassic PorkJ Offline
                    Jurassic Pork @robert-hh
                    last edited by Jurassic Pork

                    @robert-hh
                    i have made a few changes in the dth.py code :

                    class DTH:
                        'DHT sensor (dht11, dht21,dht22) reader class for Pycom'
                    
                        #__pin = Pin('P3', mode=Pin.OPEN_DRAIN)
                        __dhttype = 0
                    
                        def __init__(self, pin, sensor=0):
                            self.__pin = pin
                            self.__dhttype = sensor
                            self.__pin(1)
                            time.sleep(1.0)
                    
                    
                        def read(self):
                            #time.sleep(1)
                    
                            # send initial high
                            #self.__send_and_sleep(1, 0.025)
                    
                            # pull down to low
                            self.__send_and_sleep(0, 0.019)
                    

                    1 - no more default value for the pin
                    2 - a sleep time of one second in the constructor (init) code.
                    3 - no more sleep time at the beginning of the read method
                    4 - a sleep time of 19 ms for the startup signal. This value seems to be OK for the specifications of DTH11 AND AM2302.

                    no error in a program test of 10000 measures with my SiPy module and a DTH11 sensor;

                    1 Reply Last reply Reply Quote 2
                    • G Offline
                      geda @RobTuDelft
                      last edited by

                      @RobTuDelft , I can read values from the DHT22 with the lopy...
                      Did you use exactly the same code that @Jurassic-Pork has commited on github ?

                      1 Reply Last reply Reply Quote 0
                      • RobTuDelftR Offline
                        RobTuDelft @RobTuDelft
                        last edited by

                        @geda Yes works for me too :)

                        @RobTuDelft said in Pure Python library for reading DHT sensor on Pycom boards:

                        Great! Now working on the Lopy.
                        Thanks a lot @Jurassic-Pork & @robert-hh!

                        PYB: soft reboot                                                                                                        
                        ==========Starting main.py==========                                                                                    
                                                                                                                                                
                        <DTHResult object at 3ffe2ed0> <class 'DTHResult'>                                                                      
                        True                                                                                                                    
                        Temperature: 21.70                                                                                                      
                        Humidity: 57.50   
                        

                        Without data you're just another person with an opinion.
                        Hackster | About me

                        G 1 Reply Last reply Reply Quote 0
                        • G Offline
                          geda @RobTuDelft
                          last edited by

                          @RobTuDelft Sorry, I cannot read values ... so exactly the same code has work for you ? May be my DHT22 is broken ...

                          Jurassic PorkJ robert-hhR RobTuDelftR 3 Replies Last reply Reply Quote 0
                          • Jurassic PorkJ Offline
                            Jurassic Pork @geda
                            last edited by Jurassic Pork

                            @geda
                            have you a dht22 module or simply a dht22 component ?
                            have you a pullup resistor on the signal pin of your dht22 ?
                            what pin of your lopy are you using for the dht22 ?
                            hello robert-hh same idea :-) in the same time

                            1 Reply Last reply Reply Quote 0
                            • robert-hhR Offline
                              robert-hh @geda
                              last edited by

                              @geda Which Pin are you using. Note that P13..P18 are input only and cannot be used for that purpose.
                              see here: https://docs.pycom.io/pycom_esp32/_downloads/lopy_pinout.pdf

                              1 Reply Last reply Reply Quote 0
                              • RobTuDelftR Offline
                                RobTuDelft @geda
                                last edited by

                                @geda Yes, I use the latest library from @Jurassic-Pork.

                                This example works for me, maybe that helps.

                                I used a 4k7 resistor between data and +.

                                AM2302 circuit

                                Without data you're just another person with an opinion.
                                Hackster | About me

                                G R 2 Replies Last reply Reply Quote 1
                                • G Offline
                                  geda @RobTuDelft
                                  last edited by geda

                                  @RobTuDelft Thanks ! It works ! I made a mistake on my breadboard and had the wrong GPIO on my extension board.
                                  Thanks for your help, I have really appreciated it !

                                  Temperature: 23.30
                                  Humidity: 43.90  

                                  And thanks to Jurassic Pork for porting the library

                                  1 Reply Last reply Reply Quote 0
                                  • palmy-nzP Offline
                                    palmy-nz
                                    last edited by palmy-nz

                                    I am still getting false. Using DHT11, pins double checked and, module works on arduino.

                                    using @RobTuDelft 's main.py, but changed th = DTH(Pin('P4', mode=Pin.OPEN_DRAIN),0)
                                    as using GHT11 on Pin G11 on expansion board

                                    any ideas?
                                    Thanks guys

                                    Jurassic PorkJ L 2 Replies Last reply Reply Quote 1
                                    • Jurassic PorkJ Offline
                                      Jurassic Pork @palmy-nz
                                      last edited by

                                      hello,
                                      @palmy-nz
                                      what is your power supply for your DHT11 ? 5 v or 3,3 v ?
                                      are you using my library here ?
                                      have you a pull up resistor to 3,3v on the pin signal of your DHT11 ?
                                      have you try another expand board pin ? for example P3

                                      Friendly, J.P

                                      palmy-nzP 1 Reply Last reply Reply Quote 0
                                      • palmy-nzP Offline
                                        palmy-nz @Jurassic Pork
                                        last edited by

                                        Hi @Jurassic-Pork, yes I am using your dht library.
                                        and yes I have tried pulling it up to 3.3v and 5v, and have tried other pins.
                                        Not sure what the problem is.
                                        Here is my main py: https://gist.github.com/tairea/3607708a403b7501de142d125b07b7b8
                                        and photo of set up: https://pasteboard.co/1p8ETKipZ.jpg

                                        1 Reply Last reply Reply Quote 1
                                        • L Offline
                                          livius @palmy-nz
                                          last edited by livius

                                          @palmy-nz said in Pure Python library for reading DHT sensor on Pycom boards:

                                          Pin G11 on expansion board

                                          G11? If is see this right
                                          in code you have P3 which is G24 not G11.
                                          G11 is P4.
                                          Or did you changed it from previous post?
                                          Image quality is not enough to say for sure.

                                          P.S. i have upvoted your posts then you can now write faster - and maybe you will be allowed to post images directly

                                          think about people first, then computers

                                          palmy-nzP 1 Reply Last reply Reply Quote 0
                                          • palmy-nzP Offline
                                            palmy-nz @livius
                                            last edited by

                                            @livius I was trying other pins in the code example and photo and am making sure they match. Has to be something else?

                                            Jurassic PorkJ 1 Reply Last reply Reply Quote 0

                                            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                                            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                                            With your input, this post could be even better 💗

                                            Register Login
                                            • 1
                                            • 2
                                            • 3
                                            • 3 / 3
                                            • First post
                                              Last post

                                            Pycom on Twitter

                                            Pycom