Multiple I2C Sensors

I have an LSI3DH accelerometer and a TCS34725. They both connect through I2C. I’ve tried their individual example codes and they both work individually. Now I’ve I’ve been reading about how to use two I2C sensors together, and so far I’ve hooked them up properly.

When I run the address scanning sketch, I can see both my sensor addresses printing. Sketch for the scanner here: Arduino Playground - I2cScanner

LIS3DH address: 0x19
TCS34725 address: 0x29

Now I am searching tutorials of how to adapt the arduino sketch to print the sensor data from both sensors at the same time, and that’s where I feel confused. I tried to follow what was written here: Tutorial: Arduino and the I2C bus – Part One | tronixstuff.com

But they were programming for different sensors I get lost trying to figure out how to adapt it for my sensors.

Here is what I started from for the accelerometer:

#include "SparkFunLIS3DH.h"
#include "Wire.h"
#include "SPI.h"

LIS3DH myAccel(I2C_MODE, 0x19); //Default constructor is I2C, addr 0x19.

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  delay(1000); //relax...
  Serial.println("Processor came out of reset.\n");
  
  //myIMU.settings.adcEnabled = 1;
  //myIMU.settings.tempEnabled = 1;
  myAccel.settings.accelSampleRate = 50;  //Hz.  Can be: 0,1,10,25,50,100,200,400,1600,5000 Hz
  myAccel.settings.accelRange = 16;      //Max G force readable.  Can be: 2, 4, 8, 16
  myAccel.settings.xAccelEnabled = 1;
  myAccel.settings.yAccelEnabled = 1;
  myAccel.settings.zAccelEnabled = 1;
  
  //Call .begin() to configure the IMU
  myAccel.begin();

}

void loop()
{
  //Get all parameters
  //Serial.print("\nAccelerometer:\n");
  //Serial.print("X: ");
  Serial.print(myAccel.readFloatAccelX(), 4);
  Serial.print(" ");
 // Serial.print("Y: ");
  Serial.print(myAccel.readFloatAccelY(), 4);
  Serial.print(" ");
  //Serial.print("Z: ");
  Serial.print(myAccel.readFloatAccelZ(), 4); 
  
  Serial.println(" ");
  
  
  delay(1000);
}

Here is where I started from for the colour sensor:

#include <Wire.h>
#include "Adafruit_TCS34725.h"

/* Example code for the Adafruit TCS34725 breakout library */

/* Connect SCL    to analog 5
   Connect SDA    to analog 4
   Connect VDD    to 3.3V DC
   Connect GROUND to common ground */

/* Initialise with default values (int time = 2.4ms, gain = 1x) */
// Adafruit_TCS34725 tcs = Adafruit_TCS34725();

/* Initialise with specific int time and gain values */
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);

void setup(void) {
  Serial.begin(9600);

 if (tcs.begin()) {
    Serial.println("Found sensor");
  } else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1);*/ 
  }

}

void loop(void) {
  uint16_t r, g, b, c, colorTemp, lux;

  tcs.getRawData(&r, &g, &b, &c);
  // colorTemp = tcs.calculateColorTemperature(r, g, b);
  colorTemp = tcs.calculateColorTemperature_dn40(r, g, b, c);
  lux = tcs.calculateLux(r, g, b);

  Serial.print(r, DEC); 
  Serial.print(" ");
  Serial.print(g, DEC); 
  Serial.print(" ");
  Serial.print(b, DEC); 
  Serial.print(" ");
  Serial.println(" ");
}

Here is my very bad attempt to combine them:

#include <Wire.h>
#include "Adafruit_TCS34725.h"
#include "SparkFunLIS3DH.h"
#include "SPI.h"

LIS3DH myAccel(I2C_MODE, 0x19); //I first tried writing it with a #define like with the other sensor, but it didn't like it, so I went back to how it was originally written
#define TCS34725_address 0x29 

/* Initialise with specific int time and gain values */
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);

void setup() {
  Wire.begin();
  Serial.begin(9600); 
} 

