Skip to content

Commit ce448e7

Browse files
committed
Added get and set detection threshold functionality.
1 parent 71314d1 commit ce448e7

File tree

3 files changed

+186
-1
lines changed

3 files changed

+186
-1
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
Reading distance from the laser based VL53L1X
3+
By: Nathan Seidle
4+
Revised by: Andy England and Ricardo Ramos
5+
SparkFun Electronics
6+
Date: January 21st, 2022
7+
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
8+
9+
SparkFun labored with love to create this code. Feel like supporting open source hardware?
10+
Buy a board from SparkFun! https://www.sparkfun.com/products/14667
11+
12+
This example sets high and low thresholds for detection.
13+
14+
Are you getting weird readings? Be sure the vacuum tape has been removed from the sensor.
15+
*/
16+
17+
#include <Wire.h>
18+
#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X
19+
20+
//Optional interrupt and shutdown pins.
21+
#define SHUTDOWN_PIN 2
22+
#define INTERRUPT_PIN 3
23+
24+
SFEVL53L1X distanceSensor;
25+
//Uncomment the following line to use the optional shutdown and interrupt pins.
26+
//SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN);
27+
28+
void setup(void)
29+
{
30+
Wire.begin();
31+
32+
Serial.begin(115200);
33+
Serial.println("VL53L1X Qwiic Test");
34+
35+
if (distanceSensor.begin() != 0) //Begin returns 0 on a good init
36+
{
37+
Serial.println("Sensor failed to begin. Please check wiring. Freezing...");
38+
while (1)
39+
;
40+
}
41+
Serial.println("Sensor online!");
42+
43+
DetectionConfig dc; // struct instance which holds the detection configuration
44+
dc.IntOnNoTarget = 1; // No longer used - just use 1 per ST
45+
dc.distanceMode = DISTANCE_SHORT; // short distance mode
46+
dc.thresholdHigh = 300; // high threshold of 300 mm
47+
dc.thresholdLow = 70; // low threshold of 70 mm
48+
dc.windowMode = WINDOW_IN; // will measure and trigger interrrupt when measurement fall between 70 and 300 mm
49+
50+
if(distanceSensor.setThresholdConfig(&dc) == true)
51+
{
52+
Serial.println("Thresholds programmed. Reading configuration back...");
53+
dc = {};
54+
if(distanceSensor.getThresholdConfig(&dc) == true)
55+
{
56+
Serial.print("IntOnNoTarget: ");
57+
Serial.println(dc.IntOnNoTarget);
58+
Serial.print("Distance Mode: ");
59+
60+
if(dc.distanceMode == DISTANCE_SHORT)
61+
Serial.println("DISTANCE_SHORT (0)");
62+
else
63+
Serial.println("DISTANCE_LONG (1)");
64+
65+
Serial.print("Threshold High: ");
66+
Serial.println(dc.thresholdHigh);
67+
Serial.print("Threshold Low: ");
68+
Serial.println(dc.thresholdLow);
69+
Serial.print("Window mode: ");
70+
switch (dc.windowMode)
71+
{
72+
case WINDOW_BELOW:
73+
Serial.println("WINDOW_BELOW (0)");
74+
break;
75+
76+
case WINDOW_ABOVE:
77+
Serial.println("WINDOW_ABOVE (1)");
78+
break;
79+
80+
case WINDOW_OUT:
81+
Serial.println("WINDOW_OUT (2)");
82+
break;
83+
84+
case WINDOW_IN:
85+
Serial.println("WINDOW_IN (3)");
86+
break;
87+
88+
default:
89+
break;
90+
}
91+
}
92+
}
93+
else
94+
{
95+
Serial.println("Could not set threshold configuration.");
96+
}
97+
Serial.println("Starting measurements in 2 seconds.");
98+
delay(2000);
99+
Serial.println("Measurements started!");
100+
101+
}
102+
103+
void loop(void)
104+
{
105+
distanceSensor.startRanging(); //Write configuration bytes to initiate measurement
106+
107+
while (!distanceSensor.checkForDataReady())
108+
{
109+
delay(1);
110+
}
111+
int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
112+
distanceSensor.clearInterrupt();
113+
114+
distanceSensor.stopRanging();
115+
116+
Serial.print("Distance(mm): ");
117+
Serial.print(distance);
118+
119+
float distanceInches = distance * 0.0393701;
120+
float distanceFeet = distanceInches / 12.0;
121+
122+
Serial.print("\tDistance(ft): ");
123+
Serial.print(distanceFeet, 2);
124+
125+
Serial.println();
126+
}

