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+ }
0 commit comments