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: I2C, address: int = 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 = 3

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 = 2

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 = 9

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

property light_high_interrupt: bool

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

light_high_threshold = 1

Ambient light interrupt high threshold.

property light_integration_time: int

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 adafruit_vcnl4040

i2c = board.I2C()
sensor = adafruit_vcnl4040.VCNL4040(i2c)

sensor.light_integration_time = sensor.ALS_640MS

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

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

property light_low_interrupt: bool

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

light_low_threshold = 2

Ambient light interrupt low threshold.

light_shutdown = 0

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

property lux: float

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 adafruit_vcnl4040

i2c = board.I2C()
sensor = adafruit_vcnl4040.VCNL4040(i2c)

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

Proximity data.

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

import time
import board
import adafruit_vcnl4040

i2c = board.I2C()
sensor = adafruit_vcnl4040.VCNL4040(i2c)

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

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

property proximity_high_interrupt: bool

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 = 7

Proximity sensor interrupt high threshold setting.

proximity_integration_time = 3

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 = 2

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.

property proximity_low_interrupt: bool

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

proximity_low_threshold = 6

Proximity sensor interrupt low threshold setting.

proximity_shutdown = 3

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

property white: float

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 adafruit_vcnl4040

i2c = board.I2C()
sensor = adafruit_vcnl4040.VCNL4040(i2c)

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

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