Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Code for two different sensors on the same i2C port
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Code for two different sensors on the same i2C port

by andre123 on Sun Aug 09, 2020 9:21 am

I am a newbie to the Raspberry Pi and Python. I bought the Adafruit SHT31-D board and the Adafruit DPS310 board. I installed the SHT31-D board using the instructions supplied with the sensor. It worked first time (thank you Adafruit!).

Now I want to attach the DPS310 board to the same Raspberry Pi. The instructions supplied with the board use the same SDA1 and SCL1 pins.
Now, if I use the same SDA1 and SCL1 pins for both boards, I am confused as to how I write the code.

The Python code for the SHT31-D is:
1. import time
2. import board
3. import busio
4. import adafruit_sht31d

5. # Create library object using our Bus I2C port
6. i2c = busio.I2C(board.SCL, board.SDA)
7. sensor = adafruit_sht31d.SHT31D(i2c)

The code for the DPS310 board is:
1. import time
2. import board
3. import busio
4. import adafruit_dps310

5. i2c = busio.I2C(board.SCL, board.SDA)

6. dps310 = adafruit_dps310.DPS310(i2c)

Do I write both sets of code in line, and do I only use import time, import board and import busio once? I guess import time need only be used once, but what happens with import board and import busio?

Many thanks for your help – I appreciate this is a very basic question and I have a lot of learning to do.

andre123
 
Posts: 5
Joined: Sun Mar 23, 2014 5:21 am

Re: Code for two different sensors on the same i2C port

by dastels on Sun Aug 09, 2020 1:20 pm

Yes, you only import each module once.

Create 1 I2C object and use it to create the 2 sensor objects. So in this case, to add a sensor, you just need the line that creates the new sensor object:

Code: Select all | TOGGLE FULL SIZE
dps310 = adafruit_dps310.DPS310(i2c)

As well as importing the adafruit_dps310 module.

Dave

dastels
 
Posts: 3505
Joined: Tue Oct 20, 2015 3:22 pm

Re: Code for two different sensors on the same i2C port

by andre123 on Mon Aug 10, 2020 7:42 am

Thank you very much Dave.
Much appreciated
André

andre123
 
Posts: 5
Joined: Sun Mar 23, 2014 5:21 am

Please be positive and constructive with your questions and comments.