SI7021 I2C pin setup (SOLVED)



  • @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]



  • @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.



  • @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).



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



  • @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-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



  • @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").



  • @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())



  • @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.



  • @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())
    


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



  • @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())
    


  • @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.



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

    sensor = SI7021(i2c=bus)
    


  • @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?



  • @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


  • @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())
    


  • @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'



  • @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)


 

Pycom on Twitter