44 * Casey Kuhns @ SparkFun Electronics
55 * 10/29/2014
66 * https://github.com/sparkfun/SparkFun_ToF_Range_Finder-VL6180_Arduino_Library
7- *
7+ *
88 * The VL6180x by ST micro is a time of flight range finder that
99 * uses pulsed IR light to determine distances from object at close
1010 * range. The average range of a sensor is between 0-200mm
11- *
11+ *
1212 * In this file are the functions in the VL6180x class
13- *
13+ *
1414 * Resources:
1515 * This library uses the Arduino Wire.h to complete I2C transactions.
16- *
16+ *
1717 * Development environment specifics:
1818 * IDE: Arduino 1.0.5
1919 * Hardware Platform: Arduino Pro 3.3V/8MHz
2020 * VL6180x Breakout Version: 1.0
2121 * **Updated for Arduino 1.6.4 5/2015**
22- *
22+ *
2323 * This code is beerware. If you see me (or any other SparkFun employee) at the
2424 * local pub, and you've found our code helpful, please buy us a round!
25- *
25+ *
2626 * Distributed as-is; no warranty is given.
2727 ******************************************************************************/
2828
3232VL6180x::VL6180x (uint8_t address)
3333// Initialize the Library
3434{
35- _i2caddress = address; // set default address for communication
35+ _i2caddress = address; // set default address for communication
3636}
3737
38-
39- uint8_t VL6180x::VL6180xInit ( void ) {
40- uint8_t data; // for temp data storage
38+ uint8_t VL6180x::VL6180xInit ( void )
39+ {
40+ uint8_t data; // for temp data storage
4141
4242 data = VL6180x_getRegister (VL6180X_SYSTEM_FRESH_OUT_OF_RESET );
4343
44- if (data != 1 ) return VL6180x_FAILURE_RESET;
44+ if (data != 1 )
45+ return VL6180x_FAILURE_RESET;
4546
46- // Required by datasheet
47- // http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf
47+ // Required by datasheet
48+ // http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf
4849 VL6180x_setRegister (0x0207 , 0x01 );
4950 VL6180x_setRegister (0x0208 , 0x01 );
5051 VL6180x_setRegister (0x0096 , 0x00 );
@@ -64,7 +65,7 @@ uint8_t VL6180x::VL6180xInit(void){
6465 VL6180x_setRegister (0x00b7 , 0x00 );
6566 VL6180x_setRegister (0x00bb , 0x3c );
6667 VL6180x_setRegister (0x00b2 , 0x09 );
67- VL6180x_setRegister (0x00ca , 0x09 );
68+ VL6180x_setRegister (0x00ca , 0x09 );
6869 VL6180x_setRegister (0x0198 , 0x01 );
6970 VL6180x_setRegister (0x01b0 , 0x17 );
7071 VL6180x_setRegister (0x01ad , 0x00 );
@@ -79,38 +80,39 @@ uint8_t VL6180x::VL6180xInit(void){
7980 return 0 ;
8081}
8182
82- void VL6180x::VL6180xDefautSettings (void ){
83- // Recommended settings from datasheet
84- // http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf
85-
86- // Enable Interrupts on Conversion Complete (any source)
87- VL6180x_setRegister ( VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO , ( 4 << 3 )|( 4 ) ); // Set GPIO1 high when sample complete
88-
89-
90- VL6180x_setRegister (VL6180X_SYSTEM_MODE_GPIO1 , 0x10 ); // Set GPIO1 high when sample complete
91- VL6180x_setRegister (VL6180X_READOUT_AVERAGING_SAMPLE_PERIOD , 0x30 ); // Set Avg sample period
92- VL6180x_setRegister (VL6180X_SYSALS_ANALOGUE_GAIN , 0x46 ); // Set the ALS gain
93- VL6180x_setRegister (VL6180X_SYSRANGE_VHV_REPEAT_RATE , 0xFF ); // Set auto calibration period (Max = 255)/(OFF = 0)
94- VL6180x_setRegister (VL6180X_SYSALS_INTEGRATION_PERIOD , 0x63 ); // Set ALS integration time to 100ms
95- VL6180x_setRegister (VL6180X_SYSRANGE_VHV_RECALIBRATE , 0x01 ); // perform a single temperature calibration
96- // Optional settings from datasheet
97- // http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf
83+ void VL6180x::VL6180xDefautSettings (void )
84+ {
85+ // Recommended settings from datasheet
86+ // http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf
87+
88+ // Enable Interrupts on Conversion Complete (any source)
89+ VL6180x_setRegister ( VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO , ( 4 << 3 ) | ( 4 )); // Set GPIO1 high when sample complete
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
9899 VL6180x_setRegister (VL6180X_SYSRANGE_INTERMEASUREMENT_PERIOD , 0x09 ); // Set default ranging inter-measurement period to 100ms
99- VL6180x_setRegister (VL6180X_SYSALS_INTERMEASUREMENT_PERIOD , 0x0A ); // Set default ALS inter-measurement period to 100ms
100- VL6180x_setRegister (VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO , 0x24 ); // Configures interrupt on ‘New Sample Ready threshold event’
101- // Additional settings defaults from community
100+ VL6180x_setRegister (VL6180X_SYSALS_INTERMEASUREMENT_PERIOD , 0x0A ); // Set default ALS inter-measurement period to 100ms
101+ VL6180x_setRegister (VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO , 0x24 ); // Configures interrupt on ‘New Sample Ready threshold event’
102+ // Additional settings defaults from community
102103 VL6180x_setRegister (VL6180X_SYSRANGE_MAX_CONVERGENCE_TIME , 0x32 );
103104 VL6180x_setRegister (VL6180X_SYSRANGE_RANGE_CHECK_ENABLES , 0x10 | 0x01 );
104- VL6180x_setRegister16bit (VL6180X_SYSRANGE_EARLY_CONVERGENCE_ESTIMATE , 0x7B );
105+ VL6180x_setRegister16bit (VL6180X_SYSRANGE_EARLY_CONVERGENCE_ESTIMATE , 0x7B );
105106 VL6180x_setRegister16bit (VL6180X_SYSALS_INTEGRATION_PERIOD , 0x64 );
106107
107- VL6180x_setRegister (VL6180X_READOUT_AVERAGING_SAMPLE_PERIOD ,0x30 );
108- VL6180x_setRegister (VL6180X_SYSALS_ANALOGUE_GAIN ,0x40 );
109- VL6180x_setRegister (VL6180X_FIRMWARE_RESULT_SCALER ,0x01 );
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 );
110111}
111- void VL6180x::getIdentification (struct VL6180xIdentification *temp){
112+ void VL6180x::getIdentification (struct VL6180xIdentification *temp)
113+ {
112114
113- temp->idModel = VL6180x_getRegister (VL6180X_IDENTIFICATION_MODEL_ID );
115+ temp->idModel = VL6180x_getRegister (VL6180X_IDENTIFICATION_MODEL_ID );
114116 temp->idModelRevMajor = VL6180x_getRegister (VL6180X_IDENTIFICATION_MODEL_REV_MAJOR );
115117 temp->idModelRevMinor = VL6180x_getRegister (VL6180X_IDENTIFICATION_MODEL_REV_MINOR );
116118 temp->idModuleRevMajor = VL6180x_getRegister (VL6180X_IDENTIFICATION_MODULE_REV_MAJOR );
@@ -120,25 +122,25 @@ void VL6180x::getIdentification(struct VL6180xIdentification *temp){
120122 temp->idTime = VL6180x_getRegister16bit (VL6180X_IDENTIFICATION_TIME );
121123}
122124
125+ uint8_t VL6180x::changeAddress (uint8_t old_address, uint8_t new_address)
126+ {
123127
124- uint8_t VL6180x::changeAddress (uint8_t old_address, uint8_t new_address){
125-
126- // NOTICE: IT APPEARS THAT CHANGING THE ADDRESS IS NOT STORED IN NON-VOLATILE MEMORY
127- // POWER CYCLING THE DEVICE REVERTS ADDRESS BACK TO 0X29
128-
129- if ( old_address == new_address) return old_address;
130- if ( new_address > 127 ) return old_address;
131-
132- VL6180x_setRegister (VL6180X_I2C_SLAVE_DEVICE_ADDRESS , new_address);
133- _i2caddress = new_address;
134- return VL6180x_getRegister (VL6180X_I2C_SLAVE_DEVICE_ADDRESS );
135- }
136-
128+ // NOTICE: IT APPEARS THAT CHANGING THE ADDRESS IS NOT STORED IN NON-VOLATILE MEMORY
129+ // POWER CYCLING THE DEVICE REVERTS ADDRESS BACK TO 0X29
137130
131+ if (old_address == new_address)
132+ return old_address;
133+ if (new_address > 127 )
134+ return old_address;
135+
136+ VL6180x_setRegister (VL6180X_I2C_SLAVE_DEVICE_ADDRESS , new_address);
137+ _i2caddress = new_address;
138+ return VL6180x_getRegister (VL6180X_I2C_SLAVE_DEVICE_ADDRESS );
139+ }
138140
139141uint8_t VL6180x::getDistance ()
140142{
141- VL6180x_setRegister (VL6180X_SYSRANGE_START , 0x01 ); // Start Single shot mode
143+ VL6180x_setRegister (VL6180X_SYSRANGE_START , 0x01 ); // Start Single shot mode
142144 delay (10 );
143145 VL6180x_setRegister (VL6180X_SYSTEM_INTERRUPT_CLEAR , 0x07 );
144146 return VL6180x_getRegister (VL6180X_RESULT_RANGE_VAL );
@@ -147,42 +149,59 @@ uint8_t VL6180x::getDistance()
147149
148150float VL6180x::getAmbientLight (vl6180x_als_gain VL6180X_ALS_GAIN )
149151{
150- // First load in Gain we are using, do it everytime incase someone changes it on us.
151- // Note: Upper nibble shoudl be set to 0x4 i.e. for ALS gain of 1.0 write 0x46
152+ // First load in Gain we are using, do it everytime incase someone changes it on us.
153+ // Note: Upper nibble shoudl be set to 0x4 i.e. for ALS gain of 1.0 write 0x46
152154 VL6180x_setRegister (VL6180X_SYSALS_ANALOGUE_GAIN , (0x40 | VL6180X_ALS_GAIN )); // Set the ALS gain
153155
154- // Start ALS Measurement
156+ // Start ALS Measurement
155157 VL6180x_setRegister (VL6180X_SYSALS_START , 0x01 );
156158
157- delay (100 ); // give it time...
159+ delay (100 ); // give it time...
158160
159161 VL6180x_setRegister (VL6180X_SYSTEM_INTERRUPT_CLEAR , 0x07 );
160162
161- // Retrieve the Raw ALS value from the sensoe
163+ // Retrieve the Raw ALS value from the sensoe
162164 unsigned int alsRaw = VL6180x_getRegister16bit (VL6180X_RESULT_ALS_VAL );
163-
164- // Get Integration Period for calculation, we do this everytime incase someone changes it on us.
165+
166+ // Get Integration Period for calculation, we do this everytime incase someone changes it on us.
165167 unsigned int alsIntegrationPeriodRaw = VL6180x_getRegister16bit (VL6180X_SYSALS_INTEGRATION_PERIOD );
166-
167- float alsIntegrationPeriod = 100.0 / alsIntegrationPeriodRaw ;
168168
169- // Calculate actual LUX from Appnotes
169+ float alsIntegrationPeriod = 100.0 / alsIntegrationPeriodRaw;
170+
171+ // Calculate actual LUX from Appnotes
170172
171173 float alsGain = 0.0 ;
172-
173- switch (VL6180X_ALS_GAIN ){
174- case GAIN_20 : alsGain = 20.0 ; break ;
175- case GAIN_10 : alsGain = 10.32 ; break ;
176- case GAIN_5 : alsGain = 5.21 ; break ;
177- case GAIN_2_5 : alsGain = 2.60 ; break ;
178- case GAIN_1_67 : alsGain = 1.72 ; break ;
179- case GAIN_1_25 : alsGain = 1.28 ; break ;
180- case GAIN_1 : alsGain = 1.01 ; break ;
181- case GAIN_40 : alsGain = 40.0 ; break ;
174+
175+ switch (VL6180X_ALS_GAIN )
176+ {
177+ case GAIN_20 :
178+ alsGain = 20.0 ;
179+ break ;
180+ case GAIN_10 :
181+ alsGain = 10.32 ;
182+ break ;
183+ case GAIN_5 :
184+ alsGain = 5.21 ;
185+ break ;
186+ case GAIN_2_5 :
187+ alsGain = 2.60 ;
188+ break ;
189+ case GAIN_1_67 :
190+ alsGain = 1.72 ;
191+ break ;
192+ case GAIN_1_25 :
193+ alsGain = 1.28 ;
194+ break ;
195+ case GAIN_1 :
196+ alsGain = 1.01 ;
197+ break ;
198+ case GAIN_40 :
199+ alsGain = 40.0 ;
200+ break ;
182201 }
183202
184- // Calculate LUX from formula in AppNotes
185-
203+ // Calculate LUX from formula in AppNotes
204+
186205 float alsCalculated = (float )0.32 * ((float )alsRaw / alsGain) * alsIntegrationPeriod;
187206
188207 return alsCalculated;
@@ -194,12 +213,12 @@ uint8_t VL6180x::VL6180x_getRegister(uint16_t registerAddr)
194213{
195214 uint8_t data;
196215
197- Wire.beginTransmission ( _i2caddress ); // Address set on class instantiation
198- Wire.write ((registerAddr >> 8 ) & 0xFF ); // MSB of register address
199- Wire.write (registerAddr & 0xFF ); // LSB of register address
200- Wire.endTransmission (false ); // Send address and register address bytes
201- Wire.requestFrom ( _i2caddress , 1 );
202- data = Wire.read (); // Read Data from selected register
216+ Wire.beginTransmission (_i2caddress); // Address set on class instantiation
217+ Wire.write ((registerAddr >> 8 ) & 0xFF ); // MSB of register address
218+ Wire.write (registerAddr & 0xFF ); // LSB of register address
219+ Wire.endTransmission (false ); // Send address and register address bytes
220+ Wire.requestFrom (_i2caddress, 1 );
221+ data = Wire.read (); // Read Data from selected register
203222
204223 return data;
205224}
@@ -210,40 +229,37 @@ uint16_t VL6180x::VL6180x_getRegister16bit(uint16_t registerAddr)
210229 uint8_t data_high;
211230 uint16_t data;
212231
213- Wire.beginTransmission ( _i2caddress ); // Address set on class instantiation
214- Wire.write ((registerAddr >> 8 ) & 0xFF ); // MSB of register address
215- Wire.write (registerAddr & 0xFF ); // LSB of register address
216- Wire.endTransmission (false ); // Send address and register address bytes
232+ Wire.beginTransmission (_i2caddress); // Address set on class instantiation
233+ Wire.write ((registerAddr >> 8 ) & 0xFF ); // MSB of register address
234+ Wire.write (registerAddr & 0xFF ); // LSB of register address
235+ Wire.endTransmission (false ); // Send address and register address bytes
217236
218- Wire.requestFrom ( _i2caddress, 2 );
219- data_high = Wire.read (); // Read Data from selected register
220- data_low = Wire.read (); // Read Data from selected register
221- data = (data_high << 8 )| data_low;
237+ Wire.requestFrom (_i2caddress, 2 );
238+ data_high = Wire.read (); // Read Data from selected register
239+ data_low = Wire.read (); // Read Data from selected register
240+ data = (data_high << 8 ) | data_low;
222241
223242 return data;
224243}
225244
226245void VL6180x::VL6180x_setRegister (uint16_t registerAddr, uint8_t data)
227246{
228- Wire.beginTransmission ( _i2caddress ); // Address set on class instantiation
229- Wire.write ((registerAddr >> 8 ) & 0xFF ); // MSB of register address
230- Wire.write (registerAddr & 0xFF ); // LSB of register address
231- Wire.write (data); // Data/setting to be sent to device.
232- Wire.endTransmission (); // Send address and register address bytes
247+ Wire.beginTransmission (_i2caddress); // Address set on class instantiation
248+ Wire.write ((registerAddr >> 8 ) & 0xFF ); // MSB of register address
249+ Wire.write (registerAddr & 0xFF ); // LSB of register address
250+ Wire.write (data); // Data/setting to be sent to device.
251+ Wire.endTransmission (); // Send address and register address bytes
233252}
234253
235254void VL6180x::VL6180x_setRegister16bit (uint16_t registerAddr, uint16_t data)
236255{
237- Wire.beginTransmission ( _i2caddress ); // Address set on class instantiation
238- Wire.write ((registerAddr >> 8 ) & 0xFF ); // MSB of register address
239- Wire.write (registerAddr & 0xFF ); // LSB of register address
256+ Wire.beginTransmission (_i2caddress); // Address set on class instantiation
257+ Wire.write ((registerAddr >> 8 ) & 0xFF ); // MSB of register address
258+ Wire.write (registerAddr & 0xFF ); // LSB of register address
240259 uint8_t temp;
241260 temp = (data >> 8 ) & 0xff ;
242261 Wire.write (temp); // Data/setting to be sent to device
243262 temp = data & 0xff ;
244- Wire.write (temp); // Data/setting to be sent to device
245- Wire.endTransmission (); // Send address and register address bytes
263+ Wire.write (temp); // Data/setting to be sent to device
264+ Wire.endTransmission (); // Send address and register address bytes
246265}
247-
248-
249-
0 commit comments