Skip to content

Commit 9a5a329

Browse files
committed
add code for multi i2c guide
1 parent 8fd5b8f commit 9a5a329

File tree

6 files changed

+271
-0
lines changed

6 files changed

+271
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// SPDX-FileCopyrightText: 2022 Carter Nelson for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#include <Adafruit_BME280.h>
6+
7+
// For each device, create a separate instance.
8+
Adafruit_BME280 bme1; // BME280 #1 @ 0x77
9+
Adafruit_BME280 bme2; // BME280 #2 @ 0x76
10+
11+
void setup() {
12+
Serial.begin(9600);
13+
while(!Serial);
14+
Serial.println(F("Two BME280 Example"));
15+
16+
// NOTE: There's no need to manually call Wire.begin().
17+
// The BME280 library does that in its begin() method.
18+
19+
// In the call to begin, pass in the I2C address.
20+
// If left out, the default address is used.
21+
bme1.begin(); // use the default address of 0x77
22+
bme2.begin(0x76); // specify the alternate address of 0x76
23+
}
24+
25+
26+
void loop() {
27+
float pressure1, pressure2;
28+
29+
// Read each device separately
30+
pressure1 = bme1.readPressure();
31+
pressure2 = bme2.readPressure();
32+
33+
Serial.println("------------------------------------");
34+
Serial.print("BME280 #1 Pressure = "); Serial.println(pressure1);
35+
Serial.print("BME280 #2 Pressure = "); Serial.println(pressure2);
36+
37+
delay(1000);
38+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// SPDX-FileCopyrightText: 2022 Carter Nelson for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#include <Adafruit_BME280.h>
6+
7+
#define TCAADDR 0x70
8+
9+
// For each device, create a separate instance.
10+
Adafruit_BME280 bme1; // BME280 #1
11+
Adafruit_BME280 bme2; // BME280 #2
12+
Adafruit_BME280 bme3; // BME280 #3
13+
14+
// Helper function for changing TCA output channel
15+
void tcaselect(uint8_t channel) {
16+
if (channel > 7) return;
17+
Wire.beginTransmission(TCAADDR);
18+
Wire.write(1 << channel);
19+
Wire.endTransmission();
20+
}
21+
22+
void setup() {
23+
Serial.begin(9600);
24+
while(!Serial);
25+
Serial.println(F("Three BME280 Example"));
26+
27+
// NOTE!!! VERY IMPORTANT!!!
28+
// Must call this once manually before first call to tcaselect()
29+
Wire.begin();
30+
31+
// Before using any BME280, call tcaselect to set the channel.
32+
tcaselect(0); // TCA channel for bme1
33+
bme1.begin(); // use the default address of 0x77
34+
35+
tcaselect(1); // TCA channel for bme2
36+
bme2.begin(); // use the default address of 0x77
37+
38+
tcaselect(2); // TCA channel for bme3
39+
bme3.begin(); // use the default address of 0x77
40+
}
41+
42+
43+
void loop() {
44+
float pressure1, pressure2, pressure3;
45+
46+
// Read each device separately
47+
tcaselect(0);
48+
pressure1 = bme1.readPressure();
49+
tcaselect(1);
50+
pressure2 = bme2.readPressure();
51+
tcaselect(2);
52+
pressure3 = bme3.readPressure();
53+
54+
Serial.println("------------------------------------");
55+
Serial.print("BME280 #1 Pressure = "); Serial.println(pressure1);
56+
Serial.print("BME280 #2 Pressure = "); Serial.println(pressure2);
57+
Serial.print("BME280 #3 Pressure = "); Serial.println(pressure3);
58+
59+
delay(1000);
60+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// SPDX-FileCopyrightText: 2022 Carter Nelson for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#include <Adafruit_BME280.h>
6+
7+
#define TCAADDR 0x70
8+
9+
// For each device, create a separate instance.
10+
Adafruit_BME280 bme1; // BME280 #1
11+
Adafruit_BME280 bme2; // BME280 #2
12+
Adafruit_BME280 bme3; // BME280 #3
13+
Adafruit_BME280 bme4; // BME280 #4
14+
15+
// Helper function for changing TCA output channel
16+
void tcaselect(uint8_t channel) {
17+
if (channel > 7) return;
18+
Wire.beginTransmission(TCAADDR);
19+
Wire.write(1 << channel);
20+
Wire.endTransmission();
21+
}
22+
23+
void setup() {
24+
Serial.begin(9600);
25+
while(!Serial);
26+
Serial.println(F("Four BME280 Example"));
27+
28+
// NOTE!!! VERY IMPORTANT!!!
29+
// Must call this once manually before first call to tcaselect()
30+
Wire.begin();
31+
32+
// Before using any BME280, call tcaselect to select its output channel
33+
tcaselect(0); // TCA channel for bme1
34+
bme1.begin(); // use the default address of 0x77
35+
36+
tcaselect(1); // TCA channel for bme2
37+
bme2.begin(); // use the default address of 0x77
38+
39+
tcaselect(2); // TCA channel for bme3
40+
bme3.begin(); // use the default address of 0x77
41+
42+
tcaselect(3); // TCA channel for bme4
43+
bme4.begin(); // use the default address of 0x77
44+
}
45+
46+
47+
void loop() {
48+
float pressure1, pressure2, pressure3, pressure4;
49+
50+
// Read each device separately
51+
tcaselect(0);
52+
pressure1 = bme1.readPressure();
53+
tcaselect(1);
54+
pressure2 = bme2.readPressure();
55+
tcaselect(2);
56+
pressure3 = bme3.readPressure();
57+
tcaselect(3);
58+
pressure4 = bme4.readPressure();
59+
60+
Serial.println("------------------------------------");
61+
Serial.print("BME280 #1 Pressure = "); Serial.println(pressure1);
62+
Serial.print("BME280 #2 Pressure = "); Serial.println(pressure2);
63+
Serial.print("BME280 #3 Pressure = "); Serial.println(pressure3);
64+
Serial.print("BME280 #4 Pressure = "); Serial.println(pressure4);
65+
66+
delay(1000);
67+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# SPDX-FileCopyrightText: 2022 Carter Nelson for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import board
7+
from adafruit_bme280 import basic as adafruit_bme280
8+
9+
# Get the board's default I2C port
10+
i2c = board.I2C()
11+
12+
# Create each sensor instance
13+
bme1 = adafruit_bme280.Adafruit_BME280_I2C(i2c) # default 0x77 address
14+
bme2 = adafruit_bme280.Adafruit_BME280_I2C(i2c, 0x76) # alternate 0x76 address
15+
16+
print("Two BME280 Example")
17+
18+
while True:
19+
# Access each sensor via its instance
20+
pressure1 = bme1.pressure
21+
pressure2 = bme2.pressure
22+
23+
print("-"*20)
24+
print("BME280 #1 Pressure =", pressure1)
25+
print("BME280 #2 Pressure =", pressure2)
26+
27+
time.sleep(1)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# SPDX-FileCopyrightText: 2022 Carter Nelson for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import board
7+
import adafruit_tca9548a
8+
from adafruit_bme280 import basic as adafruit_bme280
9+
10+
# Create I2C bus as normal
11+
i2c = board.I2C()
12+
13+
# Create the TCA9548A object and give it the I2C bus
14+
tca = adafruit_tca9548a.TCA9548A(i2c)
15+
16+
#--------------------------------------------------------------------
17+
# NOTE!!! This is the "special" part of the code
18+
#
19+
# Create each BME280 using the TCA9548A channel instead of the I2C object
20+
bme1 = adafruit_bme280.Adafruit_BME280_I2C(tca[0])
21+
bme2 = adafruit_bme280.Adafruit_BME280_I2C(tca[1])
22+
bme3 = adafruit_bme280.Adafruit_BME280_I2C(tca[2])
23+
#--------------------------------------------------------------------
24+
25+
print("Three BME280 Example")
26+
27+
while True:
28+
# Access each sensor via its instance
29+
pressure1 = bme1.pressure
30+
pressure2 = bme2.pressure
31+
pressure3 = bme3.pressure
32+
33+
print("-"*20)
34+
print("BME280 #1 Pressure =", pressure1)
35+
print("BME280 #2 Pressure =", pressure2)
36+
print("BME280 #3 Pressure =", pressure3)
37+
38+
time.sleep(1)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# SPDX-FileCopyrightText: 2022 Carter Nelson for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import board
7+
import adafruit_tca9548a
8+
from adafruit_bme280 import basic as adafruit_bme280
9+
10+
# Create I2C bus as normal
11+
i2c = board.I2C()
12+
13+
# Create the TCA9548A object and give it the I2C bus
14+
tca = adafruit_tca9548a.TCA9548A(i2c)
15+
16+
#--------------------------------------------------------------------
17+
# NOTE!!! This is the "special" part of the code
18+
#
19+
# Create each BME280 using the TCA9548A channel instead of the I2C object
20+
bme1 = adafruit_bme280.Adafruit_BME280_I2C(tca[0])
21+
bme2 = adafruit_bme280.Adafruit_BME280_I2C(tca[1])
22+
bme3 = adafruit_bme280.Adafruit_BME280_I2C(tca[2])
23+
bme4 = adafruit_bme280.Adafruit_BME280_I2C(tca[3])
24+
#--------------------------------------------------------------------
25+
26+
print("Four BME280 Example")
27+
28+
while True:
29+
# Access each sensor via its instance
30+
pressure1 = bme1.pressure
31+
pressure2 = bme2.pressure
32+
pressure3 = bme3.pressure
33+
pressure4 = bme4.pressure
34+
35+
print("-"*20)
36+
print("BME280 #1 Pressure =", pressure1)
37+
print("BME280 #2 Pressure =", pressure2)
38+
print("BME280 #3 Pressure =", pressure3)
39+
print("BME280 #4 Pressure =", pressure4)
40+
41+
time.sleep(1)

0 commit comments

Comments
 (0)