I am using multiple vl6180x sensors along with Arduino UNO to detect the light and distance of each sensor. Right now, when I tried to connect three sensors applying i2c protocol(after removing four 10k resistors from two of the sensors for pull up resistors). Results read from serial monitor of Arduino shows the minimum values of three sensors. And the sensitivity, precision for these sensors drop a lot, which I am not satisfied with.
Then I used "digitalWrite" function in Arduino and applied to VL6180X pin "GPIO01-CEO" to enable certain sensor and disable certain sensor in the Arduino code. Wired thing is that it only works when the first time I set them, like if I put something like enable sensor 1, disable sensor 2, disable sensor 3, delay 1s in the loop, it works well. But later if I continue put enable sensor 2, disable sensor 1, disable sensor 3, then the results made no sense, the whole system cant work. Another thing is that I can only enable one sensor at a time, which is fine as it is described as the i2c protocol that only one slave could do the operation at a time.
Also, the address shows on serial monitor by using function " sensor.readReg16Bit(VL6180X::I2C_SLAVE__DEVICE_ADDRESS);" makes no sense once I use "setAddress" function to change the sensor address.
There are some files/ datasheet provided online for vl6180x sensor, declaring that this sensor could apply i2c and make multiple of them working properly. Till now, I don't know if this address problem, changing sensor problem lies on the Arduino side or the sensor side. As there are also many videos on youtube indicates Arduino is totally capable of applying i2c protocol. Any help will be appreciated^^
#include <Wire.h>
#include <VL6180X.h>
#define Address1 0x29
#define Address2 0xA9
#define Address3 0x169
VL6180X sensor;
int pin2 = 2;
int pin4 = 4;
int pin7 = 7;
int pin3 = 3;
int pin5 = 5;
int pin6 = 6;
void setup()
{
Serial.begin(9600);
Wire.begin();
pinMode(pin2,OUTPUT);
pinMode(pin4,OUTPUT);
pinMode(pin7,OUTPUT);
pinMode(pin3,OUTPUT);
pinMode(pin5,OUTPUT);
pinMode(pin6,OUTPUT);
digitalWrite(pin2,HIGH);
digitalWrite(pin4,HIGH);
digitalWrite(pin7,HIGH);
digitalWrite(pin3,HIGH);
digitalWrite(pin5,HIGH);
digitalWrite(pin6,HIGH);
sensor.init();
sensor.configureDefault();
Wire.beginTransmission(0x29);
int error = Wire.endTransmission();
Serial.println(error);
// Wire.beginTransmission(VL6180X::I2C_SLAVE__DEVICE_ADDRESS);
// Wire.read(0x52);
// 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.readReg16Bit(VL6180X::I2C_SLAVE__DEVICE_ADDRESS);
sensor.writeReg(VL6180X::SYSRANGE__MAX_CONVERGENCE_TIME, 30);
sensor.writeReg16Bit(VL6180X::SYSALS__INTEGRATION_PERIOD, 50);
// sensor.writeReg16Bit(VL6180X::SYSTEM__MODE_GPIO0,0);
sensor.writeReg16Bit(VL6180X::INTERLEAVED_MODE__ENABLE,0);
// sensor.setAddress(VL6180X::I2C_SLAVE__DEVICE_ADDRESS);
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);
//sensor.setAddress(Address1);
//// sensor1.setAddress(Address2);
// sensor1.init();
// sensor1.configureDefault();
// //sensor1.setAddress(Address4);
// sensor1.writeReg(VL6180X::SYSRANGE__MAX_CONVERGENCE_TIME, 30);
// sensor1.writeReg16Bit(VL6180X::SYSALS__INTEGRATION_PERIOD, 50);
// sensor1.setTimeout(500);
// sensor1.stopContinuous();
// delay(300);
// sensor1.startInterleavedContinuous(100);
}
void loop()
{
// digitalWrite(pin2,HIGH);
// digitalWrite(pin4,HIGH);
// digitalWrite(pin7,HIGH);
// digitalWrite(pin3,HIGH);
// digitalWrite(pin5,HIGH);
// digitalWrite(pin6,HIGH);
// int input = random(1,4);
// Serial.println(input);
// if (input == 1){
// hello1();
// // int error = Wire.endTransmission();
// // Serial.println(error);
// }
//
// else if (input == 2){
// hello2();
// //int error = Wire.endTransmission();
// //Serial.println(error);
// }
//
// else if (input == 3) {
// hello3();
// // int error = Wire.endTransmission();
// // Serial.println(error);
// }
hello1();
// hello2();
// hello3();
// Serial.print(sensor.readReg16Bit(VL6180X::SYSTEM__MODE_GPIO0));
// Serial.println();
//// Wire.beginTransmission(0x00);
//// Wire.write(0);
//// Wire.endTransmission();
//// sensor.setAddress(VL6180X::I2C_SLAVE__DEVICE_ADDRESS);
// //sensor.setAddress(Address2);
// //digitalWrite(pin4,HIGH);
// // digitalWrite(pin4,HIGH);
// //digitalWrite(pin7,HIGH);
// Serial.print("Ambient 1: ");
// Serial.print(sensor.readAmbientContinuous());
// if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
//
// Serial.print("\tRange 1: ");
// Serial.print(sensor.readRangeContinuousMillimeters());
// if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
//
// Serial.println();
//
// delay(1000);
}
void hello1(){
sensor.setAddress(0x80);
Wire.beginTransmission(0x80);
int error = Wire.endTransmission();
Serial.println(error);
digitalWrite(pin2,HIGH);
digitalWrite(pin4,HIGH);
digitalWrite(pin7,HIGH);
digitalWrite(pin3,LOW);
digitalWrite(pin5,LOW);
digitalWrite(pin6,HIGH);
sensor.setAddress(Address1);
Serial.print(sensor.readReg16Bit(VL6180X::SYSTEM__MODE_GPIO0));
Serial.println();
Serial.print("Ambient 1: ");
Serial.print(sensor.readAmbientContinuous());
if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
Serial.print("\tRange 1: ");
Serial.print(sensor.readRangeContinuousMillimeters());
if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
Serial.println();
delay(1000);
}
void hello2(){
sensor.setAddress(0x82);
Wire.beginTransmission(0x82);
int error = Wire.endTransmission();
Serial.println(error);
digitalWrite(pin2,HIGH);
digitalWrite(pin4,HIGH);
digitalWrite(pin7,HIGH);
digitalWrite(pin3,LOW);
digitalWrite(pin5,HIGH);
digitalWrite(pin6,LOW);
sensor.setAddress(Address2);
//sensor.setAddress(VL6180X::I2C_SLAVE__DEVICE_ADDRESS);
Serial.print(sensor.readReg16Bit(VL6180X::SYSTEM__MODE_GPIO0));
Serial.println();
Serial.print("Ambient 2: ");
Serial.print(sensor.readAmbientContinuous());
if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
Serial.print("\tRange 2: ");
Serial.print(sensor.readRangeContinuousMillimeters());
if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
Serial.println();
delay(1000);
}
void hello3(){
sensor.setAddress(0x84);
Wire.beginTransmission(0x84);
int error = Wire.endTransmission();
Serial.println(error);
digitalWrite(pin2,HIGH);
digitalWrite(pin4,HIGH);
digitalWrite(pin7,HIGH);
digitalWrite(pin3,HIGH);
digitalWrite(pin5,LOW);
digitalWrite(pin6,LOW);
sensor.setAddress(Address3);
Serial.print(sensor.readReg16Bit(VL6180X::SYSTEM__MODE_GPIO0));
Serial.println();
Serial.print("Ambient 3: ");
Serial.print(sensor.readAmbientContinuous());
if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
Serial.print("\tRange 3: ");
Serial.print(sensor.readRangeContinuousMillimeters());
if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
Serial.println();
delay(1000);
}