Skip to content

Commit be55881

Browse files
committed
Initial commit
0 parents  commit be55881

12 files changed

+12898
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

LICENSE.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
SparkFun License Information
2+
============================
3+
4+
SparkFun uses two different licenses for our files — one for hardware and one for code.
5+
6+
Hardware
7+
---------
8+
9+
**SparkFun hardware is released under [Creative Commons Share-alike 4.0 International](http://creativecommons.org/licenses/by-sa/4.0/).**
10+
11+
Note: This is a human-readable summary of (and not a substitute for) the [license](http://creativecommons.org/licenses/by-sa/4.0/legalcode).
12+
13+
You are free to:
14+
15+
Share — copy and redistribute the material in any medium or format
16+
Adapt — remix, transform, and build upon the material
17+
for any purpose, even commercially.
18+
The licensor cannot revoke these freedoms as long as you follow the license terms.
19+
Under the following terms:
20+
21+
Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
22+
ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.
23+
No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits.
24+
Notices:
25+
26+
You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation.
27+
No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material.
28+
29+
30+
Code
31+
--------
32+
33+
**SparkFun code, firmware, and software is released under the MIT License(http://opensource.org/licenses/MIT).**
34+
35+
The MIT License (MIT)
36+
37+
Copyright (c) 2016 SparkFun Electronics
38+
39+
Permission is hereby granted, free of charge, to any person obtaining a copy
40+
of this software and associated documentation files (the "Software"), to deal
41+
in the Software without restriction, including without limitation the rights
42+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
43+
copies of the Software, and to permit persons to whom the Software is
44+
furnished to do so, subject to the following conditions:
45+
46+
The above copyright notice and this permission notice shall be included in all
47+
copies or substantial portions of the Software.
48+
49+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
52+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
54+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
55+
SOFTWARE.

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
SparkFun VL53L1X Distance Sensor Library
2+
===========================================================
3+
4+
![SparkFun VL53L1X Distance Sensor](https://cdn.sparkfun.com//assets/parts/1/2/8/6/3/14667-ToF_Laser_Distance_Sensor__Qwiic__-_VL53L1X-01.jpg)
5+
6+
[*SparkX Qwiic Distance Sensor - VL532L1X (SPX-14667)*](https://www.sparkfun.com/products/14667)
7+
8+
The VL53L1X is the latest Time Of Flight (ToF) sensor to be released. It uses a VCSEL (vertical cavity surface emitting laser) to emit a class 1 IR laser and time the reflection to the target. What does all this mean? You can measure the distance to an object up to 4 meters away with millimeter resolution! That’s pretty incredible.
9+
10+
We’re far from done: The VL53L1X is a highly complex sensor with a multitude of options and configurations. We’ve written example sketches that allow you to read the distance, signal rate, and range status. Because ST has chosen not to release a complete datasheet we are forced to reverse engineer the interface from their example code and I2C data stream captures. If you’re into puzzles we could use your help to make the library better!
11+
12+
We’ve found the precision of the sensor to be 1mm but the accuracy is around +/-5mm.
13+
14+
Library written by Nathan Seidle ([SparkFun](http://www.sparkfun.com)).
15+
16+
Repository Contents
17+
-------------------
18+
19+
* **/examples** - Example sketches for the library (.ino). Run these from the Arduino IDE.
20+
* **/src** - Source files for the library (.cpp, .h).
21+
* **keywords.txt** - Keywords from this library that will be highlighted in the Arduino IDE.
22+
* **library.properties** - General library properties for the Arduino package manager.
23+
24+
Documentation
25+
--------------
26+
27+
* **[Installing an Arduino Library Guide](https://learn.sparkfun.com/tutorials/installing-an-arduino-library)** - Basic information on how to install an Arduino library.
28+
* **[Product Repository](https://github.com/sparkfunX/Qwiic_Distance_VL53L1X)** - Main repository (including hardware files)
29+
30+
License Information
31+
-------------------
32+
33+
This product is _**open source**_!
34+
35+
Various bits of the code have different licenses applied. Anything SparkFun wrote is beerware; if you see me (or any other SparkFun employee) at the local, and you've found our code helpful, please buy us a round!
36+
37+
Please use, reuse, and modify these files as you see fit. Please maintain attribution to SparkFun Electronics and release anything derivative under the same license.
38+
39+
Distributed as-is; no warranty is given.
40+
41+
- Your friends at SparkFun.

SparkFun_VL53L1X_Arduino_Library.h

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
This is a library written for the VL53L1X
3+
SparkFun sells these at its website: www.sparkfun.com
4+
Do you like this library? Help support SparkFun. Buy a board!
5+
https://www.sparkfun.com/products/14667
6+
7+
Written by Nathan Seidle @ SparkFun Electronics, April 12th, 2018
8+
9+
The VL53L1X is the latest Time Of Flight (ToF) sensor to be released. It uses
10+
a VCSEL (vertical cavity surface emitting laser) to emit a class 1 IR laser
11+
and time the reflection to the target. What does all this mean? You can measure
12+
the distance to an object up to 4 meters away with millimeter resolution!
13+
14+
We’ve found the precision of the sensor to be 1mm but the accuracy is around +/-5mm.
15+
16+
This library handles the initialization of the VL53L1X and is able to query the sensor
17+
for different readings.
18+
19+
Because ST has chosen not to release a complete datasheet we are forced to reverse
20+
engineer the interface from their example code and I2C data stream captures.
21+
For ST's reference code see STSW-IMG007
22+
23+
The device operates as a normal I2C device. There are *many* registers. Reading and
24+
writing happens with a 16-bit address. The VL53L1X auto-increments the memory
25+
pointer with each read or write.
26+
27+
Development environment specifics:
28+
Arduino IDE 1.8.5
29+
30+
This program is distributed in the hope that it will be useful,
31+
but WITHOUT ANY WARRANTY; without even the implied warranty of
32+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33+
GNU General Public License for more details.
34+
35+
You should have received a copy of the GNU General Public License
36+
along with this program. If not, see <http://www.gnu.org/licenses/>.
37+
*/
38+
39+
#ifndef SPARKFUN_VL53L1X_ARDUINO_LIBRARY_H
40+
#define SPARKFUN_VL53L1X_ARDUINO_LIBRARY_H
41+
42+
#if (ARDUINO >= 100)
43+
#include "Arduino.h"
44+
#else
45+
#include "WProgram.h"
46+
#endif
47+
48+
#include <Wire.h>
49+
50+
#include "vl53l1_register_map.h"
51+
const byte defaultAddress_VL53L1X = 0x29; //The default I2C address for the VL53L1X on the SparkFun breakout is 0x29.
52+
53+
//Platform specific configurations
54+
55+
//Define the size of the I2C buffer based on the platform the user has
56+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
57+
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
58+
59+
//I2C_BUFFER_LENGTH is defined in Wire.H
60+
#define I2C_BUFFER_LENGTH BUFFER_LENGTH
61+
62+
#elif defined(__SAMD21G18A__)
63+
64+
//SAMD21 uses RingBuffer.h
65+
#define I2C_BUFFER_LENGTH SERIAL_BUFFER_SIZE
66+
67+
#elif __MK20DX256__
68+
//Teensy
69+
70+
#elif ARDUINO_ARCH_ESP32
71+
//ESP32 based platforms
72+
73+
#else
74+
75+
//The catch-all default is 32
76+
#define I2C_BUFFER_LENGTH 32
77+
78+
#endif
79+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
80+
81+
class VL53L1X {
82+
public:
83+
84+
boolean begin(uint8_t deviceAddress = defaultAddress_VL53L1X, TwoWire &wirePort = Wire); //By default use the default I2C addres, and use Wire port
85+
86+
void softReset(); //Reset the sensor via software
87+
void writeConfiguration(uint8_t offset = 0); //Write a block of bytes to the sensor to configure it to take a measurement
88+
boolean newDataReady(); //Polls the measurement completion bit
89+
uint16_t getDistance(); //Returns the results from the last measurement, distance in mm
90+
uint16_t getSignalRate(); //Returns the results from the last measurement, signal rate
91+
uint8_t getRangeStatus(); //Returns the results from the last measurement, 0 = valid
92+
93+
uint8_t readRegister(uint16_t addr); //Read a byte from a 16-bit address
94+
uint16_t readRegister16(uint16_t addr); //Read two bytes from a 16-bit address
95+
boolean writeRegister(uint16_t addr, uint8_t val); //Write a byte to a spot
96+
boolean writeRegister16(uint16_t addr, uint16_t val); //Write two bytes to a spot
97+
98+
private:
99+
100+
//Variables
101+
TwoWire *_i2cPort; //The generic connection to user's chosen I2C hardware
102+
uint8_t _deviceAddress; //Keeps track of I2C address. setI2CAddress changes this.
103+
};
104+
105+
#endif
106+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Reading distance from the laser based VL53L1X
3+
By: Nathan Seidle
4+
SparkFun Electronics
5+
Date: April 4th, 2018
6+
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
7+
8+
Feel like supporting our work? Buy a board from SparkFun! https://www.sparkfun.com/products/14667
9+
10+
This example prints the distance to an object.
11+
12+
Are you getting weird readings? Be sure the vacuum tape has been removed from the sensor.
13+
*/
14+
15+
#include <Wire.h>
16+
17+
#include "SparkFun_VL53L1X_Arduino_Library.h"
18+
VL53L1X distanceSensor;
19+
20+
void setup(void)
21+
{
22+
Wire.begin();
23+
24+
Serial.begin(9600);
25+
Serial.println("VL53L1X Qwiic Test");
26+
27+
if (distanceSensor.begin() == false)
28+
Serial.println("Sensor offline!");
29+
30+
}
31+
32+
void loop(void)
33+
{
34+
distanceSensor.startMeasurement(); //Write configuration bytes to initiate measurement
35+
36+
//Poll for completion of measurement. Takes 40-50ms.
37+
while (distanceSensor.newDataReady() == false)
38+
delay(5);
39+
40+
int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
41+
42+
Serial.print("Distance(mm): ");
43+
Serial.print(distance);
44+
45+
float distanceInches = distance * 0.0393701;
46+
float distanceFeet = distanceInches / 12.0;
47+
48+
Serial.print("\tDistance(ft): ");
49+
Serial.print(distanceFeet, 2);
50+
51+
Serial.println();
52+
}
53+
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
Reading distance from the laser based VL53L1X
3+
By: Nathan Seidle
4+
SparkFun Electronics
5+
Date: April 4th, 2018
6+
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
7+
8+
Feel like supporting our work? Buy a board from SparkFun! https://www.sparkfun.com/products/14667
9+
10+
This example demonstrates how to read and average distance, the measurement status, and the signal rate.
11+
12+
Are you getting weird readings? Be sure the vacuum tape has been removed from the sensor.
13+
*/
14+
15+
#include <Wire.h>
16+
17+
#include "SparkFun_VL53L1X_Arduino_Library.h"
18+
VL53L1X distanceSensor;
19+
20+
//Store distance readings to get rolling average
21+
#define HISTORY_SIZE 10
22+
int history[HISTORY_SIZE];
23+
byte historySpot;
24+
25+
void setup(void)
26+
{
27+
Wire.begin();
28+
Wire.setClock(400000); //Increase I2C bus speed to 400kHz
29+
30+
Serial.begin(9600);
31+
Serial.println("VL53L1X Qwiic Test");
32+
33+
if (distanceSensor.begin() == false)
34+
Serial.println("Sensor offline!");
35+
36+
for (int x = 0 ; x < HISTORY_SIZE ; x++)
37+
history[x] = 0;
38+
}
39+
40+
void loop(void)
41+
{
42+
long startTime = millis();
43+
distanceSensor.startMeasurement(); //Write configuration block of 135 bytes to setup a measurement
44+
45+
//Poll for completion of measurement. Takes 40-50ms.
46+
while (distanceSensor.newDataReady() == false)
47+
delay(5);
48+
49+
long endTime = millis();
50+
51+
int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
52+
53+
Serial.print("Distance(mm): ");
54+
Serial.print(distance);
55+
56+
history[historySpot] = distance;
57+
if (historySpot++ == HISTORY_SIZE) historySpot = 0;
58+
59+
long avgDistance = 0;
60+
for (int x = 0 ; x < HISTORY_SIZE ; x++)
61+
avgDistance += history[x];
62+
63+
avgDistance /= HISTORY_SIZE;
64+
Serial.print("\tavgDistance: ");
65+
Serial.print(avgDistance);
66+
67+
float distanceInches = avgDistance * 0.0393701;
68+
float distanceFeet = distanceInches / 12;
69+
70+
Serial.print("\tavgDistance(ft): ");
71+
Serial.print(distanceFeet, 2);
72+
73+
int signalRate = distanceSensor.getSignalRate();
74+
Serial.print("\tSignal rate: ");
75+
Serial.print(signalRate);
76+
77+
byte rangeStatus = distanceSensor.getRangeStatus();
78+
Serial.print("\tRange Status: ");
79+
80+
//Make it human readable
81+
switch (rangeStatus)
82+
{
83+
case 0:
84+
Serial.print("Good");
85+
break;
86+
case 1:
87+
Serial.print("Signal fail");
88+
break;
89+
case 2:
90+
Serial.print("Sigma fail");
91+
break;
92+
case 7:
93+
Serial.print("Wrapped target fail");
94+
break;
95+
default:
96+
Serial.print("Unknown: ");
97+
Serial.print(rangeStatus);
98+
break;
99+
}
100+
101+
Serial.print("\tHz: ");
102+
Serial.print(1000.0 / (float)(endTime - startTime), 2);
103+
104+
Serial.println();
105+
}
106+

0 commit comments

Comments
 (0)