Arduino Code

You can easily wire this breakout to any microcontroller, we'll be using an Arduino. For another kind of microcontroller, just make sure it has I2C, then port the API code. We strongly recommend using an Arduino to start though!

  • Connect Vin to the power supply, 3-5V is fine. Use the same voltage that the microcontroller logic is based off of. For most Arduinos, that is 5V
  • Connect GND to common power/data ground
  • Connect the SCL pin to the I2C clock SCL pin on your Arduino. On an UNO & '328 based Arduino, this is also known as A5, on a Mega it is also known as digital 21 and on a Leonardo/Micro, digital 3
  • Connect the SDA pin to the I2C data SDA pin on your Arduino. On an UNO & '328 based Arduino, this is also known as A4, on a Mega it is also known as digital 20 and on a Leonardo/Micro, digital 2

The VL6180X has a default I2C address of 0x29!

Install Adafruit_VL6180X

To begin reading sensor data, you will need to install Adafruit_VL6180X Library from our github repository. It is available from the Arduino library manager so we recommend using that.

From the IDE open up the library manager...

And type in adafruit vl6180 to locate the library. Click Install

We also have a great tutorial on Arduino library installation at:
http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use

Load Demo

Open up File->Examples->Adafruit_VL6180X->vl6180x and upload to your Arduino wired up to the sensor

Thats it! Now open up the serial terminal window at 115200 speed to begin the test.

Move your hand up and down to read the sensor data, the range readings are in millimeters and the light sensors in Lux. Note that when nothing is detected, it will print out the error which may vary.

Library Reference

 You can start out by creating a Adafruit_VL6180X object using the default I2C:

Download: file
Adafruit_VL6180X vl = Adafruit_VL6180X();

Once started, you can initialize the sensor which will also do some simple initializations and settings using begin(). Note that this will return True if the sensor was found and initialized. If not, it will return False.

Download: file
vl.begin()

Reading Distance

Reading the range/distance is easy, just call readRange()

Download: file
vl.readRange()

which will return the range in millimeters. If you get 0 or a value over 200 there's likely an error. Either way, before you trust that reading make sure to ask the sensor if the last reading had an error:

Download: file
uint8_t status = vl.readRangeStatus()

The status will be 0 on no error, anything else is an error. Here's a simple code chunk that will decode the error!

Download: file
  if  ((status >= VL6180X_ERROR_SYSERR_1) && (status <= VL6180X_ERROR_SYSERR_5)) {
    Serial.println("System error");
  }
  else if (status == VL6180X_ERROR_ECEFAIL) {
    Serial.println("ECE failure");
  }
  else if (status == VL6180X_ERROR_NOCONVERGE) {
    Serial.println("No convergence");
  }
  else if (status == VL6180X_ERROR_RANGEIGNORE) {
    Serial.println("Ignoring range");
  }
  else if (status == VL6180X_ERROR_SNR) {
    Serial.println("Signal/Noise error");
  }
  else if (status == VL6180X_ERROR_RAWUFLOW) {
    Serial.println("Raw reading underflow");
  }
  else if (status == VL6180X_ERROR_RAWOFLOW) {
    Serial.println("Raw reading overflow");
  }
  else if (status == VL6180X_ERROR_RANGEUFLOW) {
    Serial.println("Range reading underflow");
  }
  else if (status == VL6180X_ERROR_RANGEOFLOW) {
    Serial.println("Range reading overflow");
  }

Reading Light / Lux

You can also read a light reading from the sensor with

Download: file
vl.readLux(GAIN)

which will return a semi-calibrated Lux reading. You can use different Gain settings to get a different range. For better results at low light,  use higher gain. For better results at high light, use a lower gain.

Here's the gain's available:

  • VL6180X_ALS_GAIN_1   - gain of 1x
  • VL6180X_ALS_GAIN_1_25    - gain of 1.25x
  • VL6180X_ALS_GAIN_1_67    - gain of 1.67x
  • VL6180X_ALS_GAIN_2_5    - gain of 2.5x
  • VL6180X_ALS_GAIN_5    - gain of 5x
  • VL6180X_ALS_GAIN_10    - gain of 10x
  • VL6180X_ALS_GAIN_20    - gain of 20x
  • VL6180X_ALS_GAIN_40    - gain of 40x

We suggest starting with a gain of 5x and then adjusting up or down as necessary

This guide was first published on Nov 30, 2016. It was last updated on Nov 30, 2016. This page (Arduino Code) was last updated on Jul 22, 2019.