src/SparkFun_VL53L1X.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ bool SFEVL53L1X::checkID()
5454
{
5555
uint16_t sensorId;
5656
_device->VL53L1X_GetSensorId(&sensorId);
57-
if ( (sensorId == 0xEACC) || (sensorId == 0xEBAA) )
57+
if ((sensorId == 0xEACC) || (sensorId == 0xEBAA))
5858
return true;
5959
return false;
6060
}
@@ -360,3 +360,43 @@ void SFEVL53L1X::calibrateXTalk(uint16_t targetDistanceInMm)
360360
uint16_t xTalk = getXTalk();
361361
_device->VL53L1X_CalibrateXtalk(targetDistanceInMm, &xTalk);
362362
};
363+
364+
bool SFEVL53L1X::setThresholdConfig(DetectionConfig *config)
365+
{
366+
VL53L1X_ERROR error = _device->VL53L1X_SetDistanceThreshold(config->thresholdLow, config->thresholdHigh,
367+
(uint8_t)config->windowMode, (uint8_t)config->IntOnNoTarget);
368+
return (error == VL53L1_ERROR_NONE);
369+
}
370+
371+
bool SFEVL53L1X::getThresholdConfig(DetectionConfig *config)
372+
{
373+
uint16_t temp16 = 0;
374+
375+
VL53L1X_ERROR error = _device->VL53L1X_GetDistanceMode(&temp16);
376+
if (error != 0)
377+
return false;
378+
else
379+
config->distanceMode = temp16;
380+
381+
error = _device->VL53L1X_GetDistanceThresholdWindow(&temp16);
382+
if (error != 0)
383+
return false;
384+
else
385+
config->windowMode = temp16;
386+
387+
config->IntOnNoTarget = 1;
388+
389+
error = _device->VL53L1X_GetDistanceThresholdLow(&temp16);
390+
if (error != 0)
391+
return false;
392+
else
393+
config->thresholdLow = temp16;
394+
395+
error = _device->VL53L1X_GetDistanceThresholdHigh(&temp16);
396+
if (error != 0)
397+
return false;
398+
else
399+
config->thresholdHigh = temp16;
400+
401+
return true;
402+
}

src/SparkFun_VL53L1X.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@
3030
#include "vl53l1_error_codes.h"
3131
#include "vl53l1x_class.h"
3232

33+
#define DISTANCE_SHORT 0
34+
#define DISTANCE_LONG 1
35+
#define WINDOW_BELOW 0
36+
#define WINDOW_ABOVE 1
37+
#define WINDOW_OUT 2
38+
#define WINDOW_IN 3
39+
40+
struct DetectionConfig
41+
{
42+
uint16_t distanceMode = DISTANCE_SHORT; // distance mode : DISTANCE_SHORT (0) or DISTANCE_LONG (1)
43+
uint16_t windowMode = WINDOW_IN; // window mode : WINDOW_BELOW (0), WINDOW_ABOVE (1), WINDOW_OUT (2), WINDOW_IN (3)
44+
uint8_t IntOnNoTarget = 1; // = 1 (No longer used - just use 1)
45+
uint16_t thresholdHigh = 0; // (in mm) : the threshold above which one the device raises an interrupt if Window = 1
46+
uint16_t thresholdLow = 0; // (in mm) : the threshold under which one the device raises an interrupt if Window = 0
47+
};
48+
3349
class SFEVL53L1X
3450
{
3551
public:
@@ -108,6 +124,9 @@ class SFEVL53L1X
108124
void startTemperatureUpdate(); //Recalibrates the sensor for temperature changes. Run this any time the temperature has changed by more than 8°C
109125
void calibrateOffset(uint16_t targetDistanceInMm); //Autocalibrate the offset by placing a target a known distance away from the sensor and passing this known distance into the function.
110126
void calibrateXTalk(uint16_t targetDistanceInMm); //Autocalibrate the crosstalk by placing a target a known distance away from the sensor and passing this known distance into the function.
127+
bool setThresholdConfig(DetectionConfig* config); //Sets threshold configuration struct
128+
bool getThresholdConfig(DetectionConfig* config); //Gets current threshold settings. Returns false on error, true otherwise. Stores results in pointer to struct argument.
129+
111130
private:
112131
TwoWire *_i2cPort;
113132
int _shutdownPin;

0 commit comments

Comments
 (0)