-
Notifications
You must be signed in to change notification settings - Fork 44
/
VL53L0X.ino
51 lines (40 loc) · 1.38 KB
/
VL53L0X.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
// VL530LX is an Infrared Ranger
// Max. Distance < 2 meter
// Connect Vin to 3-5VDC
// Connect GND to ground
// Connect SCL to I2C clock pin (UNO: A5, NodeMCU: D1, ESP32: GIOP22, RTL8720DN: PA25)
// Connect SDA to I2C data pin (UNO: A4, NodeMCU: D2, ESP32: GIOP21, RTL8720DN: PA26)
#include <Wire.h>
#include <VL53L0X.h> // Pololu v1.3.0
VL53L0X ranger;
unsigned long lastTimeRanger;
unsigned long loopDelayRanger = 100; //ms
int distance;
void setup()
{
Serial.begin(115200);
Wire.begin();
ranger.init();
ranger.setTimeout(500);
// Start continuous back-to-back mode (take readings as fast as possible).
// To use continuous timed mode, instead, provide a desired inter-measurement period in ms
// (e.g. ranger.startContinuous(100))
//ranger.startContinuous();
}
void loop()
{
/* Continuouse Read */
// this mode consume much more power
//Serial.print(ranger.readRangeContinuousMillimeters());
//Serial.println(" mm");
//if (ranger.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
/* Single Read */
// this mode consume less power
if ((millis()-lastTimeRanger) > loopDelayRanger) {
distance = ranger.readRangeSingleMillimeters();
Serial.print(distance);
Serial.println(" mm");
lastTimeRanger = millis();
if (ranger.timeoutOccurred()) Serial.println("IR Ranger TIMEOUT");
}
}