Pages: [1]   Go Down
Author Topic: Switching between 2 I2C sensors  (Read 66 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Code:
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:

Code:
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;   
 }   
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8653
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Full Member
***
Karma: 7
Posts: 132
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Online Online
Faraday Member
**
Karma: 80
Posts: 3786
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Although I answer in good faith, remember always: Your Mileage May Vary.
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

Portugal
Offline Offline
God Member
*****
Karma: 33
Posts: 964
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Shelby Township, Disco, Michigan
Offline Offline
Sr. Member
****
Karma: 15
Posts: 452
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Pages: [1]   Go Up
Jump to: