|
| 1 | +/* |
| 2 | + ST VL53L0X example using Adafruit library |
| 3 | +
|
| 4 | + Reads the Vl53L0X sensor and reports the result in mm |
| 5 | +
|
| 6 | + created 29 July 2021 |
| 7 | + by Tom Igoe |
| 8 | +*/ |
| 9 | + |
| 10 | +// include library |
| 11 | +#include "Adafruit_VL53L0X.h" |
| 12 | + |
| 13 | +// make an instance of the library: |
| 14 | +Adafruit_VL53L0X sensor = Adafruit_VL53L0X(); |
| 15 | + |
| 16 | +const int maxDistance = 2000; |
| 17 | + |
| 18 | +void setup() { |
| 19 | + // initialize serial, wait 3 seconds for |
| 20 | + // Serial Monitor to open: |
| 21 | + Serial.begin(9600); |
| 22 | + if (!Serial) delay(3000); |
| 23 | + |
| 24 | + // initialize sensor, stop if it fails: |
| 25 | + if (!sensor.begin()) { |
| 26 | + Serial.println("Sensor not responding. Check wiring."); |
| 27 | + while (true); |
| 28 | + } |
| 29 | + /* config can be: |
| 30 | + VL53L0X_SENSE_DEFAULT: about 500mm range |
| 31 | + VL53L0X_SENSE_LONG_RANGE: about 2000mm range |
| 32 | + VL53L0X_SENSE_HIGH_SPEED: about 500mm range |
| 33 | + VL53L0X_SENSE_HIGH_ACCURACY: about 400mm range, 1mm accuracy |
| 34 | + */ |
| 35 | + sensor.configSensor(Adafruit_VL53L0X::VL53L0X_SENSE_LONG_RANGE); |
| 36 | + // set sensor to range continuously: |
| 37 | + sensor.startRangeContinuous(); |
| 38 | +} |
| 39 | + |
| 40 | +void loop() { |
| 41 | + // if the reading is done: |
| 42 | + if (sensor.isRangeComplete()) { |
| 43 | + // read the result: |
| 44 | + int result = sensor.readRangeResult(); |
| 45 | + // if it's with the max distance: |
| 46 | + if (result < maxDistance) { |
| 47 | + // print the result (distance in mm): |
| 48 | + Serial.println(result); |
| 49 | + |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments