VCNL4040 is a fully integrated proximity and ambient light sensor with infrared emitter, I2C interface, and interrupt function. See datasheet for detailed information.
Library feature list:
- Easy VCNL4040 interfacing
- Proximity measurement
- Ambient light as Raw value, Lux and White value
- Proximity/Ambient light threshold interrupts (not implemented yet!)
- 2017-06-07 Initial release
- 2017-08-09 I2C read delays removed in code
Download the ZIP file and extract its content. Move the TTVCNL4040 folder to "ARDUINOAPP/hardware/libraries/". In the Arduino IDE you'll find the sample sketches under Samples->TTVCNL4040.
If you are using an Arduino board other than 3.3V, ensure to use a logic level shifter!
white: SDA (3.3V!)
grey: SCL (3.3V!)
purple: INT (optional)
Don't forget to enable ambient light or proximity sensing after creating the object.
none
TTVCNL4040 vcnl4040 = TTVCNL4040();
Returns true if IC is connected correctly and detected by the I2C master.
none
boolean true/false
if(vcnl4040.isConnected()){
Serial.println("VCNL4040 IC is connected and running.");
}
Enables the ambient light sensing.
none
nothing
vcnl4040.enableALS();
Serial.println(vcnl4040.readALS());
Sets the timing and the corresponding multiplier for lux conversion.
time: ALS_IT_80MS -> 7864 lux max., ALS_IT_160MS -> 3832 lux max., ALS_IT_320MS -> 1966 lux max., ALS_IT_640MS -> 983 lux max.
nothing
vcnl4040.setALSIntegrationTime(ALS_IT_160MS);
Returns true if ambient light sensing is active.
none
boolean true/false
if(vcnl4040.isALSEnabled()){
//do something
}
Returns the ambient light sensing value as an unsigned integer.
none
unsigned int: Current ambient light value.
Serial.println(vcnl4040.readALS());
Returns the white channel value as an unsigned integer.
none
unsigned int: Current white light value.
Serial.println(vcnl4040.readWhite());
Returns the lux value as an unsigned integer. Sensitivity can be set by calling the setALSIntegrationTime() method.
none
unsigned int: Current lux value.
Serial.println(vcnl4040.readLux());
Disables the ambient light sensing. No ALS data can be read anymore.
none
nothing
vcnl4040.disableALS();
Enables the proximity sensing functionality.
irPower: IR_PWR_50MA, IR_PWR_75MA, IR_PWR_100MA, IR_PWR_120MA, IR_PWR_140MA, IR_PWR_160MA, IR_PWR_180MA or IR_PWR_200MA
nothing
vcnl4040.enablePS(IR_PWR_200MA);
Returns true if proximity sensing is active.
none
boolean true/false
if(vcnl4040.isPSEnabled()){
//do something
}
Disables the proximity sensing functionality. No PS data can be read anymore.
none
nothing
vcnl4040.disablePS();
Returns the selected register's two bytes as an unsigned integer.
registerAddress: 0x00 - 0x0C (see datasheet page 9)
unsigned int: containing the two register bytes (high and low byte)
unsigned int value = vcnl4040.readCommandRegister(0x0C);
Writes to the selected register.
registerAddress: 0x00 - 0x0C (see datasheet page 9)
lowByte: The low byte of a 16bit integer value
highByte: The high byte of a 16bit integer value
nothing
vcnl4040.writeCommandRegister(0x00, 0x00, 0x00);