├── .gitattributes ├── .github └── workflows │ └── compile-sketch.yml ├── .gitignore ├── LICENSE.md ├── README.md ├── examples └── SparkFun_VL6180X_demo │ └── SparkFun_VL6180X_demo.ino ├── keywords.txt ├── library.properties └── src ├── SparkFun_VL6180X.cpp └── SparkFun_VL6180X.h /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.github/workflows/compile-sketch.yml: -------------------------------------------------------------------------------- 1 | name: Compile Sketch 2 | 3 | on: 4 | - push 5 | - pull_request 6 | 7 | jobs: 8 | compile-sketch: 9 | runs-on: ubuntu-latest 10 | 11 | steps: 12 | - name: Checkout repository 13 | uses: actions/checkout@v2 14 | 15 | - name: Compile sketch 16 | uses: arduino/compile-sketches@v1 17 | with: 18 | fqbn: 'sparkfun:apollo3:redboard_artemis' 19 | sketch-paths: | 20 | - ./ 21 | platforms: | 22 | - source-url: "https://github.com/sparkfun/Arduino_Apollo3/releases/download/v2.0.6/Arduino_Apollo3.tar.gz" 23 | - name: "sparkfun:apollo3" 24 | libraries: | 25 | - source-url: "https://github.com/sparkfun/SparkFun_ToF_Range_Finder-VL6180_Arduino_Library.git" -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## SparkFun Useful stuff 3 | ################# 4 | 5 | ## AVR Development 6 | *.eep 7 | *.elf 8 | *.lst 9 | *.lss 10 | *.sym 11 | *.d 12 | *.o 13 | *.srec 14 | *.map 15 | 16 | ## Notepad++ backup files 17 | *.bak 18 | 19 | ## BOM files 20 | *bom* 21 | 22 | ################# 23 | ## Eclipse 24 | ################# 25 | 26 | *.pydevproject 27 | .project 28 | .metadata 29 | bin/ 30 | tmp/ 31 | *.tmp 32 | *.bak 33 | *.swp 34 | *~.nib 35 | local.properties 36 | .classpath 37 | .settings/ 38 | .loadpath 39 | 40 | # External tool builders 41 | .externalToolBuilders/ 42 | 43 | # Locally stored "Eclipse launch configurations" 44 | *.launch 45 | 46 | # CDT-specific 47 | .cproject 48 | 49 | # PDT-specific 50 | .buildpath 51 | 52 | 53 | ############# 54 | ## Eagle 55 | ############# 56 | 57 | # Ignore the board and schematic backup files 58 | *.b#? 59 | *.s#? 60 | 61 | 62 | ################# 63 | ## Visual Studio 64 | ################# 65 | 66 | ## Ignore Visual Studio temporary files, build results, and 67 | ## files generated by popular Visual Studio add-ons. 68 | 69 | # User-specific files 70 | *.suo 71 | *.user 72 | *.sln.docstates 73 | 74 | # Build results 75 | [Dd]ebug/ 76 | [Rr]elease/ 77 | *_i.c 78 | *_p.c 79 | *.ilk 80 | *.meta 81 | *.obj 82 | *.pch 83 | *.pdb 84 | *.pgc 85 | *.pgd 86 | *.rsp 87 | *.sbr 88 | *.tlb 89 | *.tli 90 | *.tlh 91 | *.tmp 92 | *.vspscc 93 | .builds 94 | *.dotCover 95 | 96 | ## TODO: If you have NuGet Package Restore enabled, uncomment this 97 | #packages/ 98 | 99 | # Visual C++ cache files 100 | ipch/ 101 | *.aps 102 | *.ncb 103 | *.opensdf 104 | *.sdf 105 | 106 | # Visual Studio profiler 107 | *.psess 108 | *.vsp 109 | 110 | # ReSharper is a .NET coding add-in 111 | _ReSharper* 112 | 113 | # Installshield output folder 114 | [Ee]xpress 115 | 116 | # DocProject is a documentation generator add-in 117 | DocProject/buildhelp/ 118 | DocProject/Help/*.HxT 119 | DocProject/Help/*.HxC 120 | DocProject/Help/*.hhc 121 | DocProject/Help/*.hhk 122 | DocProject/Help/*.hhp 123 | DocProject/Help/Html2 124 | DocProject/Help/html 125 | 126 | # Click-Once directory 127 | publish 128 | 129 | # Others 130 | [Bb]in 131 | [Oo]bj 132 | sql 133 | TestResults 134 | *.Cache 135 | ClientBin 136 | stylecop.* 137 | ~$* 138 | *.dbmdl 139 | Generated_Code #added for RIA/Silverlight projects 140 | 141 | # Backup & report files from converting an old project file to a newer 142 | # Visual Studio version. Backup files are not needed, because we have git ;-) 143 | _UpgradeReport_Files/ 144 | Backup*/ 145 | UpgradeLog*.XML 146 | 147 | 148 | ############ 149 | ## Windows 150 | ############ 151 | 152 | # Windows image file caches 153 | Thumbs.db 154 | 155 | # Folder config file 156 | Desktop.ini 157 | 158 | 159 | ############# 160 | ## Python 161 | ############# 162 | 163 | *.py[co] 164 | 165 | # Packages 166 | *.egg 167 | *.egg-info 168 | dist 169 | build 170 | eggs 171 | parts 172 | bin 173 | var 174 | sdist 175 | develop-eggs 176 | .installed.cfg 177 | 178 | # Installer logs 179 | pip-log.txt 180 | 181 | # Unit test / coverage reports 182 | .coverage 183 | .tox 184 | 185 | #Translations 186 | *.mo 187 | 188 | #Mr Developer 189 | .mr.developer.cfg 190 | 191 | # Mac crap 192 | .DS_Store 193 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | 2 | License Information 3 | ------------------- 4 | 5 | The hardware is released under [Creative Commons Share-alike 3.0](http://creativecommons.org/licenses/by-sa/3.0/). 6 | 7 | All other code is open source so please feel free to do anything you want with it; you buy me a beer if you use this and we meet someday ([Beerware license](http://en.wikipedia.org/wiki/Beerware)). 8 | 9 | 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | SparkFun ToF Range Finder - VL6180 Arduino Library 2 | ======================================== 3 | 4 | [![SparkFun ToF Range Finder Sensor - VL6180](https://cdn.sparkfun.com//assets/parts/9/5/9/7/12785-01.jpg)](https://www.sparkfun.com/products/12785) 5 | 6 | [*SparkFun ToF Range Finder Sensor - VL6180 (SEN-12785)*](https://www.sparkfun.com/products/12785) 7 | 8 | Arduino library with basic functionality of the VL6180 sensor. 9 | 10 | [![Compile Sketch](https://github.com/sparkfun/SparkFun_ToF_Range_Finder-VL6180_Arduino_Library/actions/workflows/compile-sketch.yml/badge.svg)](https://github.com/sparkfun/SparkFun_ToF_Range_Finder-VL6180_Arduino_Library/actions/workflows/compile-sketch.yml) 11 | 12 | Repository Contents 13 | ------------------- 14 | 15 | * **/examples** - Example sketches for the library (.ino). Run these from the Arduino IDE. 16 | * **/src** - Source files for the library (.cpp, .h). 17 | * **keywords.txt** - Keywords from this library that will be highlighted in the Arduino IDE. 18 | * **library.properties** - General library properties for the Arduino package manager. 19 | 20 | Documentation 21 | -------------- 22 | 23 | * **[Installing an Arduino Library Guide](https://learn.sparkfun.com/tutorials/installing-an-arduino-library)** - Basic information on how to install an Arduino library. 24 | * **[Breakout Repository](https://github.com/sparkfun/ToF_Range_Finder_Breakout-VL6180/tree/master)** - Main repository (including hardware files) for the breakout. 25 | * **[Sensor Repository](https://github.com/sparkfun/ToF_Range_Finder_Sensor-VL6180/tree/master)** - Main repository (including hardware files) for the sensor board. 26 | * **[Hookup Guide](https://learn.sparkfun.com/tutorials/vl6180-hookup-guide)** - Basic hookup guide for the VL6180. 27 | 28 | Products that use this Library 29 | --------------------------------- 30 | 31 | * [SEN-12785](https://www.sparkfun.com/products/12785)- Sensor Board 32 | * [SEN-12784](https://www.sparkfun.com/products/12784)- Breakout board 33 | 34 | 35 | License Information 36 | ------------------- 37 | 38 | This product is _**open source**_! 39 | 40 | The **code** 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! 41 | 42 | 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. 43 | 44 | Distributed as-is; no warranty is given. 45 | 46 | - Your friends at SparkFun. 47 | 48 | -------------------------------------------------------------------------------- /examples/SparkFun_VL6180X_demo/SparkFun_VL6180X_demo.ino: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * SparkFun_VL6180X_demo.ino 3 | * Example Sketch for VL6180x time of flight range finder. 4 | * Casey Kuhns @ SparkFun Electronics 5 | * 10/29/2014 6 | * https://github.com/sparkfun/SparkFun_ToF_Range_Finder-VL6180_Arduino_Library 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 | * Resources: 13 | * This library uses the Arduino Wire.h to complete I2C transactions. 14 | * 15 | * Development environment specifics: 16 | * IDE: Arduino 1.0.5 17 | * Hardware Platform: Arduino Pro 3.3V/8MHz 18 | * VL6180x Breakout Version: 1.0 19 | * **Updated for Arduino 1.6.4 5/2015** 20 | 21 | * 22 | * This code is beerware. If you see me (or any other SparkFun employee) at the 23 | * local pub, and you've found our code helpful, please buy us a round! 24 | * 25 | * Distributed as-is; no warranty is given. 26 | ******************************************************************************/ 27 | 28 | #include 29 | 30 | #include 31 | 32 | /*const float GAIN_1 = 1.01; // Actual ALS Gain of 1.01 33 | const float GAIN_1_25 = 1.28; // Actual ALS Gain of 1.28 34 | const float GAIN_1_67 = 1.72; // Actual ALS Gain of 1.72 35 | const float GAIN_2_5 = 2.6; // Actual ALS Gain of 2.60 36 | const float GAIN_5 = 5.21; // Actual ALS Gain of 5.21 37 | const float GAIN_10 = 10.32; // Actual ALS Gain of 10.32 38 | const float GAIN_20 = 20; // Actual ALS Gain of 20 39 | const float GAIN_40 = 40; // Actual ALS Gain of 40 40 | */ 41 | #define VL6180X_ADDRESS 0x29 42 | 43 | VL6180xIdentification identification; 44 | VL6180x sensor(VL6180X_ADDRESS); 45 | 46 | void setup() 47 | { 48 | 49 | Serial.begin(115200); // Start Serial at 115200bps 50 | Wire.begin(); // Start I2C library 51 | delay(100); // delay .1s 52 | 53 | sensor.getIdentification(&identification); // Retrieve manufacture info from device memory 54 | printIdentification(&identification); // Helper function to print all the Module information 55 | 56 | if (sensor.VL6180xInit() != 0) 57 | { 58 | Serial.println("Failed to initialize. Freezing..."); // Initialize device and check for errors 59 | while (1) 60 | ; 61 | } 62 | 63 | sensor.VL6180xDefautSettings(); // Load default settings to get started. 64 | 65 | delay(1000); // delay 1s 66 | } 67 | 68 | void loop() 69 | { 70 | 71 | // Get Ambient Light level and report in LUX 72 | Serial.print("Ambient Light Level (Lux) = "); 73 | 74 | // Input GAIN for light levels, 75 | // GAIN_20 // Actual ALS Gain of 20 76 | // GAIN_10 // Actual ALS Gain of 10.32 77 | // GAIN_5 // Actual ALS Gain of 5.21 78 | // GAIN_2_5 // Actual ALS Gain of 2.60 79 | // GAIN_1_67 // Actual ALS Gain of 1.72 80 | // GAIN_1_25 // Actual ALS Gain of 1.28 81 | // GAIN_1 // Actual ALS Gain of 1.01 82 | // GAIN_40 // Actual ALS Gain of 40 83 | 84 | Serial.println(sensor.getAmbientLight(GAIN_1)); 85 | 86 | // Get Distance and report in mm 87 | Serial.print("Distance measured (mm) = "); 88 | Serial.println(sensor.getDistance()); 89 | 90 | delay(500); 91 | }; 92 | 93 | void printIdentification(struct VL6180xIdentification *temp) 94 | { 95 | Serial.print("Model ID = "); 96 | Serial.println(temp->idModel); 97 | 98 | Serial.print("Model Rev = "); 99 | Serial.print(temp->idModelRevMajor); 100 | Serial.print("."); 101 | Serial.println(temp->idModelRevMinor); 102 | 103 | Serial.print("Module Rev = "); 104 | Serial.print(temp->idModuleRevMajor); 105 | Serial.print("."); 106 | Serial.println(temp->idModuleRevMinor); 107 | 108 | Serial.print("Manufacture Date = "); 109 | Serial.print((temp->idDate >> 3) & 0x001F); 110 | Serial.print("/"); 111 | Serial.print((temp->idDate >> 8) & 0x000F); 112 | Serial.print("/1"); 113 | Serial.print((temp->idDate >> 12) & 0x000F); 114 | Serial.print(" Phase: "); 115 | Serial.println(temp->idDate & 0x0007); 116 | 117 | Serial.print("Manufacture Time (s)= "); 118 | Serial.println(temp->idTime * 2); 119 | Serial.println(); 120 | Serial.println(); 121 | } 122 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Datatypes (KEYWORD1) 3 | ####################################### 4 | 5 | VL6180x KEYWORD1 6 | 7 | ####################################### 8 | # Methods and Functions (KEYWORD2) 9 | ####################################### 10 | 11 | VL6180xInit KEYWORD2 12 | VL6180xDefautSettings KEYWORD2 13 | getDistance KEYWORD2 14 | getAmbientLight KEYWORD2 15 | getIdentification KEYWORD2 16 | changeAddress KEYWORD2 17 | 18 | ####################################### 19 | # Constants (LITERAL1) 20 | ####################################### 21 | 22 | 23 | VL6180X_IDENTIFICATION_MODEL_ID KEYWORD3 24 | VL6180X_IDENTIFICATION_MODEL_REV_MAJOR KEYWORD3 25 | VL6180X_IDENTIFICATION_MODEL_REV_MINOR KEYWORD3 26 | VL6180X_IDENTIFICATION_MODULE_REV_MAJOR KEYWORD3 27 | VL6180X_IDENTIFICATION_MODULE_REV_MINOR KEYWORD3 28 | VL6180X_IDENTIFICATION_DATE KEYWORD3 29 | VL6180X_IDENTIFICATION_TIME KEYWORD3 30 | 31 | VL6180X_SYSTEM_MODE_GPIO0 KEYWORD3 32 | VL6180X_SYSTEM_MODE_GPIO1 KEYWORD3 33 | VL6180X_SYSTEM_HISTORY_CTRL KEYWORD3 34 | VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO KEYWORD3 35 | VL6180X_SYSTEM_INTERRUPT_CLEAR KEYWORD3 36 | VL6180X_SYSTEM_FRESH_OUT_OF_RESET KEYWORD3 37 | VL6180X_SYSTEM_GROUPED_PARAMETER_HOLD KEYWORD3 38 | 39 | VL6180X_SYSRANGE_START KEYWORD3 40 | VL6180X_SYSRANGE_THRESH_HIGH KEYWORD3 41 | VL6180X_SYSRANGE_THRESH_LOW KEYWORD3 42 | VL6180X_SYSRANGE_INTERMEASUREMENT_PERIOD KEYWORD3 43 | VL6180X_SYSRANGE_MAX_CONVERGENCE_TIME KEYWORD3 44 | VL6180X_SYSRANGE_CROSSTALK_COMPENSATION_RATE KEYWORD3 45 | VL6180X_SYSRANGE_CROSSTALK_VALID_HEIGHT KEYWORD3 46 | VL6180X_SYSRANGE_EARLY_CONVERGENCE_ESTIMATE KEYWORD3 47 | VL6180X_SYSRANGE_PART_TO_PART_RANGE_OFFSET KEYWORD3 48 | VL6180X_SYSRANGE_RANGE_IGNORE_VALID_HEIGHT KEYWORD3 49 | VL6180X_SYSRANGE_RANGE_IGNORE_THRESHOLD KEYWORD3 50 | VL6180X_SYSRANGE_MAX_AMBIENT_LEVEL_MULT KEYWORD3 51 | VL6180X_SYSRANGE_RANGE_CHECK_ENABLES KEYWORD3 52 | VL6180X_SYSRANGE_VHV_RECALIBRATE KEYWORD3 53 | VL6180X_SYSRANGE_VHV_REPEAT_RATE KEYWORD3 54 | 55 | VL6180X_SYSALS_START KEYWORD3 56 | VL6180X_SYSALS_THRESH_HIGH KEYWORD3 57 | VL6180X_SYSALS_THRESH_LOW KEYWORD3 58 | VL6180X_SYSALS_INTERMEASUREMENT_PERIOD KEYWORD3 59 | VL6180X_SYSALS_ANALOGUE_GAIN KEYWORD3 60 | VL6180X_SYSALS_INTEGRATION_PERIOD KEYWORD3 61 | 62 | VL6180X_RESULT_RANGE_STATUS KEYWORD3 63 | VL6180X_RESULT_ALS_STATUS KEYWORD3 64 | VL6180X_RESULT_INTERRUPT_STATUS_GPIO KEYWORD3 65 | VL6180X_RESULT_ALS_VAL KEYWORD3 66 | VL6180X_RESULT_HISTORY_BUFFER KEYWORD3 67 | VL6180X_RESULT_RANGE_VAL KEYWORD3 68 | VL6180X_RESULT_RANGE_RAW KEYWORD3 69 | VL6180X_RESULT_RANGE_RETURN_RATE KEYWORD3 70 | VL6180X_RESULT_RANGE_REFERENCE_RATE KEYWORD3 71 | VL6180X_RESULT_RANGE_RETURN_SIGNAL_COUNT KEYWORD3 72 | VL6180X_RESULT_RANGE_REFERENCE_SIGNAL_COUNT KEYWORD3 73 | VL6180X_RESULT_RANGE_RETURN_AMB_COUNT KEYWORD3 74 | VL6180X_RESULT_RANGE_REFERENCE_AMB_COUNT KEYWORD3 75 | VL6180X_RESULT_RANGE_RETURN_CONV_TIME KEYWORD3 76 | VL6180X_RESULT_RANGE_REFERENCE_CONV_TIME KEYWORD3 77 | 78 | VL6180X_READOUT_AVERAGING_SAMPLE_PERIOD KEYWORD3 79 | VL6180X_FIRMWARE_BOOTUP KEYWORD3 80 | VL6180X_FIRMWARE_RESULT_SCALER KEYWORD3 81 | VL6180X_I2C_SLAVE_DEVICE_ADDRESS KEYWORD3 82 | VL6180X_INTERLEAVED_MODE_ENABLE KEYWORD3 83 | 84 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=SparkFun VL6180 Sensor 2 | version=1.1.3 3 | author=SparkFun Electronics 4 | maintainer=SparkFun Electronics 5 | sentence=The VL6180 combines an IR emitter, a range sensor, and an ambient light sensor together for you to easily use and communicate with via an I2C interface. 6 | paragraph=Library allows you to read distance and light outputs from the sensor, outputting the data via a serial connection. 7 | category=Sensors 8 | url=https://github.com/sparkfun/SparkFun_ToF_Range_Finder-VL6180_Arduino_Library 9 | architectures=* -------------------------------------------------------------------------------- /src/SparkFun_VL6180X.cpp: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * SparkFun_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/SparkFun_ToF_Range_Finder-VL6180_Arduino_Library 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 | * **Updated for Arduino 1.6.4 5/2015** 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 30 | #include "SparkFun_VL6180X.h" 31 | 32 | VL6180x::VL6180x(uint8_t address) 33 | // Initialize the Library 34 | { 35 | _i2caddress = address; // set default address for communication 36 | } 37 | 38 | uint8_t VL6180x::VL6180xInit(void) 39 | { 40 | uint8_t data; // for temp data storage 41 | 42 | data = VL6180x_getRegister(VL6180X_SYSTEM_FRESH_OUT_OF_RESET); 43 | 44 | if (data != 1) 45 | 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 | { 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 99 | VL6180x_setRegister(VL6180X_SYSRANGE_INTERMEASUREMENT_PERIOD, 0x09); // Set default ranging inter-measurement period to 100ms 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 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, 0x64); 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 | 115 | temp->idModel = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODEL_ID); 116 | temp->idModelRevMajor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODEL_REV_MAJOR); 117 | temp->idModelRevMinor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODEL_REV_MINOR); 118 | temp->idModuleRevMajor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODULE_REV_MAJOR); 119 | temp->idModuleRevMinor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODULE_REV_MINOR); 120 | 121 | temp->idDate = VL6180x_getRegister16bit(VL6180X_IDENTIFICATION_DATE); 122 | temp->idTime = VL6180x_getRegister16bit(VL6180X_IDENTIFICATION_TIME); 123 | } 124 | 125 | uint8_t VL6180x::changeAddress(uint8_t old_address, uint8_t new_address) 126 | { 127 | 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 130 | 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 | } 140 | 141 | uint8_t VL6180x::getDistance() 142 | { 143 | VL6180x_setRegister(VL6180X_SYSRANGE_START, 0x01); // Start Single shot mode 144 | delay(10); 145 | VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07); 146 | return VL6180x_getRegister(VL6180X_RESULT_RANGE_VAL); 147 | // return distance; 148 | } 149 | 150 | float VL6180x::getAmbientLight(vl6180x_als_gain VL6180X_ALS_GAIN) 151 | { 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 154 | VL6180x_setRegister(VL6180X_SYSALS_ANALOGUE_GAIN, (0x40 | VL6180X_ALS_GAIN)); // Set the ALS gain 155 | 156 | // Start ALS Measurement 157 | VL6180x_setRegister(VL6180X_SYSALS_START, 0x01); 158 | 159 | delay(100); // give it time... 160 | 161 | VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07); 162 | 163 | // Retrieve the Raw ALS value from the sensoe 164 | unsigned int alsRaw = VL6180x_getRegister16bit(VL6180X_RESULT_ALS_VAL); 165 | 166 | // Get Integration Period for calculation, we do this everytime incase someone changes it on us. 167 | unsigned int alsIntegrationPeriodRaw = VL6180x_getRegister16bit(VL6180X_SYSALS_INTEGRATION_PERIOD); 168 | 169 | float alsIntegrationPeriod = 100.0 / alsIntegrationPeriodRaw; 170 | 171 | // Calculate actual LUX from Appnotes 172 | 173 | float alsGain = 0.0; 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; 201 | } 202 | 203 | // Calculate LUX from formula in AppNotes 204 | 205 | float alsCalculated = (float)0.32 * ((float)alsRaw / alsGain) * alsIntegrationPeriod; 206 | 207 | return alsCalculated; 208 | } 209 | 210 | // --- Private Functions --- // 211 | 212 | uint8_t VL6180x::VL6180x_getRegister(uint16_t registerAddr) 213 | { 214 | uint8_t data; 215 | 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 222 | 223 | return data; 224 | } 225 | 226 | uint16_t VL6180x::VL6180x_getRegister16bit(uint16_t registerAddr) 227 | { 228 | uint8_t data_low; 229 | uint8_t data_high; 230 | uint16_t data; 231 | 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 236 | 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; 241 | 242 | return data; 243 | } 244 | 245 | void VL6180x::VL6180x_setRegister(uint16_t registerAddr, uint8_t data) 246 | { 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 252 | } 253 | 254 | void VL6180x::VL6180x_setRegister16bit(uint16_t registerAddr, uint16_t data) 255 | { 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 259 | uint8_t temp; 260 | temp = (data >> 8) & 0xff; 261 | Wire.write(temp); // Data/setting to be sent to device 262 | temp = data & 0xff; 263 | Wire.write(temp); // Data/setting to be sent to device 264 | Wire.endTransmission(); // Send address and register address bytes 265 | } 266 | -------------------------------------------------------------------------------- /src/SparkFun_VL6180X.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * SparkFun_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/SparkFun_ToF_Range_Finder-VL6180_Arduino_Library 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 | **Updated for Arduino 1.6.4 5/2015** 23 | * 24 | * Some settings and initial values come from code written by Kris Winer 25 | * VL6180X_t3 Basic Example Code 26 | * by: Kris Winer 27 | * date: September 1, 2014 28 | * license: Beerware - Use this code however you'd like. If you 29 | * find it useful you can buy me a beer some time. 30 | * 31 | * This code is beerware. If you see me (or any other SparkFun employee) at the 32 | * local pub, and you've found our code helpful, please buy us a round! 33 | * 34 | * Distributed as-is; no warranty is given. 35 | ******************************************************************************/ 36 | 37 | #ifndef SparkFun_VL6180X_h 38 | #define SparkFun_VL6180X_h 39 | 40 | #include 41 | 42 | #define VL6180x_FAILURE_RESET -1 43 | 44 | #define VL6180X_IDENTIFICATION_MODEL_ID 0x0000 45 | #define VL6180X_IDENTIFICATION_MODEL_REV_MAJOR 0x0001 46 | #define VL6180X_IDENTIFICATION_MODEL_REV_MINOR 0x0002 47 | #define VL6180X_IDENTIFICATION_MODULE_REV_MAJOR 0x0003 48 | #define VL6180X_IDENTIFICATION_MODULE_REV_MINOR 0x0004 49 | #define VL6180X_IDENTIFICATION_DATE 0x0006 // 16bit value 50 | #define VL6180X_IDENTIFICATION_TIME 0x0008 // 16bit value 51 | 52 | #define VL6180X_SYSTEM_MODE_GPIO0 0x0010 53 | #define VL6180X_SYSTEM_MODE_GPIO1 0x0011 54 | #define VL6180X_SYSTEM_HISTORY_CTRL 0x0012 55 | #define VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO 0x0014 56 | #define VL6180X_SYSTEM_INTERRUPT_CLEAR 0x0015 57 | #define VL6180X_SYSTEM_FRESH_OUT_OF_RESET 0x0016 58 | #define VL6180X_SYSTEM_GROUPED_PARAMETER_HOLD 0x0017 59 | 60 | #define VL6180X_SYSRANGE_START 0x0018 61 | #define VL6180X_SYSRANGE_THRESH_HIGH 0x0019 62 | #define VL6180X_SYSRANGE_THRESH_LOW 0x001A 63 | #define VL6180X_SYSRANGE_INTERMEASUREMENT_PERIOD 0x001B 64 | #define VL6180X_SYSRANGE_MAX_CONVERGENCE_TIME 0x001C 65 | #define VL6180X_SYSRANGE_CROSSTALK_COMPENSATION_RATE 0x001E 66 | #define VL6180X_SYSRANGE_CROSSTALK_VALID_HEIGHT 0x0021 67 | #define VL6180X_SYSRANGE_EARLY_CONVERGENCE_ESTIMATE 0x0022 68 | #define VL6180X_SYSRANGE_PART_TO_PART_RANGE_OFFSET 0x0024 69 | #define VL6180X_SYSRANGE_RANGE_IGNORE_VALID_HEIGHT 0x0025 70 | #define VL6180X_SYSRANGE_RANGE_IGNORE_THRESHOLD 0x0026 71 | #define VL6180X_SYSRANGE_MAX_AMBIENT_LEVEL_MULT 0x002C 72 | #define VL6180X_SYSRANGE_RANGE_CHECK_ENABLES 0x002D 73 | #define VL6180X_SYSRANGE_VHV_RECALIBRATE 0x002E 74 | #define VL6180X_SYSRANGE_VHV_REPEAT_RATE 0x0031 75 | 76 | #define VL6180X_SYSALS_START 0x0038 77 | #define VL6180X_SYSALS_THRESH_HIGH 0x003A 78 | #define VL6180X_SYSALS_THRESH_LOW 0x003C 79 | #define VL6180X_SYSALS_INTERMEASUREMENT_PERIOD 0x003E 80 | #define VL6180X_SYSALS_ANALOGUE_GAIN 0x003F 81 | #define VL6180X_SYSALS_INTEGRATION_PERIOD 0x0040 82 | 83 | #define VL6180X_RESULT_RANGE_STATUS 0x004D 84 | #define VL6180X_RESULT_ALS_STATUS 0x004E 85 | #define VL6180X_RESULT_INTERRUPT_STATUS_GPIO 0x004F 86 | #define VL6180X_RESULT_ALS_VAL 0x0050 87 | #define VL6180X_RESULT_HISTORY_BUFFER 0x0052 88 | #define VL6180X_RESULT_RANGE_VAL 0x0062 89 | #define VL6180X_RESULT_RANGE_RAW 0x0064 90 | #define VL6180X_RESULT_RANGE_RETURN_RATE 0x0066 91 | #define VL6180X_RESULT_RANGE_REFERENCE_RATE 0x0068 92 | #define VL6180X_RESULT_RANGE_RETURN_SIGNAL_COUNT 0x006C 93 | #define VL6180X_RESULT_RANGE_REFERENCE_SIGNAL_COUNT 0x0070 94 | #define VL6180X_RESULT_RANGE_RETURN_AMB_COUNT 0x0074 95 | #define VL6180X_RESULT_RANGE_REFERENCE_AMB_COUNT 0x0078 96 | #define VL6180X_RESULT_RANGE_RETURN_CONV_TIME 0x007C 97 | #define VL6180X_RESULT_RANGE_REFERENCE_CONV_TIME 0x0080 98 | 99 | #define VL6180X_READOUT_AVERAGING_SAMPLE_PERIOD 0x010A 100 | #define VL6180X_FIRMWARE_BOOTUP 0x0119 101 | #define VL6180X_FIRMWARE_RESULT_SCALER 0x0120 102 | #define VL6180X_I2C_SLAVE_DEVICE_ADDRESS 0x0212 103 | #define VL6180X_INTERLEAVED_MODE_ENABLE 0x02A3 104 | 105 | enum vl6180x_als_gain 106 | { // Data sheet shows gain values as binary list 107 | 108 | GAIN_20 = 0, // Actual ALS Gain of 20 109 | GAIN_10, // Actual ALS Gain of 10.32 110 | GAIN_5, // Actual ALS Gain of 5.21 111 | GAIN_2_5, // Actual ALS Gain of 2.60 112 | GAIN_1_67, // Actual ALS Gain of 1.72 113 | GAIN_1_25, // Actual ALS Gain of 1.28 114 | GAIN_1, // Actual ALS Gain of 1.01 115 | GAIN_40, // Actual ALS Gain of 40 116 | 117 | }; 118 | 119 | struct VL6180xIdentification 120 | { 121 | uint8_t idModel; 122 | uint8_t idModelRevMajor; 123 | uint8_t idModelRevMinor; 124 | uint8_t idModuleRevMajor; 125 | uint8_t idModuleRevMinor; 126 | uint16_t idDate; 127 | uint16_t idTime; 128 | }; 129 | 130 | class VL6180x 131 | { 132 | public: 133 | // Initalize library with default address 134 | VL6180x(uint8_t address); 135 | // Send manditory settings as stated in ST datasheet. 136 | // http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf (Section 1.3) 137 | uint8_t VL6180xInit(void); 138 | // Use default settings from ST data sheet section 9. 139 | // http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf 140 | void VL6180xDefautSettings(void); 141 | 142 | // Get Range distance in (mm) 143 | uint8_t getDistance(); 144 | // Get ALS level in Lux 145 | float getAmbientLight(vl6180x_als_gain VL6180X_ALS_GAIN); 146 | 147 | // Load structure provided by the user with identification info 148 | // Structure example: 149 | // struct VL6180xIdentification 150 | // { 151 | // uint8_t idModel; 152 | // uint8_t idModelRevMajor; 153 | // uint8_t idModelRevMinor; 154 | // uint8_t idModuleRevMajor; 155 | // uint8_t idModuleRevMinor; 156 | // uint16_t idDate; 157 | // uint16_t idTime; 158 | // }; 159 | void getIdentification(struct VL6180xIdentification *temp); 160 | 161 | // Change the default address of the device to allow multiple 162 | // sensors on the bus. Can use up to 127 sensors. New address 163 | // is saved in non-volatile device memory. 164 | uint8_t changeAddress(uint8_t old_address, uint8_t new_address); 165 | 166 | private: 167 | // Store address given when the class is initialized. 168 | // This value can be changed by the changeAddress() function 169 | int _i2caddress; 170 | 171 | uint8_t VL6180x_getRegister(uint16_t registerAddr); 172 | uint16_t VL6180x_getRegister16bit(uint16_t registerAddr); 173 | 174 | void VL6180x_setRegister(uint16_t registerAddr, uint8_t data); 175 | void VL6180x_setRegister16bit(uint16_t registerAddr, uint16_t data); 176 | }; 177 | 178 | #endif 179 | --------------------------------------------------------------------------------