SI7021 I2C pin setup (SOLVED)
-
@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 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()) -
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-hh My bad, so sorry. The error is in this line:
sensor = SI7021(i2c=bus) -
@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 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 errorLine 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 Have you add a 1K or 4K7 or 10K pull-up resistor on G8 and G7 ?
What is the board you use ? -
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 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.
-
@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 errorpin5 = 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 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 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()) -
@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) -
@tttadam According to the picture, your wiring seems wrong. You connected SDA to P10 and SCL to P9.
-
@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 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 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 Strange things, i have a look to your wire connexion, i hope my eyes are wrong but it seem you power your sensor with VIN (approx 5V), but your device SI7021 have +3.6V maximum power supply voltage so +5V can destroy your device.
If i take time to watch your wire it because typically on a scan i2c your device seem to ACK all address starting by 0x40 (64) and this can be happen in case of bad gnd connexion, please check your power supply +3.3V and GND for your SI7021.
If you have an oscilloscope for probin SDA and SCL this will be very helpfull. -
@tttadam Definitely Vcc should be 3.3 V. And I cannot tell whether pull-up resistors are present on the board. Adding some with a value of about 4.7 - 10 kOhm should not hurt.
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