-
Notifications
You must be signed in to change notification settings - Fork 2
/
VL6180x_1_sensor.ino
66 lines (55 loc) · 2.2 KB
/
VL6180x_1_sensor.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* 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();
}