The default libraries and sketches for the VL6180X sensor all use the
I2C protocol (communication bus). The sensor data is sent in a serial address format using only two (2) wires: Serial Clock (or SCL) and Serial Data (or SDA).
I2C and the Arduino Nano board
Larger boards have dedicated connectors for SDA and SCL, but not the Nano board. Instead the Nano board uses the analogue ports A4 for SDA and A5 for SCL. This is a standard for Arduino boards lacking dedicated ports for SDA and SCL.
/* This minimal example shows how to get single-shot range
measurements from the VL6180X.
The range readings are in units of mm. */
#include <Wire.h>
#include <VL6180X.h>
VL6180X sensor;
void setup()
{
Serial.begin(9600);
Wire.begin();
sensor.init();
sensor.configureDefault();
sensor.setTimeout(500);
}
void loop()
{
Serial.print(sensor.readRangeSingleMillimeters());
if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
Serial.println();
}
/* This example demonstrates how to use interleaved mode to
take continuous range and ambient light measurements. The
datasheet recommends using interleaved mode instead of
running "range and ALS continuous modes simultaneously (i.e.
asynchronously)".
In order to attain a faster update rate (10 Hz), the max
convergence time for ranging and integration time for
ambient light measurement are reduced from the normally
recommended defaults. See section 2.4.4 ("Continuous mode
limits") and Table 6 ("Interleaved mode limits (10 Hz
operation)") in the VL6180X datasheet for more details.
Raw ambient light readings can be converted to units of lux
using the equation in datasheet section 2.13.4 ("ALS count
to lux conversion").
Example: A VL6180X gives an ambient light reading of 613
with the default gain of 1 and an integration period of
50 ms as configured in this sketch (reduced from 100 ms as
set by configureDefault()). With the factory calibrated
resolution of 0.32 lux/count, the light level is therefore
(0.32 * 613 * 100) / (1 * 50) or 392 lux.
The range readings are in units of mm. */
#include <Wire.h>
#include <VL6180X.h>
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()
{
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();
}
Other alternatives
The post VL6810X sketch and wiring - UNO contains links to alternative sketches that retrieves more information from the VL6810X module.