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

    SI7021 I2C pin setup (SOLVED)

    Scheduled Pinned Locked Moved MicroPython
    micropythonsi7021i2c
    59 Posts 4 Posters 13.2k Views 4 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.
    • T Offline
      tttadam
      last edited by tttadam

      Re: SI7021 I2C sensor

      Hi there,

      I would like to use a SI7021 sensor. I found this code which suppose to work, but I can't find where does the I2C pin setup happening. So where could a I change the code to use GPIO06 and 07?

      Thank you in advance :)

      E 1 Reply Last reply Reply Quote 0
      • E Offline
        Eric73 @tttadam
        last edited by

        @tttadam
        As nothing is set in i2c = I2C(0, I2C.MASTER, baudrate=100000), I2C0 use default pin(P9,P10), if you would like to use other pin for I2C you have to use
        i2c = I2C(0, I2C.MASTER, baudrate=100000, pins=('P23','P22'))

        Note: P23 and P22 are for example only, you have to check with your board hardware documentation what is the logical pin number of GPIO06 and GPIO7 (for Lopy4 this pins are not available and P23 is GPIO14 and P22 is GPIO25)

        T 1 Reply Last reply Reply Quote 2
        • T Offline
          tttadam @Eric73
          last edited by

          @eric73 I am trying to use crankshaft solution from the last comment.
          there what I found is
          self.i2c = i2c.
          This is the line what I suppose to replace with
          self.i2c = I2C(0, I2C.MASTER, baudrate=100000, pins=('P23','P22'))

          And after that how can I use it?
          I put this code to an other .py file
          my main.py look like this:
          import SI7021
          print(SI7021.readTemp())
          print(SI7021.readRH())
          But for that I got an error:
          AttributeError: 'module' object has no attribute 'readTemp'

          E 1 Reply Last reply Reply Quote 0
          • E Offline
            Eric73 @tttadam
            last edited by

            @tttadam Sorry, it's wrong place to do your change, create your i2c object in your main.py and then use it as arguments when you create your SI7021 object. P22 and P23 are for example only, use the rigth pin of your board instead.

            from machine import I2C
            import SI7021
            Bus=I2C(0, I2C.MASTER, baudrate=100000, pins=('P23','P22'))
            Sensor=SI7021(i2c=Bus)
            print(Sensor.readTemp())
            
            T 1 Reply Last reply Reply Quote 2
            • T Offline
              tttadam @Eric73
              last edited by

              @eric73

              Something is still wrong...
              This is my code:

              import pycom
              from machine import I2C
              import SI7021
              
              # i2c = I2C(0, I2C.MASTER, baudrate=100000, pins=(Pin.exp_board.G7,Pin.exp_board.G8))
              bus = I2C(0, I2C.MASTER, baudrate=100000, pins=("P7","P8"))
              sensor = SI7021(i2c=bus)
              print(sensor.readTemp())
              print(sensor.readRH())
              

              and this is the error what I get.

               Traceback (most recent call last):
                File "main.py", line 12, in <module>
              TypeError: 'module' object is not callable
              robert-hhR 1 Reply Last reply Reply Quote 0
              • robert-hhR Offline
                robert-hh @tttadam
                last edited by

                @tttadam I see 9 lines of code, the error is flagged in line 12 of main.py. What is the content of line 12 or line 11?

                T 1 Reply Last reply Reply Quote 1
                • T Offline
                  tttadam @robert-hh
                  last edited by

                  @robert-hh My bad, so sorry. The error is in this line:

                  sensor = SI7021(i2c=bus)
                  
                  robert-hhR 1 Reply Last reply Reply Quote 0
                  • robert-hhR Offline
                    robert-hh @tttadam
                    last edited by robert-hh

                    @tttadam Try:

                    sensor = SI7021.SI7021(i2c=bus)
                    

                    = class SI7021 in module SI7021. Quite confusing at the beginning. You could also have imported the class as:
                    from SI7021 import SI7021, then the initial code would work.

                    T 1 Reply Last reply Reply Quote 2
                    • T Offline
                      tttadam @robert-hh
                      last edited by

                      @robert-hh Now I am getting a totaly different error, so I think We are getting somewhere.
                      I checked the wiring multiple times. That should be okay.
                      VIN-->VIN
                      GND-->GND
                      G8-->SCL
                      G7-->SDA)

                      Traceback (most recent call last):
                        File "main.py", line 14, in <module>
                        File "/flash/lib/SI7021.py", line 31, in readTemp
                        File "/flash/lib/SI7021.py", line 28, in write_command
                      OSError: I2C bus error
                      

                      Line 14 is this: print(sensor.readTemp())

                      import pycom
                      import time
                      from machine import I2C, Pin
                      from dth import DTH
                      from SI7021 import SI7021
                      
                      bus = I2C(0, I2C.MASTER, baudrate=100000, pins=(Pin.exp_board.G8,Pin.exp_board.G7))
                      # bus = I2C(0, I2C.MASTER, baudrate=100000, pins=("P8","P7"))
                      sensor = SI7021(i2c=bus)
                      print(sensor.readTemp())
                      print(sensor.readRH())
                      
                      E robert-hhR 2 Replies Last reply Reply Quote 0
                      • E Offline
                        Eric73 @tttadam
                        last edited by Eric73

                        @tttadam Have you add a 1K or 4K7 or 10K pull-up resistor on G8 and G7 ?
                        What is the board you use ?

                        T 1 Reply Last reply Reply Quote 1
                        • T Offline
                          tttadam @Eric73
                          last edited by

                          @eric73

                          Oh, I did not. I just tried out the built in resistances, but doesn't make a difference, still getting the same error.
                          I am using right now a lopy4 with ExpansionBoard V3r (But in the final product I will use a pytrack board)

                          import pycom
                          import time
                          from machine import I2C, Pin
                          from dth import DTH
                          from SI7021 import SI7021
                          
                          bus = I2C(0, I2C.MASTER, baudrate=100000, pins=(Pin(Pin.exp_board.G8,mode= Pin.OPEN_DRAIN, pull= Pin.PULL_UP),Pin(Pin.exp_board.G7,mode= Pin.OPEN_DRAIN, pull= Pin.PULL_UP)))
                          # bus = I2C(0, I2C.MASTER, baudrate=100000, pins=("P8","P7"))
                          sensor = SI7021(i2c=bus)
                          print(sensor.readTemp())
                          print(sensor.readRH())
                          
                          1 Reply Last reply Reply Quote 0
                          • robert-hhR Offline
                            robert-hh @tttadam
                            last edited by robert-hh

                            @tttadam On the expansion board G7 and G8 are also connected to CTS and RTS of the USB/UART bridge. If you want to use them for other means, you have to remove the respective jumpers.

                            T 1 Reply Last reply Reply Quote 2
                            • T Offline
                              tttadam @robert-hh
                              last edited by

                              @robert-hh Okay, I changed the pins to 5 and 4. I am still getting the same error, but now it takes some time, like a second or so. Before that a I got the error immediately.

                              Traceback (most recent call last):
                                File "main.py", line 16, in <module>
                                File "/flash/lib/SI7021.py", line 31, in readTemp
                                File "/flash/lib/SI7021.py", line 28, in write_command
                              OSError: I2C bus error
                              
                              pin5 = Pin(Pin.exp_board.G5,mode= Pin.OPEN_DRAIN, pull= Pin.PULL_UP)
                              pin4 = Pin(Pin.exp_board.G4,mode= Pin.OPEN_DRAIN, pull= Pin.PULL_UP)
                              bus = I2C(0, I2C.MASTER, baudrate=100000, pins=(pin5,pin4))
                              # bus = I2C(0, I2C.MASTER, baudrate=100000, pins=("P8","P7"))
                              sensor = SI7021(i2c=bus)
                              print(sensor.readTemp())
                              print(sensor.readRH())
                              
                              

                              line16 is this: print(sensor.readTemp())

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

                                @tttadam G5 and G4 aka P13 and P14 are input only ports. I wonder that it does not give an error immedetaly when declaring. Why don't you use the default pins G16 and G17 aka "P9" for SDA and "P10" for SCL, at least for a first test?
                                Edit: You do not have to initialize the pins yourself. In the i2c constructor, you can write ... pins=("P9", "P10") or pins=("G16", "G17").

                                T 1 Reply Last reply Reply Quote 1
                                • T Offline
                                  tttadam @robert-hh
                                  last edited by

                                  @robert-hh Yes You are right, I should thought about this earlier.
                                  So I modified my code like this. Something is still wrong, because I still get the same error.
                                  My pins are correct, now but I took a picture just in case.

                                  sensor = SI7021(i2c=I2C())
                                  print(sensor.readTemp())
                                  print(sensor.readRH())
                                  

                                  layout

                                  T 1 Reply Last reply Reply Quote 0
                                  • T Offline
                                    tttadam @tttadam
                                    last edited by

                                    @tttadam said in SI7021 I2C pin setup:

                                    Looks like my picture didn't make it.
                                    Here is the url: [https://imgur.com/a/j8fPQxZ](link url)

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

                                      @tttadam According to the picture, your wiring seems wrong. You connected SDA to P10 and SCL to P9.

                                      T 1 Reply Last reply Reply Quote 1
                                      • T Offline
                                        tttadam @robert-hh
                                        last edited by

                                        @robert-hh I switched them back and for forth. Maybe a I took the picture on the wrong state but I tried both way (just to make sure).

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

                                          @tttadam In order to test the communication, do some basics like:

                                          from machine import I2C
                                          i2c = I2C(0, I2C.MASTER)
                                          i2c.scan()
                                          

                                          That already should return the adress of the connected device. If that works, then you know at least that the I2C connection is fine and which address your device is using.

                                          T 1 Reply Last reply Reply Quote 1
                                          • T Offline
                                            tttadam @robert-hh
                                            last edited by

                                            @robert-hh said in SI7021 I2C pin setup:

                                            i2c.scan()
                                            This is what it returns:

                                            [64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120]

                                            E robert-hhR 2 Replies 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
                                            • 2 / 3
                                            • First post
                                              Last post

                                            Pycom on Twitter

                                            Pycom