Skip to content

Commit 83195ac

Browse files
committed
Merged pull request and added One Shot example to library.
1 parent ce448e7 commit 83195ac

File tree

5 files changed

+90
-3
lines changed

5 files changed

+90
-3
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Reading distance from the laser based VL53L1X using a one-shot measurement.
3+
By: Nathan Seidle
4+
Revised by: Andy England, Ricardo Ramos
5+
Pull request by: Joseph Duchesne
6+
SparkFun Electronics
7+
Date: January 31st, 2022
8+
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
9+
10+
SparkFun labored with love to create this code. Feel like supporting open source hardware?
11+
Buy a board from SparkFun! https://www.sparkfun.com/products/14667
12+
13+
This example makes a one shot measurement.
14+
15+
Are you getting weird readings? Be sure the vacuum tape has been removed from the sensor.
16+
*/
17+
18+
#include <Wire.h>
19+
#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X
20+
21+
//Optional interrupt and shutdown pins.
22+
#define SHUTDOWN_PIN 2
23+
#define INTERRUPT_PIN 3
24+
25+
SFEVL53L1X distanceSensor;
26+
//Uncomment the following line to use the optional shutdown and interrupt pins.
27+
//SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN);
28+
29+
void setup(void)
30+
{
31+
Wire.begin();
32+
33+
Serial.begin(115200);
34+
Serial.println("VL53L1X Qwiic Test");
35+
36+
if (distanceSensor.begin() != 0) //Begin returns 0 on a good init
37+
{
38+
Serial.println("Sensor failed to begin. Please check wiring. Freezing...");
39+
while (1)
40+
;
41+
}
42+
Serial.println("Sensor online!");
43+
44+
45+
}
46+
47+
void loop(void)
48+
{
49+
distanceSensor.startOneshotRanging(); //Write configuration bytes to initiate measurement
50+
while (!distanceSensor.checkForDataReady())
51+
{
52+
delay(1);
53+
}
54+
int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
55+
56+
Serial.print("Distance(mm): ");
57+
Serial.print(distance);
58+
59+
float distanceInches = distance * 0.0393701;
60+
float distanceFeet = distanceInches / 12.0;
61+
62+
Serial.print("\tDistance(ft): ");
63+
Serial.print(distanceFeet, 2);
64+
65+
Serial.println();
66+
}

src/SparkFun_VL53L1X.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ void SFEVL53L1X::startRanging()
133133
_device->VL53L1X_StartRanging();
134134
}
135135

136+
void SFEVL53L1X::startOneshotRanging()
137+
{
138+
_device->VL53L1X_StartOneshotRanging();
139+
}
140+
136141
void SFEVL53L1X::stopRanging()
137142
{
138143
_device->VL53L1X_StopRanging();

src/SparkFun_VL53L1X.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class SFEVL53L1X
6363
void setInterruptPolarityLow(); //Set the polarity of an active interrupt to Low
6464
uint8_t getInterruptPolarity(); //get the current interrupt polarity
6565
void startRanging(); //Begins taking measurements
66+
void startOneshotRanging(); // Start one-shot ranging
6667
void stopRanging(); //Stops taking measurements
6768
bool checkForDataReady(); //Checks the to see if data is ready
6869
void setTimingBudgetInMs(uint16_t timingBudget); //Set the timing budget for a measurement

src/vl53l1x_class.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,21 @@ VL53L1X_ERROR VL53L1X::VL53L1X_GetInterruptPolarity(uint8_t *pInterruptPolarity)
223223
VL53L1X_ERROR VL53L1X::VL53L1X_StartRanging()
224224
{
225225
VL53L1X_ERROR status = 0;
226-
226+
VL53L1_WrByte(Device, SYSTEM__INTERRUPT_CLEAR, 0x01); /* clear interrupt trigger */
227227
status = VL53L1_WrByte(Device, SYSTEM__MODE_START, 0x40); /* Enable VL53L1X */
228228
return status;
229229
}
230230

231+
232+
VL53L1X_ERROR VL53L1X::VL53L1X_StartOneshotRanging()
233+
{
234+
VL53L1X_ERROR status = 0;
235+
VL53L1_WrByte(Device, SYSTEM__INTERRUPT_CLEAR, 0x01); /* clear interrupt trigger */
236+
status = VL53L1_WrByte(Device, SYSTEM__MODE_START, 0x10); /* Enable VL53L1X one-shot ranging */
237+
return status;
238+
}
239+
240+
231241
VL53L1X_ERROR VL53L1X::VL53L1X_StopRanging()
232242
{
233243
VL53L1X_ERROR status = 0;
@@ -963,7 +973,7 @@ VL53L1X_ERROR VL53L1X::VL53L1_RdDWord(VL53L1_DEV Dev, uint16_t index, uint32_t *
963973
status = VL53L1_I2CRead(Dev->I2cDevAddr, index, buffer, 4);
964974
if (!status)
965975
{
966-
*data = (buffer[0] << 24) + (buffer[1] << 16) + (buffer[2] << 8) + buffer[3];
976+
*data = (buffer[0] << 24) + (buffer[1] << 16U) + (buffer[2] << 8) + buffer[3];
967977
}
968978
return status;
969979
}
@@ -998,7 +1008,7 @@ VL53L1X_ERROR VL53L1X::VL53L1_I2CWrite(uint8_t DeviceAddr, uint16_t RegisterAddr
9981008
buffer[0] = RegisterAddr >> 8;
9991009
buffer[1] = RegisterAddr & 0xFF;
10001010
dev_i2c->write(buffer, 2);
1001-
for (int i = 0; i < NumByteToWrite; i++)
1011+
for (uint16_t i = 0; i < NumByteToWrite; i++)
10021012
dev_i2c->write(pBuffer[i]);
10031013

10041014
dev_i2c->endTransmission(true);

src/vl53l1x_class.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,11 @@ class VL53L1X : public RangeSensor
304304
*/
305305
VL53L1X_ERROR VL53L1X_StartRanging();
306306

307+
/**
308+
* @brief This function starts a one-shot ranging distance operation\n
309+
*/
310+
VL53L1X_ERROR VL53L1X_StartOneshotRanging();
311+
307312
/**
308313
* @brief This function stops the ranging.
309314
*/

0 commit comments

Comments
 (0)