This post illustrates how two I2C components can be connected in parallel and serve data to an Arduino Nano board.
Modules
The two modules that are used in this project are the VL6810X Time of Flight (ToF) distance sensor and the BH1750 ambient light sensor. Actually the VL6810X ToF sensor also contains an ambient light sensor, and the project can thus also be used for checking the consistency between these two sensors.
Wiring
Sketch
The sketch is simply created by joining the individual sketches for the two modules.
#include <Wire.h>
#include <VL6180X.h>
#include <math.h>
int BH1750address = 0x23; //i2c address
byte buff[2];
VL6180X sensor;
void setup()
{
Serial.begin(9600);
Wire.begin();
sensor.init();
sensor.configureDefault();
// Reduce range max convergence time and ALS integration
// time to 30 ms and 50 ms, respectively, to allow 10 Hz
// operation (as suggested by Table 6 ("Interleaved mode
// limits (10 Hz operation)") in the datasheet).
sensor.writeReg(VL6180X::SYSRANGE__MAX_CONVERGENCE_TIME, 30);
sensor.writeReg16Bit(VL6180X::SYSALS__INTEGRATION_PERIOD, 50);
sensor.setTimeout(500);
// stop continuous mode if already active
sensor.stopContinuous();
// in case stopContinuous() triggered a single-shot
// measurement, wait for it to complete
delay(300);
// start interleaved continuous mode with period of 100 ms
sensor.startInterleavedContinuous(100);
}
void loop()
{
int i;
uint16_t val=0;
BH1750_Init(BH1750address);
delay(200);
if(2==BH1750_Read(BH1750address))
{
val=((buff[0]<<8)|buff[1])/1.2;
Serial.print(val,DEC);
Serial.println(" luxus");
}
Serial.print("Ambient: ");
Serial.print(sensor.readAmbientContinuous());
if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
Serial.print("\tRange: ");
Serial.print(sensor.readRangeContinuousMillimeters());
if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
Serial.println();
delay(150);
}
int BH1750_Read(int address) //
{
int i=0;
Wire.beginTransmission(address);
Wire.requestFrom(address, 2);
while(Wire.available()) //
{
buff[i] = Wire.read(); // receive one byte
i++;
}
Wire.endTransmission();
return i;
}
void BH1750_Init(int address)
{
Wire.beginTransmission(address);
Wire.write(0x10);//1lx reolution 120ms
Wire.endTransmission();
}
To see the output, open the Serial Monitor of Arduino from the menu:
Tools -> Serial Monitor
The lux reading from the BH1750 ambient light sensor is higher compared to the lux reported from the VL6180X ToF distance sensor. Unless you direct both sensors toward the light source, then they will be very similar. This indicates that the filed of view (FoV) of the BH1750 sensor is wider compared to the VL618X sensor.