Switching between 2 I2C sensors

Hi,

I have 2 sensors, the adafruit bmp180 temperature sensor and the k30 CO2 sensor. Both have I2C interface.

  1. http://www.adafruit.com/products/1603
  2. http://cdn.shopify.com/s/files/1/0019/5952/files/Senseair-Arduino.pdf

I’m wondering how do I switch between the two of them in code?

Part of the loop function is provided below? But would like to know how I switch between both sensors?

void loop() {
  
int sensorValue1 = analogRead(sensorPin1);
int sensorValue2 = analogRead(sensorPin2);

//How do I select the adafruit sensor here before reading values? 

int sensorValue3 = bmp.readTemperature();
int sensorValue4 = bmp.readPressure();

//When I call the readCO2() the sensor is selected in the function...so it is okay
 
int co2Value = readCO2(); 
int sensorValue5 = co2Value;

The CO2 function:

int readCO2()   
{   
 int co2_value = 0;   
 // We will store the CO2 value inside this variable.   
  
 digitalWrite(13, HIGH);   
 // On most Arduino platforms this pin is used as an indicator light.   
  
 //////////////////////////   
 /* Begin Write Sequence */   
 //////////////////////////   
  
 Wire.beginTransmission(co2Addr);   
 Wire.write(0x22);   
 Wire.write(0x00);   
 Wire.write(0x08);   
 Wire.write(0x2A);   
 Wire.endTransmission();   
  
 /////////////////////////   
 /* End Write Sequence. */   
 /////////////////////////   
  
 /*  
 We wait 10ms for the sensor to process our command.  
 The sensors's primary duties are to accurately  
 measure CO2 values. Waiting 10ms will ensure the  
 data is properly written to RAM  
 
 delay(10);  
 
 /////////////////////////  
 /* Begin Read Sequence */   
 /////////////////////////   
  
 /*  
 Since we requested 2 bytes from the sensor we must  
 read in 4 bytes. This includes the payload, checksum,  
 and command status byte.  
 
 */   
  
 Wire.requestFrom(co2Addr, 4);   
  
 byte i = 0;   
 byte buffer[4] = {0, 0, 0, 0};   
  
 /*  
 Wire.available() is not nessessary. Implementation is obscure but we leave  
 it in here for portability and to future proof our code  
 */   
 while(Wire.available())   
 {   
 buffer[i] = Wire.read();   
 i++;   
 }   
  
 ///////////////////////   
 /* End Read Sequence */   
 ///////////////////////   
  
 /*  
 Using some bitwise manipulation we will shift our buffer  
 into an integer for general consumption  
 */   
  
 co2_value = 0;   
 co2_value |= buffer[1] & 0xFF;   
 co2_value = co2_value << 8;   
 co2_value |= buffer[2] & 0xFF;   
  
  
 byte sum = 0; //Checksum Byte   
 sum = buffer[0] + buffer[1] + buffer[2]; //Byte addition utilizes overflow  
 if(sum == buffer[3])   
 {   
 // Success!   
 digitalWrite(13, LOW);   
 return co2_value;   
 }   
 else   
 {   
 // Failure!   
 /*  
 Checksum failure can be due to a number of factors,  
 fuzzy electrons, sensor busy, etc.  
 */   
  
 digitalWrite(13, LOW);   
 return 0;   
 }   
}

The different I2C devices should have different I2C addresses. You select them by address.

You should find that you have one number, co2Addr, probably defined in the top part of your program which you have not shown, which selects only the CO2 sensor and a different i2c address for selecting only the temperature sensor. If you were unlucky and bought different sensors which happen to have the same i2c address then it won't work, but that would require pretty bad luck.

So, write a dev sketch to get back the CO2 number and Serial.print it, then another one for only the T, and when each of those work write a combined sketch which calls both those functions. That will be pretty much a copy&paste job from your two dev sketches.

The i2c address does what your post asked for.

You could run the scanner to verify the device addresses on the bus.

http://playground.arduino.cc/Main/I2cScanner

You don't have libraries for those sensors? It seems that you are using a library for the bmp180, but not for the CO2 sensor. What is the reason for that?

If you were unlucky and bought different sensors which happen to have the same i2c address then it won't work, but that would require pretty bad luck.

No problem, you don't have to hard wire the adderss. Just connect an address line of each to two output pins. Then select with software.