|
| 1 | +/* |
| 2 | + Communicate with BME280s with different I2C addresses |
| 3 | + Nathan Seidle @ SparkFun Electronics |
| 4 | + March 23, 2015 |
| 5 | +
|
| 6 | + Feel like supporting our work? Buy a board from SparkFun! |
| 7 | + https://www.sparkfun.com/products/14348 - Qwiic Combo Board |
| 8 | + https://www.sparkfun.com/products/13676 - BME280 Breakout Board |
| 9 | +
|
| 10 | + This example shows how to connect two sensors on the same I2C bus. |
| 11 | +
|
| 12 | + The BME280 has two I2C addresses: 0x77 (jumper open) or 0x76 (jumper closed) |
| 13 | +
|
| 14 | + Hardware connections: |
| 15 | + BME280 -> Arduino |
| 16 | + GND -> GND |
| 17 | + 3.3 -> 3.3 |
| 18 | + SDA -> A4 |
| 19 | + SCL -> A5 |
| 20 | +*/ |
| 21 | + |
| 22 | +#include <Wire.h> |
| 23 | + |
| 24 | +#include "SparkFunBME280.h" |
| 25 | +BME280 mySensorA; //Uses default I2C address 0x77 |
| 26 | +BME280 mySensorB; //Uses I2C address 0x76 (jumper closed) |
| 27 | + |
| 28 | +void setup() |
| 29 | +{ |
| 30 | + Serial.begin(9600); |
| 31 | + Serial.println("Example showing alternate I2C addresses"); |
| 32 | + |
| 33 | + Wire.begin(); |
| 34 | + |
| 35 | + mySensorA.setI2CAddress(0x77); //The default for the SparkFun Environmental Combo board is 0x77 (jumper open). |
| 36 | + //If you close the jumper it is 0x76 |
| 37 | + //The I2C address must be set before .begin() otherwise the cal values will fail to load. |
| 38 | + |
| 39 | + if(mySensorA.beginI2C() == false) Serial.println("Sensor A connect failed"); |
| 40 | + |
| 41 | + mySensorB.setI2CAddress(0x76); //Connect to a second sensor |
| 42 | + if(mySensorB.beginI2C() == false) Serial.println("Sensor B connect failed"); |
| 43 | +} |
| 44 | + |
| 45 | +void loop() |
| 46 | +{ |
| 47 | + Serial.print("HumidityA: "); |
| 48 | + Serial.print(mySensorA.readFloatHumidity(), 0); |
| 49 | + |
| 50 | + Serial.print(" PressureA: "); |
| 51 | + Serial.print(mySensorA.readFloatPressure(), 0); |
| 52 | + |
| 53 | + Serial.print(" TempA: "); |
| 54 | + //Serial.print(mySensorA.readTempC(), 2); |
| 55 | + Serial.print(mySensorA.readTempF(), 2); |
| 56 | + |
| 57 | + Serial.print(" HumidityB: "); |
| 58 | + Serial.print(mySensorB.readFloatHumidity(), 0); |
| 59 | + |
| 60 | + Serial.print(" PressureB: "); |
| 61 | + Serial.print(mySensorB.readFloatPressure(), 0); |
| 62 | + |
| 63 | + Serial.print(" TempB: "); |
| 64 | + //Serial.print(mySensorB.readTempC(), 2); |
| 65 | + Serial.print(mySensorB.readTempF(), 2); |
| 66 | + |
| 67 | + Serial.println(); |
| 68 | + |
| 69 | + delay(50); |
| 70 | +} |
| 71 | + |
0 commit comments