Skip to content

Commit d21ce80

Browse files
committed
split repo
1 parent fe7ec83 commit d21ce80

3 files changed

Lines changed: 365 additions & 0 deletions

File tree

Libraries/Arduino/SFE_VL6180X.cpp

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
/******************************************************************************
2+
SFE_VL6180x.cpp
3+
Library for VL6180x time of flight range finder.
4+
Casey Kuhns @ SparkFun Electronics
5+
10/29/2014
6+
https://github.com/sparkfun/
7+
8+
The VL6180x by ST micro is a time of flight range finder that
9+
uses pulsed IR light to determine distances from object at close
10+
range. The average range of a sensor is between 0-200mm
11+
12+
In this file are the functions in the VL6180x class
13+
14+
Resources:
15+
This library uses the Arduino Wire.h to complete I2C transactions.
16+
17+
Development environment specifics:
18+
IDE: Arduino 1.0.5
19+
Hardware Platform: Arduino Pro 3.3V/8MHz
20+
VL6180x Breakout Version: 1.0
21+
22+
23+
This code is beerware. If you see me (or any other SparkFun employee) at the
24+
local pub, and you've found our code helpful, please buy us a round!
25+
26+
Distributed as-is; no warranty is given.
27+
******************************************************************************/
28+
29+
#include <Wire.h>
30+
#include "SFE_VL6180X.h"
31+
32+
VL6180x::VL6180x(uint8_t address)
33+
// Initialize the Library
34+
{
35+
Wire.begin(); // Arduino Wire library initializer
36+
_i2caddress = address; //set default address for communication
37+
}
38+
39+
40+
uint8_t VL6180x::VL6180xInit(void){
41+
uint8_t data; //for temp data storage
42+
43+
data = VL6180x_getRegister(VL6180X_SYSTEM_FRESH_OUT_OF_RESET);
44+
45+
if(data != 1) return VL6180x_FAILURE_RESET;
46+
47+
//Required by datasheet
48+
//http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf
49+
VL6180x_setRegister(0x0207, 0x01);
50+
VL6180x_setRegister(0x0208, 0x01);
51+
VL6180x_setRegister(0x0096, 0x00);
52+
VL6180x_setRegister(0x0097, 0xfd);
53+
VL6180x_setRegister(0x00e3, 0x00);
54+
VL6180x_setRegister(0x00e4, 0x04);
55+
VL6180x_setRegister(0x00e5, 0x02);
56+
VL6180x_setRegister(0x00e6, 0x01);
57+
VL6180x_setRegister(0x00e7, 0x03);
58+
VL6180x_setRegister(0x00f5, 0x02);
59+
VL6180x_setRegister(0x00d9, 0x05);
60+
VL6180x_setRegister(0x00db, 0xce);
61+
VL6180x_setRegister(0x00dc, 0x03);
62+
VL6180x_setRegister(0x00dd, 0xf8);
63+
VL6180x_setRegister(0x009f, 0x00);
64+
VL6180x_setRegister(0x00a3, 0x3c);
65+
VL6180x_setRegister(0x00b7, 0x00);
66+
VL6180x_setRegister(0x00bb, 0x3c);
67+
VL6180x_setRegister(0x00b2, 0x09);
68+
VL6180x_setRegister(0x00ca, 0x09);
69+
VL6180x_setRegister(0x0198, 0x01);
70+
VL6180x_setRegister(0x01b0, 0x17);
71+
VL6180x_setRegister(0x01ad, 0x00);
72+
VL6180x_setRegister(0x00ff, 0x05);
73+
VL6180x_setRegister(0x0100, 0x05);
74+
VL6180x_setRegister(0x0199, 0x05);
75+
VL6180x_setRegister(0x01a6, 0x1b);
76+
VL6180x_setRegister(0x01ac, 0x3e);
77+
VL6180x_setRegister(0x01a7, 0x1f);
78+
VL6180x_setRegister(0x0030, 0x00);
79+
80+
return 0;
81+
}
82+
83+
void VL6180x::VL6180xDefautSettings(void){
84+
//Recommended settings from datasheet
85+
//http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf
86+
87+
//Enable Interrupts on Conversion Complete (any source)
88+
VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO, (4 << 3)|(4) ); // Set GPIO1 high when sample complete
89+
90+
91+
VL6180x_setRegister(VL6180X_SYSTEM_MODE_GPIO1, 0x10); // Set GPIO1 high when sample complete
92+
VL6180x_setRegister(VL6180X_READOUT_AVERAGING_SAMPLE_PERIOD, 0x30); //Set Avg sample period
93+
VL6180x_setRegister(VL6180X_SYSALS_ANALOGUE_GAIN, 0x46); // Set the ALS gain
94+
VL6180x_setRegister(VL6180X_SYSRANGE_VHV_REPEAT_RATE, 0xFF); // Set auto calibration period (Max = 255)/(OFF = 0)
95+
VL6180x_setRegister(VL6180X_SYSALS_INTEGRATION_PERIOD, 0x63); // Set ALS integration time to 100ms
96+
VL6180x_setRegister(VL6180X_SYSRANGE_VHV_RECALIBRATE, 0x01); // perform a single temperature calibration
97+
//Optional settings from datasheet
98+
//http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf
99+
VL6180x_setRegister(VL6180X_SYSRANGE_INTERMEASUREMENT_PERIOD, 0x09); // Set default ranging inter-measurement period to 100ms
100+
VL6180x_setRegister(VL6180X_SYSALS_INTERMEASUREMENT_PERIOD, 0x31); // Set default ALS inter-measurement period to 500ms
101+
VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO, 0x24); // Configures interrupt on ‘New Sample Ready threshold event’
102+
//Additional settings defaults from community
103+
VL6180x_setRegister(VL6180X_SYSRANGE_MAX_CONVERGENCE_TIME, 0x32);
104+
VL6180x_setRegister(VL6180X_SYSRANGE_RANGE_CHECK_ENABLES, 0x10 | 0x01);
105+
VL6180x_setRegister16bit(VL6180X_SYSRANGE_EARLY_CONVERGENCE_ESTIMATE, 0x7B );
106+
VL6180x_setRegister16bit(VL6180X_SYSALS_INTEGRATION_PERIOD, 0x63);
107+
108+
VL6180x_setRegister(VL6180X_READOUT_AVERAGING_SAMPLE_PERIOD,0x30);
109+
VL6180x_setRegister(VL6180X_SYSALS_ANALOGUE_GAIN,0x40);
110+
VL6180x_setRegister(VL6180X_FIRMWARE_RESULT_SCALER,0x01);
111+
}
112+
void VL6180x::getIdentification(struct VL6180xIdentification *temp){
113+
114+
temp->idModel = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODEL_ID);
115+
temp->idModelRevMajor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODEL_REV_MAJOR);
116+
temp->idModelRevMinor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODEL_REV_MINOR);
117+
temp->idModuleRevMajor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODULE_REV_MAJOR);
118+
temp->idModuleRevMinor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODULE_REV_MINOR);
119+
120+
temp->idDate = VL6180x_getRegister16bit(VL6180X_IDENTIFICATION_DATE);
121+
temp->idTime = VL6180x_getRegister16bit(VL6180X_IDENTIFICATION_TIME);
122+
}
123+
124+
125+
uint8_t VL6180x::getDistance()
126+
{
127+
VL6180x_setRegister(VL6180X_SYSRANGE_START, 0x01); //Start Single shot mode
128+
delay(10);
129+
return VL6180x_getRegister(VL6180X_RESULT_RANGE_VAL);
130+
VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07);
131+
// return distance;
132+
}
133+
uint8_t VL6180x::getAmbientLight()
134+
{
135+
VL6180x_setRegister(VL6180X_SYSALS_START, 0x01);
136+
delay(100);
137+
VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07);
138+
return VL6180x_getRegister(VL6180X_RESULT_ALS_VAL);
139+
}
140+
141+
// --- Private Functions --- //
142+
143+
uint8_t VL6180x::VL6180x_getRegister(uint16_t registerAddr)
144+
{
145+
uint8_t data;
146+
147+
Wire.beginTransmission( _i2caddress ); // Address set on class instantiation
148+
Wire.write((registerAddr >> 8) & 0xFF); //MSB of register address
149+
Wire.write(registerAddr & 0xFF); //LSB of register address
150+
Wire.endTransmission(false); //Send address and register address bytes
151+
Wire.requestFrom( _i2caddress , 1);
152+
data = Wire.read(); //Read Data from selected register
153+
154+
return data;
155+
}
156+
157+
uint16_t VL6180x::VL6180x_getRegister16bit(uint16_t registerAddr)
158+
{
159+
uint8_t data_low;
160+
uint8_t data_high;
161+
uint16_t data;
162+
163+
Wire.beginTransmission( _i2caddress ); // Address set on class instantiation
164+
Wire.write((registerAddr >> 8) & 0xFF); //MSB of register address
165+
Wire.write(registerAddr & 0xFF); //LSB of register address
166+
Wire.endTransmission(false); //Send address and register address bytes
167+
168+
Wire.requestFrom( _i2caddress, 2);
169+
data_high = Wire.read(); //Read Data from selected register
170+
data_low = Wire.read(); //Read Data from selected register
171+
data = (data_high << 8)|data_low;
172+
173+
return data;
174+
}
175+
176+
void VL6180x::VL6180x_setRegister(uint16_t registerAddr, uint8_t data)
177+
{
178+
Wire.beginTransmission( _i2caddress ); // Address set on class instantiation
179+
Wire.write((registerAddr >> 8) & 0xFF); //MSB of register address
180+
Wire.write(registerAddr & 0xFF); //LSB of register address
181+
Wire.write(data); // Data/setting to be sent to device.
182+
Wire.endTransmission(); //Send address and register address bytes
183+
}
184+
185+
void VL6180x::VL6180x_setRegister16bit(uint16_t registerAddr, uint16_t data)
186+
{
187+
Wire.beginTransmission( _i2caddress ); // Address set on class instantiation
188+
Wire.write((registerAddr >> 8) & 0xFF); //MSB of register address
189+
Wire.write(registerAddr & 0xFF); //LSB of register address
190+
uint8_t temp;
191+
temp = (data >> 8) & 0xff;
192+
Wire.write(temp); // Data/setting to be sent to device
193+
temp = data & 0xff;
194+
Wire.write(temp); // Data/setting to be sent to device
195+
Wire.endTransmission(); //Send address and register address bytes
196+
}
197+
198+

