It's easy to use the VL6180X sensor with Python and CircuitPython, and the Adafruit CircuitPython VL6180X module.  This module allows you to easily write Python code that reads the light and proximity readings from the sensor.

You can use this sensor with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.

CircuitPython Microcontroller Wiring

First wire up a VL6180 breakout to your board exactly as shown below. Here's an example of wiring a Feather M4 to the sensor with I2C:

  • Board GND to sensor GND (black wire)
  • Board 3V to sensor VIN (red wire)
  • Board SDA to sensor SDA (blue wire)
  • Board SCL to sensor SCL (yellow wire)
  • Board GND to sensor GND (black wire)
  • Board 3V to sensor VIN (red wire)
  • Board SDA to sensor SDA (blue wire)
  • Board SCL to sensor SCL (yellow wire)

Here's an example of wiring a Feather M0 to the sensor with I2C:

  • Board 3V to sensor VIN
  • Board GND to sensor GND
  • Board SCL to sensor SCL
  • Board SDA to sensor SDA

Python Computer Wiring

Since there's dozens of Linux computers/boards you can use we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported

Here's the Raspberry Pi wired with I2C:

  • Pi GND to sensor GND (black wire)
  • Pi 3V3 to sensor VIN (red wire)
  • Pi SDA to sensor SDA (blue wire)
  • Pi SCL to sensor SCL (yellow wire)
  • Pi GND to sensor GND (black wire)
  • Pi 3V3 to sensor VIN (red wire)
  • Pi SDA to sensor SDA (blue wire)
  • Pi SCL to sensor SCL (yellow wire)
  • Pi 3V3 to sensor VIN
  • Pi GND to sensor GND
  • Pi SCL to sensor SCL
  • Pi SDA to sensor SDA

CircuitPython Installation of VL6180X Library

Next you'll need to install the Adafruit CircuitPython VL6180X library on your CircuitPython board.  

First make sure you are running the latest version of Adafruit CircuitPython for your board.

Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle.  Our introduction guide has a great page on how to install the library bundle for both express and non-express boards.

Remember for non-express boards like the, you'll need to manually install the necessary libraries from the bundle:

  • adafruit_vl6180x.mpy
  • adafruit_bus_device

Before continuing make sure your board's lib folder or root filesystem has the adafruit_vl6180x.mpy, and adafruit_bus_device files and folders copied over.

Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

Python Installation of VL6180X Library

You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!

Once that's done, from your command line run the following command:

  • sudo pip3 install adafruit-circuitpython-vl6180x

If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!

CircuitPython & Python Usage

To demonstrate the usage of the sensor we'll initialize it and read the distance and more from the board's Python REPL.  Run the following code to import the necessary modules to initialize the I2C bus and sensor:

import board
import busio
import adafruit_vl6180x
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_vl6180x.VL6180X(i2c)

Now you're ready to read the range and other values from the sensor.  You can do so with a few properties and functions:

  • range Property - This property returns the range as read from the sensor in millimeters.
  • range_status Property - This property returns any error or issues that was encountered while reading the range.  You can compare against a few global values to see what might have occured.  Check the datasheet for more details about these error cases and how to resolve them:
    • adafruit_vl6180x.ERROR_NONE
    • adafruit_vl6180x.ERROR_SYSERR_1
    • adafruit_vl6180x.ERROR_SYSERR_5
    • adafruit_vl6180x.ERROR_ECEFAIL
    • adafruit_vl6180x.ERROR_NOCONVERGE
    • adafruit_vl6180x.ERROR_RANGEIGNORE
    • adafruit_vl6180x.ERROR_SNR
    • adafruit_vl6180x.ERROR_RAWUFLOW
    • adafruit_vl6180x.ERROR_RAWOFLOW
    • adafruit_vl6180x.ERROR_RANGEUFLOW
    • adafruit_vl6180x.ERROR_RANGEOFLOW
  • read_lux Function - This function can be called to read the light read from the sensor and return it in lux.  In order to call this function you must specify a gain that will be used during the reading.  Check the datasheet for more details on how gain impacts the reading and which value you might want to use for your application.  You can specify gain as one of these values:
    • adafruit_vl6180x.ALS_GAIN_1 - 1x gain
    • adafruit_vl6180x.ALS_GAIN_1_25 - 1.25x gain
    • adafruit_vl6180x.ALS_GAIN_1_67 - 1.67x gain
    • adafruit_vl6180x.ALS_GAIN_2_5 - 2.5x gain
    • adafruit_vl6180x.ALS_GAIN_5 - 5x gain
    • adafruit_vl6180x.ALS_GAIN_10 - 10x gain
    • adafruit_vl6180x.ALS_GAIN_20 - 20x gain
    • adafruit_vl6180x.ALS_GAIN_40 - 40x gain
print('Range: {0}mm'.format(sensor.range))
print('Range status: {0}'.format(sensor.range_status))
print('Light (1x gain): {0}lux'.format(sensor.read_lux(adafruit_vl6180x.ALS_GAIN_1)))

That's all there is to using the VL6180X distance sensor with CircuitPython!

Here's a complete example that will read the range and lux (using 1x gain) each second and print it out.  Save this as on your board and open the REPL to see the output.

Full Example Code

# SPDX-FileCopyrightText: 2018 Tony DiCola for Adafruit Industries
# SPDX-License-Identifier: MIT

# Demo of reading the range and lux from the VL6180x distance sensor and
# printing it every second.

import time

import board
import busio

import adafruit_vl6180x

# Create I2C bus.
i2c = busio.I2C(board.SCL, board.SDA)

# Create sensor instance.
sensor = adafruit_vl6180x.VL6180X(i2c)
# You can add an offset to distance measurements here (e.g. calibration)
# Swapping for the following would add a +10 millimeter offset to measurements:
# sensor = adafruit_vl6180x.VL6180X(i2c, offset=10)

# Main loop prints the range and lux every second:
while True:
    # Read the range in millimeters and print it.
    range_mm = sensor.range
    print("Range: {0}mm".format(range_mm))
    # Read the light, note this requires specifying a gain value:
    # - adafruit_vl6180x.ALS_GAIN_1 = 1x
    # - adafruit_vl6180x.ALS_GAIN_1_25 = 1.25x
    # - adafruit_vl6180x.ALS_GAIN_1_67 = 1.67x
    # - adafruit_vl6180x.ALS_GAIN_2_5 = 2.5x
    # - adafruit_vl6180x.ALS_GAIN_5 = 5x
    # - adafruit_vl6180x.ALS_GAIN_10 = 10x
    # - adafruit_vl6180x.ALS_GAIN_20 = 20x
    # - adafruit_vl6180x.ALS_GAIN_40 = 40x
    light_lux = sensor.read_lux(adafruit_vl6180x.ALS_GAIN_1)
    print("Light (1x gain): {0}lux".format(light_lux))
    # Delay for a second.

This guide was first published on Nov 30, 2016. It was last updated on Dec 08, 2023.

This page (Python & CircuitPython) was last updated on Dec 08, 2023.

Text editor powered by tinymce.