Qwiic Proximity Sensor (VCNL4040) Hookup Guide

Pages
Contributors: santaimpersonator, Englandsaurus
Favorited Favorite 2

Arduino Library

Note: This example assumes you are using the latest version of the Arduino IDE on your desktop. If this is your first time using Arduino, please review our tutorial on installing the Arduino IDE. If you have not previously installed an Arduino library, please check out our installation guide.

We’ve written an Arduino library to flex every feature of this sensor. You can grab it from the Arduino Library Manager by searching for 'SparkFun VCNL4040'. Otherwise you can get it from the GitHub repository or use the download button below.

Library Functions

The Arduino library is commented and the functions should be self-explanatory. However, below is a detailed list of the available library functions.

Note: It is recommended that users begin with the examples in the following section before diving head first into the library functions. The library examples demonstrate how to setup the more basic features of the VCNL4040 without having to dig through the datasheet. Once users have become familiar with the basic setups and have gone through the datasheet thoroughly, it should be easier to follow the library functions below.

.begin - Check communication with sensor and set it to default I2C Wire library settings

.isConnected - Checks connection

.setLEDCurrent - Sets the IR LED sink current to one of 8 settings (75, 100, 120, 140, 160, 180, and 200 mA).

.setIRDutyCycle - Sets duty cycle of the IR LED (40 max.)

.setProxInterruptPersistance - Sets the Prox interrupt persistance value.
The PS persistence function (PS_PERS, 1, 2, 3, 4) helps to avoid false trigger of the PS INT. It defines the amount of consecutive hits needed in order for a PS interrupt event to be triggered. .setAmbientInterruptPersistance - Sets the Ambient interrupt persistance value
The ALS persistence function (ALS_PERS, 1, 2, 4, 8) helps to avoid false trigger of the ALS INT. It defines the amount of consecutive hits needed in order for a ALS interrupt event to be triggered.

.setProxIntegrationTime - Sets the integration time for the proximity sensor
.setAmbientIntegrationTime - Sets the integration time for the ambient light sensor

.powerOnProximity - Power on the prox sensing portion of the device
.powerOffProximity - Power off the prox sensing portion of the device

.powerOnAmbient - Power on the ALS sensing portion of the device
.powerOffAmbient - Power off the ALS sensing portion of the device

.setProxResolution - Sets the proximity resolution to 12 or 16 bit

.enableAmbientInterrupts - Enables ALS interrupt
.disableAmbientInterrupts - Disables ALS interrupt

.enableSmartPersistance - Turn on smart presistance
.disableSmartPersistance - Turn off smart presistance
To accelerate the PS response time, smart persistence prevents the misjudgment of proximity sensing, but also keeps a fast response time.

.enableActiveForceMode - Enables active force mode
.disableActiveForceMode - Disable active force mode
An extreme power saving way to use PS is to apply PS active force mode. Anytime host would like to request one proximity measurement, enable the active force mode. This triggers a single PS measurement, which can be read from the PS result registers. VCNL4040 stays in standby mode constantly.

.takeSingleProxMeasurement - Sets trigger bit so sensor takes a force mode measurement and returns to standby.

.enableWhiteChannel - Enables the white measurement channel
.disableWhiteChannel - Disables the white measurement channel

.enableProxLogicMode - Enables the proximity detection logic output mode
.disableProxLogicMode - Disables the proximity detection logic output mode
When this mode is selected, the INT pin is pulled low when an object is close to the sensor (value is above high threshold) and is reset to high when the object moves away (value is below low threshold). Register: PS_THDH / PS_THDL define where these threshold levels are set.

.setProxCancellation - Sets the proximity sensing cancellation value - helps reduce cross talk with ambient light

.setALSHighThreshold - Value that ALS must go above to trigger an interrupt
.setALSLowThreshold - Value that ALS must go below to trigger an interrupt

.setProxHighThreshold - Value that Proximity Sensing must go above to trigger an interrupt
.setProxLowThreshold - Value that Proximity Sensing must go below to trigger an interrupt

.getProximity - Read the Proximity value
.getAmbient - Read the Ambient light value
.getWhite - Read the White light value
.getID - Read the sensors ID

.setProxInterruptType - Enable four proximity interrupt types
.isClose - Interrupt flag: True if prox value greater than high threshold
.isAway - Interrupt flag: True if prox value lower than low threshold
.isLight - Interrupt flag: True if ALS value higher than high threshold
.isDark - Interrupt flag: True if ALS value lower than low threshold

.readCommand - Reads two consecutive bytes from a given 'command code' location
.writeCommand - Write two bytes to a given command code location (8 bits)