Skip to content

Commit fdb8e76

Browse files
committed
add multi tca code
1 parent 87c23d5 commit fdb8e76

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 TCA9548A, add an entry with its address
8+
const uint8_t TCA_ADDRESSES[] = {
9+
0x70,
10+
0x71
11+
};
12+
const uint8_t TCA_COUNT = sizeof(TCA_ADDRESSES) / sizeof(TCA_ADDRESSES[0]);
13+
14+
Adafruit_BME280 bme1; // BME280 #1
15+
Adafruit_BME280 bme2; // BME280 #2
16+
Adafruit_BME280 bme3; // BME280 #3
17+
18+
void tcaselect(uint8_t tca, uint8_t channel) {
19+
if (tca >= TCA_COUNT) return;
20+
if (channel > 7) return;
21+
22+
// loop over all TCA's
23+
for (uint8_t i=0; i<TCA_COUNT; i++) {
24+
Wire.beginTransmission(TCA_ADDRESSES[i]);
25+
if (i == tca)
26+
// set output channel for selected TCA
27+
Wire.write(1 << channel);
28+
else
29+
// for others, turn off all channels
30+
Wire.write(0);
31+
Wire.endTransmission();
32+
}
33+
}
34+
35+
36+
void setup() {
37+
Serial.begin(9600);
38+
while(!Serial);
39+
Serial.println("Multiple BME280 / Multiple TCA9548A Example.");
40+
41+
Serial.print("Total number of TCA's = "); Serial.println(TCA_COUNT);
42+
for (uint8_t i=0; i<TCA_COUNT; i++) {
43+
Serial.print(i); Serial.print(" : 0x"); Serial.println(TCA_ADDRESSES[i], 16);
44+
}
45+
46+
// NOTE!!! VERY IMPORTANT!!!
47+
// Must call this once manually before first call to tcaselect()
48+
Wire.begin();
49+
50+
// Before using any BME280, call tcaselect to select its output channel
51+
tcaselect(0, 0); // TCA 0, channel 0 for bme1
52+
bme1.begin(); // use the default address of 0x77
53+
54+
tcaselect(0, 1); // TCA 0, channel 1 for bme2
55+
bme2.begin(); // use the default address of 0x77
56+
57+
tcaselect(1, 0); // TCA 1, channel 0 for bme3
58+
bme3.begin(); // use the default address of 0x77
59+
}
60+
61+
void loop() {
62+
float pressure1, pressure2, pressure3;
63+
64+
tcaselect(0, 0);
65+
pressure1 = bme1.readPressure();
66+
tcaselect(0, 1);
67+
pressure2 = bme2.readPressure();
68+
tcaselect(1, 0);
69+
pressure3 = bme3.readPressure();
70+
71+
Serial.println("------------------------------------");
72+
Serial.print("BME280 #1 Pressure = "); Serial.println(pressure1);
73+
Serial.print("BME280 #2 Pressure = "); Serial.println(pressure2);
74+
Serial.print("BME280 #3 Pressure = "); Serial.println(pressure3);
75+
76+
delay(1000);
77+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
#--------------------------------------------------------------------
14+
# NOTE!!! This is the "special" part of the code
15+
#
16+
# For each TCA9548A, create a separate instance and give each
17+
# the *same* I2C bus but with specific address for each
18+
tca1 = adafruit_tca9548a.TCA9548A(i2c, 0x70) # TCA with address 0x70
19+
tca2 = adafruit_tca9548a.TCA9548A(i2c, 0x71) # TCA with address 0x71
20+
# Create each BME280 using the TCA9548A channel instead of the I2C object
21+
# Be sure to use the TCA instance each BME280 is attached to
22+
bme1 = adafruit_bme280.Adafruit_BME280_I2C(tca1[0]) # TCA 1 Channel 0
23+
bme2 = adafruit_bme280.Adafruit_BME280_I2C(tca1[1]) # TCA 1 Channel 1
24+
bme3 = adafruit_bme280.Adafruit_BME280_I2C(tca2[0]) # TCA 2 Channel 0
25+
#--------------------------------------------------------------------
26+
27+
print("Multiple BME280 / Multiple TCA9548A Example")
28+
29+
while True:
30+
# Access each sensor via its instance
31+
pressure1 = bme1.pressure
32+
pressure2 = bme2.pressure
33+
pressure3 = bme3.pressure
34+
35+
print("-"*20)
36+
print("BME280 #1 Pressure =", pressure1)
37+
print("BME280 #2 Pressure =", pressure2)
38+
print("BME280 #3 Pressure =", pressure3)
39+
40+
time.sleep(1)

0 commit comments

Comments
 (0)