Python & CircuitPython

It's easy to use the VCNL4010 sensor with Python and CircuitPython, and the Adafruit CircuitPython VCNL4010 module.  This module allows you to easily write Python code that reads small-distance proximity 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 VCNL4010 to your board exactly as shown in the previous pages for Arduino. Here's an example of wiring a Feather M0 to the sensor with I2C:

  • Board 3.3V to sensor Vin (Feather is 3.3V logic)
  • Board ground / GND to sensor ground / 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 3V3 to sensor VIN
  • Pi GND to sensor GND
  • Pi SCL to sensor SCK
  • Pi SDA to sensor SDA

CircuitPython Installation of VCNL4010 Library

To use the VCNL4010 you'll need to install the Adafruit CircuitPython VCNL4010 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_vcnl4010.mpy
  • adafruit_bus_device

Before continuing make sure your board's lib folder or root filesystem has the adafruit_vcnl4010.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 VCNL4010 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-vcnl4010

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 proximity and ambient light from the Python REPL.

Run the following code to import the necessary modules and initialize the I2C connection with the sensor:

Download: file
import board
import busio
import adafruit_vcnl4010
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_vcnl4010.VCNL4010(i2c)

Now you're ready to read values from the sensor using any of these properties:

  • proximity - The value of the proximity sensor.  This has no units and is inversely proportional to the distance of an object from the sensor.  I.e. the farther an object from the sensor the lower this value.  The range of values are 0 (very far away, up to ~200mm) to 65535 (right in front of the sensor).
  • ambient_lux - The detected ambient light as a value in lux.
  • ambient - The raw ambient light sensor value--this has no units but is proportional to the brightness.  You probably want to use the lux property above instead of this raw value!
Download: file
print('Proximity: {0}'.format(sensor.proximity))
print('Ambient light: {0} lux'.format(sensor.ambient_lux))

In addition there are a couple properties you can both read and write to change the behavior of the sensor:

  • led_current and led_current_mA - These properties control the amount of current given to the sensor's LED for proximity detection.  The sensor supports 0 - 200mA of current and the led_current property is set with units of 10mA (i.e. a value of 20 is 200mA), while the led_current_mA property takes the value in milliamps directly (like 200mA).  The default is 200mA of current for the brightest LED.
  • frequency - This property controls the frequency of measurement timings.  You can set it to a value of:
    • adafruit_vcnl4010.FREQUENCY_3M125 - 3.125 mhz
    • adafruit_vcnl4010.FREQUENCY_1M5625 - 1.5625 mhz
    • adafruit_vcnl4010.FREQUENCY_781K25 - 781.25 khz
    • adafruit_vcnl4010.FREQUENCY_390K625 - 390.625 khz (the default)

Read the VCNL4010 datasheet to understand how LED current and measurement frequency impact power consumption and accuracy!

Download: file
sensor.led_current_mA = 120
sensor.frequency = adafruit_vcnl4010.FREQUENCY_3M125

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

Here's a complete example of reading the proximity and ambient light every second and printing it at the serial terminal.  Save this as code.py on your board and open the serial terminal to see the output.

Full Example Code

# Simple demo of the VCNL4010 proximity and light sensor.
# Will print the proximity and ambient light every second.
# Author: Tony DiCola
import time

import board
import busio

import adafruit_vcnl4010


# Initialize I2C bus and VCNL4010 module.
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_vcnl4010.VCNL4010(i2c)

# You can optionally adjust the sensor LED current.  The default is 200mA
# which is the maximum value.  Note this is only set in 10mA increments.
#sensor.led_current_mA = 120  # Set 120 mA LED current

# You can also adjust the measurement frequency for the sensor.  The default
# is 390.625 khz, but these values are possible to set too:
# - FREQUENCY_3M125: 3.125 Mhz
# - FREQUENCY_1M5625: 1.5625 Mhz
# - FREQUENCY_781K25: 781.25 Khz
# - FREQUENCY_390K625: 390.625 Khz (default)
#sensor.frequency = adafruit_vcnl4010.FREQUENCY_3M125  # 3.125 Mhz

# Main loop runs forever printing the proximity and light level.
while True:
    proximity = sensor.proximity  # Proximity has no units and is a 16-bit
                                  # value.  The LOWER the value the further
                                  # an object from the sensor (up to ~200mm).
    print('Proximity: {0}'.format(proximity))
    ambient_lux = sensor.ambient_lux
    print('Ambient light: {0} lux'.format(ambient_lux))
    time.sleep(1.0)
This guide was first published on Dec 29, 2017. It was last updated on Dec 29, 2017. This page (Python & CircuitPython) was last updated on Aug 20, 2019.