Libraries/Arduino/SFE_VL6180X.h

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/******************************************************************************
2+
SFE_VL6180X.h
3+
Library for VL6180x time of flight range finder.
4+
Casey Kuhns @ SparkFun Electronics
5+
10/29/2014
6+
https://github.com/sparkfun/
7+
8+
The VL6180x by ST micro is a time of flight range finder that
9+
uses pulsed IR light to determine distances from object at close
10+
range. The average range of a sensor is between 0-200mm
11+
12+
In this file are the function prototypes in the VL6180x class
13+
14+
Resources:
15+
This library uses the Arduino Wire.h to complete I2C transactions.
16+
17+
Development environment specifics:
18+
IDE: Arduino 1.0.5
19+
Hardware Platform: Arduino Pro 3.3V/8MHz
20+
VL6180x Breakout Version: 1.0
21+
22+
Some settings and initial values come from code written by Kris Winer
23+
VL6180X_t3 Basic Example Code
24+
by: Kris Winer
25+
date: September 1, 2014
26+
license: Beerware - Use this code however you'd like. If you
27+
find it useful you can buy me a beer some time.
28+
29+
This code is beerware. If you see me (or any other SparkFun employee) at the
30+
local pub, and you've found our code helpful, please buy us a round!
31+
32+
Distributed as-is; no warranty is given.
33+
******************************************************************************/
34+
35+
#ifndef SFE_VL6180X_h
36+
#define SFE_VL6180X_h
37+
38+
#include <Arduino.h>
39+
40+
#define VL6180x_FAILURE_RESET -1
41+
42+
#define VL6180X_IDENTIFICATION_MODEL_ID 0x0000
43+
#define VL6180X_IDENTIFICATION_MODEL_REV_MAJOR 0x0001
44+
#define VL6180X_IDENTIFICATION_MODEL_REV_MINOR 0x0002
45+
#define VL6180X_IDENTIFICATION_MODULE_REV_MAJOR 0x0003
46+
#define VL6180X_IDENTIFICATION_MODULE_REV_MINOR 0x0004
47+
#define VL6180X_IDENTIFICATION_DATE 0x0006 //16bit value
48+
#define VL6180X_IDENTIFICATION_TIME 0x0008 //16bit value
49+
50+
#define VL6180X_SYSTEM_MODE_GPIO0 0x0010
51+
#define VL6180X_SYSTEM_MODE_GPIO1 0x0011
52+
#define VL6180X_SYSTEM_HISTORY_CTRL 0x0012
53+
#define VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO 0x0014
54+
#define VL6180X_SYSTEM_INTERRUPT_CLEAR 0x0015
55+
#define VL6180X_SYSTEM_FRESH_OUT_OF_RESET 0x0016
56+
#define VL6180X_SYSTEM_GROUPED_PARAMETER_HOLD 0x0017
57+
58+
#define VL6180X_SYSRANGE_START 0x0018
59+
#define VL6180X_SYSRANGE_THRESH_HIGH 0x0019
60+
#define VL6180X_SYSRANGE_THRESH_LOW 0x001A
61+
#define VL6180X_SYSRANGE_INTERMEASUREMENT_PERIOD 0x001B
62+
#define VL6180X_SYSRANGE_MAX_CONVERGENCE_TIME 0x001C
63+
#define VL6180X_SYSRANGE_CROSSTALK_COMPENSATION_RATE 0x001E
64+
#define VL6180X_SYSRANGE_CROSSTALK_VALID_HEIGHT 0x0021
65+
#define VL6180X_SYSRANGE_EARLY_CONVERGENCE_ESTIMATE 0x0022
66+
#define VL6180X_SYSRANGE_PART_TO_PART_RANGE_OFFSET 0x0024
67+
#define VL6180X_SYSRANGE_RANGE_IGNORE_VALID_HEIGHT 0x0025
68+
#define VL6180X_SYSRANGE_RANGE_IGNORE_THRESHOLD 0x0026
69+
#define VL6180X_SYSRANGE_MAX_AMBIENT_LEVEL_MULT 0x002C
70+
#define VL6180X_SYSRANGE_RANGE_CHECK_ENABLES 0x002D
71+
#define VL6180X_SYSRANGE_VHV_RECALIBRATE 0x002E
72+
#define VL6180X_SYSRANGE_VHV_REPEAT_RATE 0x0031
73+
74+
#define VL6180X_SYSALS_START 0x0038
75+
#define VL6180X_SYSALS_THRESH_HIGH 0x003A
76+
#define VL6180X_SYSALS_THRESH_LOW 0x003C
77+
#define VL6180X_SYSALS_INTERMEASUREMENT_PERIOD 0x003E
78+
#define VL6180X_SYSALS_ANALOGUE_GAIN 0x003F
79+
#define VL6180X_SYSALS_INTEGRATION_PERIOD 0x0040
80+
81+
#define VL6180X_RESULT_RANGE_STATUS 0x004D
82+
#define VL6180X_RESULT_ALS_STATUS 0x004E
83+
#define VL6180X_RESULT_INTERRUPT_STATUS_GPIO 0x004F
84+
#define VL6180X_RESULT_ALS_VAL 0x0050
85+
#define VL6180X_RESULT_HISTORY_BUFFER 0x0052
86+
#define VL6180X_RESULT_RANGE_VAL 0x0062
87+
#define VL6180X_RESULT_RANGE_RAW 0x0064
88+
#define VL6180X_RESULT_RANGE_RETURN_RATE 0x0066
89+
#define VL6180X_RESULT_RANGE_REFERENCE_RATE 0x0068
90+
#define VL6180X_RESULT_RANGE_RETURN_SIGNAL_COUNT 0x006C
91+
#define VL6180X_RESULT_RANGE_REFERENCE_SIGNAL_COUNT 0x0070
92+
#define VL6180X_RESULT_RANGE_RETURN_AMB_COUNT 0x0074
93+
#define VL6180X_RESULT_RANGE_REFERENCE_AMB_COUNT 0x0078
94+
#define VL6180X_RESULT_RANGE_RETURN_CONV_TIME 0x007C
95+
#define VL6180X_RESULT_RANGE_REFERENCE_CONV_TIME 0x0080
96+
97+
#define VL6180X_READOUT_AVERAGING_SAMPLE_PERIOD 0x010A
98+
#define VL6180X_FIRMWARE_BOOTUP 0x0119
99+
#define VL6180X_FIRMWARE_RESULT_SCALER 0x0120
100+
#define VL6180X_I2C_SLAVE_DEVICE_ADDRESS 0x0212
101+
#define VL6180X_INTERLEAVED_MODE_ENABLE 0x02A3
102+
103+
struct VL6180xIdentification
104+
{
105+
uint8_t idModel;
106+
uint8_t idModelRevMajor;
107+
uint8_t idModelRevMinor;
108+
uint8_t idModuleRevMajor;
109+
uint8_t idModuleRevMinor;
110+
uint16_t idDate;
111+
uint16_t idTime;
112+
};
113+
114+
115+
class VL6180x
116+
{
117+
public:
118+
//Initalize library with default address
119+
VL6180x(uint8_t address);
120+
//Send manditory settings as stated in ST datasheet.
121+
// http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf (Section 1.3)
122+
uint8_t VL6180xInit(void);
123+
// Use default settings from ST data sheet section 9.
124+
// http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf
125+
void VL6180xDefautSettings(void);
126+
127+
// Get Range distance in (mm)
128+
uint8_t getDistance();
129+
// Get ALS level in Lux
130+
uint8_t getAmbientLight();
131+
132+
//Load structure provided by the user with identification info
133+
//Structure example:
134+
// struct VL6180xIdentification
135+
// {
136+
// uint8_t idModel;
137+
// uint8_t idModelRevMajor;
138+
// uint8_t idModelRevMinor;
139+
// uint8_t idModuleRevMajor;
140+
// uint8_t idModuleRevMinor;
141+
// uint16_t idDate;
142+
// uint16_t idTime;
143+
// };
144+
void getIdentification(struct VL6180xIdentification *temp);
145+
146+
//Change the default address of the device to allow multiple
147+
//sensors on the bus. Can use up to 127 sensors. New address
148+
//is saved in non-volatile device memory.
149+
uint8_t changeAddress();
150+
151+
152+
private:
153+
//Store address given when the class is initialized.
154+
//This value can be changed by the changeAddress() function
155+
int _i2caddress;
156+
157+
uint8_t VL6180x_getRegister(uint16_t registerAddr);
158+
uint16_t VL6180x_getRegister16bit(uint16_t registerAddr);
159+
160+
void VL6180x_setRegister(uint16_t registerAddr, uint8_t data);
161+
void VL6180x_setRegister16bit(uint16_t registerAddr, uint16_t data);
162+
163+
164+
};
165+
166+
#endif

Libraries/README.md.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
See https://github.com/sparkfun/SparkFun_VL6180_Time_of_Flight_Library for library

0 commit comments

Comments
 (0)