Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple sensors #19

Merged
merged 2 commits into from
Jun 21, 2021
Merged

Support multiple sensors #19

merged 2 commits into from
Jun 21, 2021

Conversation

caternuson
Copy link
Contributor

For #16 and maybe #18. Add support for multiple sensors. Sort of a hack, but tested and works. Basic idea is to add new class members for i2c_dev and spi_dev and then dynamically set them to the global ones for each read method.

Do not have BMP390 to test.

Multiple SPI Test

bmp388_multi_spi

#include "Adafruit_BMP3XX.h"

#define BMP1_CS 7
#define BMP2_CS 9

Adafruit_BMP3XX bmp1;
Adafruit_BMP3XX bmp2;

void setup() {
  Serial.begin(115200);
  while (!Serial);
  Serial.println("Adafruit BMP388 / BMP390 test");

  if (! bmp1.begin_SPI(BMP1_CS)) {  // hardware SPI mode  
    Serial.println("Could not find a valid BMP sensor #1, check wiring!");
    while (1);
  }

  if (! bmp2.begin_SPI(BMP2_CS)) {  // hardware SPI mode  
    Serial.println("Could not find a valid BMP sensor #2, check wiring!");
    while (1);
  }

  Serial.println("Initialization Success!");
}

void loop() {
  if (! bmp1.performReading()) {
    Serial.println("Failed to perform reading BMP #1 :(");
    return;
  }
  Serial.print("BMP #1 Temperature = ");
  Serial.print(bmp1.temperature);
  Serial.println(" *C");

  if (! bmp2.performReading()) {
    Serial.println("Failed to perform reading BMP #2 :(");
    return;
  }
  Serial.print("BMP #2 Temperature = ");
  Serial.print(bmp2.temperature);
  Serial.println(" *C");

  delay(2000);
  
}

Multiple I2C Test

bmp388_multi_i2c

#include "Adafruit_BMP3XX.h"

Adafruit_BMP3XX bmp1;
Adafruit_BMP3XX bmp2;

void setup() {
  Serial.begin(115200);
  while (!Serial);
  Serial.println("Adafruit BMP388 / BMP390 test");

  if (! bmp1.begin_I2C()) {  
    Serial.println("Could not find a valid BMP sensor #1, check wiring!");
    while (1);
  }

  if (! bmp2.begin_I2C(0x76)) {  
    Serial.println("Could not find a valid BMP sensor #2, check wiring!");
    while (1);
  }

  Serial.println("Initialization Success!");
}

void loop() {
  if (! bmp1.performReading()) {
    Serial.println("Failed to perform reading BMP #1 :(");
    return;
  }
  Serial.print("BMP #1 Temperature = ");
  Serial.print(bmp1.temperature);
  Serial.println(" *C");

  if (! bmp2.performReading()) {
    Serial.println("Failed to perform reading BMP #2 :(");
    return;
  }
  Serial.print("BMP #2 Temperature = ");
  Serial.print(bmp2.temperature);
  Serial.println(" *C");

  delay(2000);
  
}

Both work and print out expected results. Can breathe on second sensor and see its temp value rise.
Screenshot from 2021-06-18 12-36-24

@ladyada ladyada merged commit 5326652 into adafruit:master Jun 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants