adafruit_vcnl4040

A CircuitPython library for the VCNL4040 proximity and ambient light sensor.

  • Author(s): Kattni Rembor

Implementation Notes

Hardware:

Software and Dependencies:

class adafruit_vcnl4040.VCNL4040(i2c, address=96)

Driver for the VCNL4040 proximity and ambient light sensor.

Parameters:i2c_bus (busio.I2C) – The I2C bus the VCNL4040 is connected to.
led_current

LED current selection setting, in mA. Options are LED_50MA, LED_75MA, LED_100MA, LED_120MA, LED_140MA, LED_160MA, LED_180MA, LED_200MA.

led_duty_cycle

Proximity sensor LED duty ratio setting. Ratios are 1/40, 1/80, 1/160, and 1/320. Options are: LED_1_40, LED_1_80, LED_1_160, LED_1_320.

light

Raw ambient light data. The raw ambient light data which will change with integration time and gain settings changes. Use lux to get the correctly scaled value for the current integration time and gain settings

light_high_interrupt

High interrupt event. Triggered when ambient light value exceeds high threshold.

light_high_threshold

Ambient light interrupt high threshold.

light_integration_time

Ambient light sensor integration time setting. Longer time has higher sensitivity. Can be: ALS_80MS, ALS_160MS, ALS_320MS or ALS_640MS.

This example sets the ambient light integration time to 640ms and prints the ambient light sensor data.

import time
import board
import busio
import adafruit_vcnl4040

i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_vcnl4040.VCNL4040(i2c)

sensor.light_integration_time = sensor.ALS_640MS

while True:
    print("Ambient light:", sensor.light)
light_interrupt

Ambient light sensor interrupt enable. True to enable, and False to disable.

light_low_interrupt

Low interrupt event. Triggered when ambient light value drops below low threshold.

light_low_threshold

Ambient light interrupt low threshold.

light_shutdown

Ambient light sensor shutdown. When True, ambient light data is disabled.

lux

Ambient light data in lux. Represents the raw sensor data scaled according to the current integration time and gain settings.

This example prints the ambient light data. Cover the sensor to see the values change.

import time
import board
import busio
import adafruit_vcnl4040

i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_vcnl4040.VCNL4040(i2c)

while True:
    print("Ambient light: %.2f lux"%sensor.lux)
    time.sleep(0.1)
proximity

Proximity data.

This example prints the proximity data. Move your hand towards the sensor to see the values change.

import time
import board
import busio
import adafruit_vcnl4040

i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_vcnl4040.VCNL4040(i2c)

while True:
    print("Proximity:", sensor.proximity)
    time.sleep(0.1)
proximity_bits

Proximity data output setting. 0 when proximity sensor output is 12 bits, 1 when proximity sensor output is 16 bits.

proximity_high_interrupt

If interrupt is set to PS_INT_CLOSE or PS_INT_CLOSE_AWAY, trigger event when proximity rises above high threshold interrupt.

proximity_high_threshold

Proximity sensor interrupt high threshold setting.

proximity_integration_time

Proximity sensor integration time setting. Integration times are 1T, 1.5T, 2T, 2.5T, 3T, 3.5T, 4T, and 8T. Options are: PS_1T, PS_1_5T, PS_2T, PS_2_5T, PS_3T, PS_3_5T, PS_4T, PS_8T.

proximity_interrupt

Interrupt enable. Interrupt setting are close, away, close and away, or disabled. Options are: PS_INT_DISABLE, PS_INT_CLOSE, PS_INT_AWAY, PS_INT_CLOSE_AWAY.

proximity_low_interrupt

If interrupt is set to PS_INT_AWAY or PS_INT_CLOSE_AWAY, trigger event when proximity drops below low threshold.

proximity_low_threshold

Proximity sensor interrupt low threshold setting.

proximity_shutdown

Proximity sensor shutdown. When True, proximity data is disabled.

white

White light data scaled according to the current integration time and gain settings.

This example prints the white light data. Cover the sensor to see the values change.

import time
import board
import busio
import adafruit_vcnl4040

i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_vcnl4040.VCNL4040(i2c)

while True:
    print("White light:", sensor.white)
    time.sleep(0.1)
white_shutdown

White light channel shutdown. When True, white light data is disabled.