void lis3dh() {
  Wire.beginTransmission(myAccel); //why am I getting an error here?
  Wire.write(0x19);
  myAccel.settings.accelSampleRate = 50;  //Hz.  Can be: 0,1,10,25,50,100,200,400,1600,5000 Hz
  myAccel.settings.accelRange = 16;      //Max G force readable.  Can be: 2, 4, 8, 16
  myAccel.settings.xAccelEnabled = 1;
  myAccel.settings.yAccelEnabled = 1;
  myAccel.settings.zAccelEnabled = 1;
  myAccel.begin();
} 

void tc34725() {
  Wire.beginTransmission(TCS34725_address); 
  Wire.write(0x29);
   
   if (tcs.begin()) {
    Serial.println("Found sensor");
  } else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1);*/ 
  } 
}

 void loop() {
  Serial.print(myAccel.readFloatAccelX(), 4);
  Serial.print(" ");
  Serial.print(myAccel.readFloatAccelY(), 4);
  Serial.print(" ");
  Serial.print(myAccel.readFloatAccelZ(), 4);  
  Serial.println(" ");
 
 uint16_t r, g, b, c, colorTemp, lux;

  tcs.getRawData(&r, &g, &b, &c);
  // colorTemp = tcs.calculateColorTemperature(r, g, b);
  colorTemp = tcs.calculateColorTemperature_dn40(r, g, b, c);
  lux = tcs.calculateLux(r, g, b);

  Serial.print(r, DEC); 
  Serial.print(" ");
  Serial.print(g, DEC); 
  Serial.print(" ");
  Serial.print(b, DEC); 
  Serial.print(" ");
  Serial.print(" ");
  Serial.println(" ");
  
  delay(1000);
}

I don't see you calling either the lis3dh() or the tc34725() function in loop().

wvmarle:
I don't see you calling either the lis3dh() or the tc34725() function in loop().

It wasn't like that in the original simple example codes. How would I do that?

Like this? Still get lots of errors.

#include <Wire.h>
#include "Adafruit_TCS34725.h"
#include "SparkFunLIS3DH.h"
#include "SPI.h"

LIS3DH myAccel(I2C_MODE, 0x19); //I first tried writing it with a #define like with the other sensor, but it didn't like it, so I went back to how it was originally written
#define TCS34725_address 0x29 

/* Initialise with specific int time and gain values */
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);

void setup() {
  Wire.begin();
  Serial.begin(9600); 
} 

void lis3dh() {
  Wire.beginTransmission(myAccel); //why am I getting an error here?
  Wire.write(0x19);
  myAccel.settings.accelSampleRate = 50;  //Hz.  Can be: 0,1,10,25,50,100,200,400,1600,5000 Hz
  myAccel.settings.accelRange = 16;      //Max G force readable.  Can be: 2, 4, 8, 16
  myAccel.settings.xAccelEnabled = 1;
  myAccel.settings.yAccelEnabled = 1;
  myAccel.settings.zAccelEnabled = 1;
  myAccel.begin();
  Wire.endTransmission();  
} 

void tc34725() {
  Wire.beginTransmission(TCS34725_address); 
  Wire.write(0x29);
   
   if (tcs.begin()) {
    Serial.println("Found sensor");
  } else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1);*/ 
  } 
  Wire.endTransmission();  
}

 void loop() {
  lis3dh();
  Serial.print(myAccel.readFloatAccelX(), 4);
  Serial.print(" ");
  Serial.print(myAccel.readFloatAccelY(), 4);
  Serial.print(" ");
  Serial.print(myAccel.readFloatAccelZ(), 4);  
  Serial.println(" ");

 tc34725();
 uint16_t r, g, b, c, colorTemp, lux;

  tcs.getRawData(&r, &g, &b, &c);
  // colorTemp = tcs.calculateColorTemperature(r, g, b);
  colorTemp = tcs.calculateColorTemperature_dn40(r, g, b, c);
  lux = tcs.calculateLux(r, g, b);

  Serial.print(r, DEC); 
  Serial.print(" ");
  Serial.print(g, DEC); 
  Serial.print(" ");
  Serial.print(b, DEC); 
  Serial.print(" ");
  Serial.print(" ");
  Serial.println(" ");
  
  delay(1000);
}

Actually, I realized that the LIS3DH has the capability to alternatively hookup via SPI. I have the colour sensor on the I2C and the accel on the SPI now and it works. Although it still kind of bothers me that I couldn't figure out the code for two I2C.