SI7021 I2C pin setup (SOLVED)
-
@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.
-
@eric73 Well, you are right. I gave the sensor 5v instead of 3.3. I think I killed it.. well least a learned something. I also tried with 10k resistor but still getting I2C bus error...
Thanks for the help anyway. -
@tttadam did you try swapping sda and scl again when running with 3.3v?
-
@robert-hh Yes.
for i2c.scan() i get back this: [64], but I2C bus error for print(sensor.readTemp())
-
@tttadam So it means that at least the i2c protocol is now working. 64 or 0x40 ist the proper i2c address of the device. Try to reduce the baud rate.
-
I created a small code so it looped through all tha baudrates. Maybe a little overkill.
Off topic, but the code sometimes freezed for a half a minute or so. It was because I was created to much i2c object, and a garbage collector kicked in? Can I prevent that behavior?
Also If you can give any feedback for my code I would appreciate it very much.Anyway this code was unsuccessful. I was unable to find a the correct baud rate, I got the same I2C bus error always.
import pycom from machine import I2C, Pin from SI7021 import SI7021 import time pin9 = Pin("P9",mode= Pin.OPEN_DRAIN, pull= Pin.PULL_UP) pin10 = Pin("P10",mode= Pin.OPEN_DRAIN, pull= Pin.PULL_UP) expledpin = Pin('G16', mode=Pin.OUT) start = True buttonpin = Pin('G17', mode= Pin.IN, pull=Pin.PULL_UP) while start: if buttonpin() == 0: start = False with open("/sd/data.csv", 'a')as ds: expledpin.value(1) for baud in range(1, 115200): pycom.rgbled(0x0000FF) bus = I2C(0, I2C.MASTER, baudrate=baud, pins=(pin9,pin10)) # bus = I2C(0, I2C.MASTER, baudrate=100000, pins=("P8","P7")) sensor = SI7021(i2c=bus) try: print(sensor.readTemp()) ds.write("\n"+str(sensor.readTemp())) pycom.rgbled(0x00FF00) except OSError as er: pycom.rgbled(0xFF0000) expledpin.value(0) ds.write(str(repr(er))+str(baud)) expledpin.value(1) time.sleep(0.001) pycom.rgbled(0x000000) ds.close() -
@tttadam You do not have to re-create the i2c object. You can call i2c.init() to change the parameters. And I still do not know why you create the Pin objects separately. You just have to name them when creating the I2C object:
bus = I2C(0, I2C.MASTER, baudrate=baud, pins=("P9","P10"))
The init method of i2c will initialize the Pin objects. -
Okay I modified the code as you recomended it.
But I got the same error on every baudrate.
Here is the csv, but nothing intresting in it: http://www.filedropper.com/data_11import pycom from machine import I2C, Pin from SI7021 import SI7021 import time expledpin = Pin('G16', mode=Pin.OUT) buttonpin = Pin('G17', mode= Pin.IN, pull=Pin.PULL_UP) i2c = I2C(0, pins=("P9","P10")) start = True while start: if buttonpin() == 0: start = False with open("/sd/data.csv", 'a')as ds: expledpin.value(1) for baud in range(1, 115200): pycom.rgbled(0x0000FF) i2c.init(I2C.MASTER, baudrate=baud) sensor = SI7021(i2c=i2c) try: print(sensor.readTemp()) ds.write("\n\r;;"+str(sensor.readTemp())) pycom.rgbled(0x00FF00) except OSError as er: pycom.rgbled(0xFF0000) expledpin.value(0) ds.write("\n\r"+str(repr(er))+";"+str(baud)+";") expledpin.value(1) time.sleep(0.001) pycom.rgbled(0x000000) ds.close()
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