Application Note

VISHAY SEMICONDUCTORS
www.vishay.com
Optical Sensors
Application Note
C++ Software Code for
VCNL4010, VCNL4020, and VCNL3020
This application note provides an overview of C++ software code that is available for the VCNL4010, VCNL4020, and
VCNL3020. There are three files included: main.ccp, the header file VCNL40x0.h, and the C-code file VCNL40x0.ccp.
To receive a copy of the complete software code along with the four text files that show screen results for the four applications
listed below, send an e-mail to [email protected].
The main.cpp contains four examples:
1. main #1 Read proximity on demand and ambient light on demand in endless loop
2. main #2 Proximity measurement in self-timed mode with 4 measurements/s
Read proximity value if ready with conversion, endless loop
3. main #3 Proximity measurement in self-timed mode with 31 measurements/s
Interrupt waiting on proximity value > upper threshold limit
4. main #4 Proximity measurement and ambient light measurement in self-timed mode
Proximity with 31 measurements/s, ambient light with 2 measurement/s
Interrupt waiting on proximity value > upper threshold limit
5. main #5 Read proximity on demand in an endless loop
main.cpp
The delivered main.cpp shows:
#define VERSION "\n Version: 1.2 01/2012\n"
#define MAIN4
// select MAIN1, MAIN2, MAIN3, MAIN4, or MAIN5
// * please note that MAIN1 and MAIN4 must be modified to be used with VCNL3020 *
(no ambient measurements) e.g. MAIN5
#define BAUD
115200
// increase up to 921600 for high speed communication (depends on terminal programm
and USB mode)
#include "mbed.h"
#include "VCNL40x0.h"
VCNL40x0 VCNL40x0_Device (p28, p27, VCNL40x0_ADDRESS);
DigitalOut mled0(LED1);
DigitalOut mled1(LED2);
DigitalOut mled2(LED3);
Serial pc(USBTX, USBRX);
//
//
//
//
//
Define SDA, SCL pin and I2C address
LED #1
LED #2
LED #3
Tx, Rx USB transmissiondefine VERSION "
mbed.h takes care of all necessary I2C-Bus, port, and internal and peripheral handling.
The function of the LEDs is just to show the I2C transmissions and interrupt activation. More information about the
microcontroller and the compiler can be found at the end of this document.
The main #4 is the most complex software code and is shown below.
For main #4, the proximity measurement speed is 31 measurements per second in self-timed mode. The ambient light
measurement which is made at the same time has a rate of 2 measurements per second.
The interrupt is assigned to the proximity measurement and the upper threshold is set to 100 counts above the previously
measured offset counts.
When the count exceeds the upper threshold and an interrupt is generated, a red indicator light is illuminated on the VCNL40x0
demo kit sensor board.
Revision: 07-Dec-12
Document Number: 84140
1
For technical questions, contact: [email protected]
THIS DOCUMENT IS SUBJECT TO CHANGE WITHOUT NOTICE. THE PRODUCTS DESCRIBED HEREIN AND THIS DOCUMENT
ARE SUBJECT TO SPECIFIC DISCLAIMERS, SET FORTH AT www.vishay.com/doc?91000
APPLICATION NOTE
This header file comes with the mbed board: (www.mbed.org).
Application Note
www.vishay.com
Vishay Semiconductors
C++ Software Code for
VCNL4010, VCNL4020, and VCNL3020
main.cpp [Example 4]
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// main #4
// Proximity Measurement and Ambient light Measurement in selftimed mode
// Proximity with 31 measurements/s, Ambient light with 2 measurement/s
// Interrupt waiting on proximity value > upper threshold limit
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
# ifdef MAIN4
int main() {
unsigned
unsigned
unsigned
unsigned
unsigned
unsigned
unsigned
unsigned
unsigned
unsigned
int i=0;
char ID=0;
char Command=0;
char Current=0;
int ProxiValue=0;
int SummeProxiValue=0;
int AverageProxiValue=0;
int AmbiValue=0;
char InterruptStatus=0;
char InterruptControl=0;
pc.baud(BAUD);
// print information on screen
pc.printf("\n\n VCNL4010/4020/3020 Proximity/Ambient Light Sensor");
pc.printf("\n library tested with mbed LPC1768 (ARM Cortex-M3 core) on www.mbed.org");
pc.printf(VERSION);
pc.printf("\n Demonstration #4:");
pc.printf("\n Proximity Measurement and Ambient light Measurement in selftimed mode");
pc.printf("\n Proximity with 31 measurements/s, Ambient light with 2 measurement/s");
pc.printf("\n Interrupt waiting on proximity value > upper threshold limit");
VCNL40x0_Device.ReadID (&ID);
// Read VCNL40x0 product ID revision register
pc.printf("\n\n Product ID Revision Register: %d", ID);
VCNL40x0_Device.SetCurrent (20);
VCNL40x0_Device.ReadCurrent (&Current);
pc.printf("\n IR LED Current: %d", Current);
// Set current to 200mA
// Read back IR LED current
// stop all activities (necessary for changing proximity rate, see datasheet)
VCNL40x0_Device.SetCommandRegister (COMMAND_ALL_DISABLE);
// set proximity rate to 31/s
VCNL40x0_Device.SetProximityRate (PROX_MEASUREMENT_RATE_31);
// enable prox and ambi in selftimed mode
VCNL40x0_Device.SetCommandRegister (COMMAND_PROX_ENABLE |
COMMAND_AMBI_ENABLE |
COMMAND_SELFTIMED_MODE_ENABLE);
// set interrupt control for threshold
VCNL40x0_Device.SetInterruptControl (INTERRUPT_THRES_SEL_PROX |
INTERRUPT_THRES_ENABLE |
INTERRUPT_COUNT_EXCEED_1);
// set ambient light measurement parameter
VCNL40x0_Device.SetAmbiConfiguration (AMBI_PARA_AVERAGE_32 |
AMBI_PARA_AUTO_OFFSET_ENABLE |
AMBI_PARA_MEAS_RATE_2);
APPLICATION NOTE
// measure average of prox value
SummeProxiValue = 0;
for (i=0; i<30; i++) {
do {
VCNL40x0_Device.ReadCommandRegister (&Command);
} while (!(Command & COMMAND_MASK_PROX_DATA_READY));
// wait on prox data ready bit
// read command register
// prox data ready ?
VCNL40x0_Device.ReadProxiValue (&ProxiValue);
// read prox value
SummeProxiValue += ProxiValue;
// Summary of all measured prox values
}
AverageProxiValue = SummeProxiValue/30;
// calculate average
VCNL40x0_Device.SetHighThreshold (AverageProxiValue+100);
// set upper threshold for interrupt
pc.printf("\n Upper Threshold Value: %i cts\n\n", AverageProxiValue+100);
wait_ms(2000);
// wait 2s (only for display)
// endless loop /////////////////////////////////////////////////////////////////////////////////////
Revision: 07-Dec-12
Document Number: 84140
2
For technical questions, contact: [email protected]
THIS DOCUMENT IS SUBJECT TO CHANGE WITHOUT NOTICE. THE PRODUCTS DESCRIBED HEREIN AND THIS DOCUMENT
ARE SUBJECT TO SPECIFIC DISCLAIMERS, SET FORTH AT www.vishay.com/doc?91000
Application Note
www.vishay.com
Vishay Semiconductors
C++ Software Code for
VCNL4010, VCNL4020, and VCNL3020
while (1) {
// wait on data ready bit
do {
VCNL40x0_Device.ReadCommandRegister (&Command); // read command register
} while (!(Command & (COMMAND_MASK_PROX_DATA_READY | COMMAND_MASK_AMBI_DATA_READY))); // data ready ?
// read interrupt status register
VCNL40x0_Device.ReadInterruptStatus (&InterruptStatus);
// check interrupt status for High Threshold
if (InterruptStatus & INTERRUPT_MASK_STATUS_THRES_HI) {
mled2 = 1;
VCNL40x0_Device.SetInterruptStatus (InterruptStatus);
mled2 = 0;
}
// prox value ready for using
if (Command & COMMAND_MASK_PROX_DATA_READY) {
mled0 = 1;
VCNL40x0_Device.ReadProxiValue (&ProxiValue);
// LED on, Interrupt
// clear Interrupt Status
// LED off, Interrupt
// LED on, Prox Data Ready
// read prox value
// print prox value and interrupt status on screen
pc.printf("\nProxi: %5.0i cts \tInterruptStatus: %i", ProxiValue, InterruptStatus);
mled0 = 0;
// LED off, Prox data Ready
}
// ambi value ready for using
if (Command & COMMAND_MASK_AMBI_DATA_READY) {
mled1 = 1;
VCNL40x0_Device.ReadAmbiValue (&AmbiValue);
// LED on, Ambi Data Ready
// read ambi value
// print ambi value and interrupt status on screen
pc.printf("\n
mled1 = 0;
Ambi: %i", AmbiValue);
// LED off, Ambi Data Ready
}
}
}
# endif
VCNL40x0.h
This VCNL40x0.h header file contains all VCNL40x0 register numbers and registers bit information:
#ifndef VCNL40x0_H
#define VCNL40x0_H
#include "mbed.h"
APPLICATION NOTE
// Library for the Vishay Proximity/Ambient Light Sensor VCNL4010/4020/3020
// The VCNL4x00 is a I2C digital Proximity and Ambient Light Sensor in a small SMD package
#define VCNL40x0_ADDRESS
(0x26) // 001 0011 shifted left 1 bit = 0x26
// registers
#define REGISTER_COMMAND
#define REGISTER_ID
#define REGISTER_PROX_RATE
#define REGISTER_PROX_CURRENT
#define REGISTER_AMBI_PARAMETER
#define REGISTER_AMBI_VALUE
#define REGISTER_PROX_VALUE
#define REGISTER_INTERRUPT_CONTROL
#define REGISTER_INTERRUPT_LOW_THRES
#define REGISTER_INTERRUPT_HIGH_THRES
#define REGISTER_INTERRUPT_STATUS
#define REGISTER_PROX_TIMING
#define REGISTER_AMBI_IR_LIGHT_LEVEL
(0x80)
(0x81)
(0x82)
(0x83)
(0x84)
(0x85)
(0x87)
(0x89)
(0x8a)
(0x8c)
(0x8e)
(0xf9)
(0x90)
// Bits
#define
#define
#define
#define
#define
#define
#define
#define
#define
(0x00)
(0x01)
(0x02)
(0x04)
(0x08)
(0x10)
(0x20)
(0x40)
(0x80)
in Command register (0x80)
COMMAND_ALL_DISABLE
COMMAND_SELFTIMED_MODE_ENABLE
COMMAND_PROX_ENABLE
COMMAND_AMBI_ENABLE
COMMAND_PROX_ON_DEMAND
COMMAND_AMBI_ON_DEMAND
COMMAND_MASK_PROX_DATA_READY
COMMAND_MASK_AMBI_DATA_READY
COMMAND_MASK_LOCK
Revision: 07-Dec-12
// This register is not intended to be use by customer
Document Number: 84140
3
For technical questions, contact: [email protected]
THIS DOCUMENT IS SUBJECT TO CHANGE WITHOUT NOTICE. THE PRODUCTS DESCRIBED HEREIN AND THIS DOCUMENT
ARE SUBJECT TO SPECIFIC DISCLAIMERS, SET FORTH AT www.vishay.com/doc?91000
Application Note
www.vishay.com
Vishay Semiconductors
C++ Software Code for
VCNL4010, VCNL4020, and VCNL3020
// Bits in Product ID Revision Register (0x81)
#define PRODUCT_MASK_REVISION_ID
(0x0f)
#define PRODUCT_MASK_PRODUCT_ID
(0xf0)
// Bits
#define
#define
#define
#define
#define
#define
#define
#define
#define
in Prox Measurement Rate register (0x82)
PROX_MEASUREMENT_RATE_2
(0x00)
// DEFAULT
PROX_MEASUREMENT_RATE_4
(0x01)
PROX_MEASUREMENT_RATE_8
(0x02)
PROX_MEASUREMENT_RATE_16
(0x03)
PROX_MEASUREMENT_RATE_31
(0x04)
PROX_MEASUREMENT_RATE_62
(0x05)
PROX_MEASUREMENT_RATE_125
(0x06)
PROX_MEASUREMENT_RATE_250
(0x07)
PROX_MASK_MEASUREMENT_RATE
(0x07)
// Bits in Proximity LED current setting (0x83)
#define PROX_MASK_LED_CURRENT
(0x3f)
// DEFAULT = 2
#define PROX_MASK_FUSE_PROG_ID
(0xc0)
// Bits
#define
#define
#define
#define
#define
#define
#define
#define
#define
in Ambient Light Parameter register (0x84)
AMBI_PARA_AVERAGE_1
(0x00)
AMBI_PARA_AVERAGE_2
(0x01)
AMBI_PARA_AVERAGE_4
(0x02)
AMBI_PARA_AVERAGE_8
(0x03)
AMBI_PARA_AVERAGE_16
(0x04)
AMBI_PARA_AVERAGE_32
(0x05)
// DEFAULT
AMBI_PARA_AVERAGE_64
(0x06)
AMBI_PARA_AVERAGE_128
(0x07)
AMBI_MASK_PARA_AVERAGE
(0x07)
#define AMBI_PARA_AUTO_OFFSET_ENABLE
#define AMBI_MASK_PARA_AUTO_OFFSET
(0x08)
(0x08)
#define
#define
#define
#define
#define
#define
#define
#define
#define
(0x00)
(0x10)
(0x20)
(0x30)
(0x40)
(0x50)
(0x60)
(0x70)
(0x70)
AMBI_PARA_MEAS_RATE_1
AMBI_PARA_MEAS_RATE_2
AMBI_PARA_MEAS_RATE_3
AMBI_PARA_MEAS_RATE_4
AMBI_PARA_MEAS_RATE_5
AMBI_PARA_MEAS_RATE_6
AMBI_PARA_MEAS_RATE_8
AMBI_PARA_MEAS_RATE_10
AMBI_MASK_PARA_MEAS_RATE
#define AMBI_PARA_CONT_CONV_ENABLE
#define AMBI_MASK_PARA_CONT_CONV
(0x80)
(0x80)
// DEFAULT enable
// DEFAULT
// DEFAULT disable
APPLICATION NOTE
// Bits in Interrupt Control Register (x89)
#define INTERRUPT_THRES_SEL_PROX
(0x00)
#define INTERRUPT_THRES_SEL_ALS
(0x01)
#define INTERRUPT_THRES_ENABLE
(0x02)
#define INTERRUPT_ALS_READY_ENABLE
(0x04)
#define INTERRUPT_PROX_READY_ENABLE
(0x08)
#define
#define
#define
#define
#define
#define
#define
#define
#define
INTERRUPT_COUNT_EXCEED_1
INTERRUPT_COUNT_EXCEED_2
INTERRUPT_COUNT_EXCEED_4
INTERRUPT_COUNT_EXCEED_8
INTERRUPT_COUNT_EXCEED_16
INTERRUPT_COUNT_EXCEED_32
INTERRUPT_COUNT_EXCEED_64
INTERRUPT_COUNT_EXCEED_128
INTERRUPT_MASK_COUNT_EXCEED
(0x00)
(0x20)
(0x40)
(0x60)
(0x80)
(0xa0)
(0xc0)
(0xe0)
(0xe0)
// Bits
#define
#define
#define
#define
#define
#define
#define
#define
in Interrupt Status Register (x8e)
INTERRUPT_STATUS_THRES_HI
(0x01)
INTERRUPT_STATUS_THRES_LO
(0x02)
INTERRUPT_STATUS_ALS_READY
(0x04)
INTERRUPT_STATUS_PROX_READY
(0x08)
INTERRUPT_MASK_STATUS_THRES_HI (0x01)
INTERRUPT_MASK_THRES_LO
(0x02)
INTERRUPT_MASK_ALS_READY
(0x04)
INTERRUPT_MASK_PROX_READY
(0x08)
Revision: 07-Dec-12
// DEFAULT
Document Number: 84140
4
For technical questions, contact: [email protected]
THIS DOCUMENT IS SUBJECT TO CHANGE WITHOUT NOTICE. THE PRODUCTS DESCRIBED HEREIN AND THIS DOCUMENT
ARE SUBJECT TO SPECIFIC DISCLAIMERS, SET FORTH AT www.vishay.com/doc?91000
Application Note
www.vishay.com
Vishay Semiconductors
C++ Software Code for
VCNL4010, VCNL4020, and VCNL3020
typedef enum {
VCNL40x0_ERROR_OK = 0,
VCNL40x0_ERROR_I2CINIT,
VCNL40x0_ERROR_I2CBUSY,
VCNL40x0_ERROR_LAST
} VCNL40x0Error_e;
// Everything executed normally
// Unable to initialize I2C
// I2C already in use
class VCNL40x0 {
public:
// Creates an instance of the class.
// Connect module at I2C address addr using I2C port pins sda and scl.
VCNL40x0 (PinName sda, PinName scl, unsigned char addr);
// Destroys instance
~VCNL40x0();
// public functions
VCNL40x0Error_e Init (void);
VCNL40x0Error_e
VCNL40x0Error_e
VCNL40x0Error_e
VCNL40x0Error_e
VCNL40x0Error_e
VCNL40x0Error_e
VCNL40x0Error_e
VCNL40x0Error_e
VCNL40x0Error_e
SetCommandRegister (unsigned char Command);
SetCurrent (unsigned char CurrentValue);
SetProximityRate (unsigned char ProximityRate);
SetAmbiConfiguration (unsigned char AmbiConfiguration);
SetLowThreshold (unsigned int LowThreshold);
SetHighThreshold (unsigned int HighThreshold);
SetInterruptControl (unsigned char InterruptControl);
SetInterruptStatus (unsigned char InterruptStatus);
SetModulatorTimingAdjustment (unsigned char ModulatorTimingAdjustment);
VCNL40x0Error_e
VCNL40x0Error_e
VCNL40x0Error_e
VCNL40x0Error_e
VCNL40x0Error_e
VCNL40x0Error_e
VCNL40x0Error_e
ReadID (unsigned char *ID);
ReadCurrent (unsigned char *CurrentValue);
ReadCommandRegister (unsigned char *Command);
ReadProxiValue (unsigned int *ProxiValue);
ReadAmbiValue (unsigned int *AmbiValue);
ReadInterruptStatus (unsigned char *InterruptStatus);
ReadInterruptControl (unsigned char *InterruptControl);
VCNL40x0Error_e ReadProxiOnDemand (unsigned int *ProxiValue);
VCNL40x0Error_e ReadAmbiOnDemand (unsigned int *AmbiValue);
private:
I2C _i2c;
int _addr;
char _send[3];
char _receive[2];
};
#endif
VCNL40x0.cpp
This VCNL40x0.cpp file contains all definitions and public functions:
#include "VCNL40x0.h"
APPLICATION NOTE
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
VCNL40x0::VCNL40x0(PinName sda, PinName scl, unsigned char addr) : _i2c(sda, scl), _addr(addr) {
_i2c.frequency(1000000);
// set I2C frequency to 1MHz
}
VCNL40x0::~VCNL40x0() {
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
VCNL40x0Error_e VCNL40x0::SetCommandRegister (unsigned char Command) {
_send[0] = REGISTER_COMMAND;
_send[1] = Command;
_i2c.write(VCNL40x0_ADDRESS,_send, 2);
// VCNL40x0 Configuration register
// Write 2 bytes on I2C
return VCNL40x0_ERROR_OK;
}
Revision: 07-Dec-12
Document Number: 84140
5
For technical questions, contact: [email protected]
THIS DOCUMENT IS SUBJECT TO CHANGE WITHOUT NOTICE. THE PRODUCTS DESCRIBED HEREIN AND THIS DOCUMENT
ARE SUBJECT TO SPECIFIC DISCLAIMERS, SET FORTH AT www.vishay.com/doc?91000
Application Note
www.vishay.com
Vishay Semiconductors
C++ Software Code for
VCNL4010, VCNL4020, and VCNL3020
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
VCNL40x0Error_e VCNL40x0::ReadCommandRegister (unsigned char *Command) {
_send[0] = REGISTER_COMMAND;
_i2c.write(VCNL40x0_ADDRESS,_send, 1);
_i2c.read(VCNL40x0_ADDRESS+1,_receive, 1);
// VCNL40x0 Configuration register
// Write 1 byte on I2C
// Read 1 byte on I2C
*Command = (unsigned char)(_receive[0]);
return VCNL40x0_ERROR_OK;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
VCNL40x0Error_e VCNL40x0::ReadID (unsigned char *ID) {
_send[0] = REGISTER_ID;
_i2c.write(VCNL40x0_ADDRESS, _send, 1);
_i2c.read(VCNL40x0_ADDRESS+1, _receive, 1);
// VCNL40x0 product ID revision register
// Write 1 byte on I2C
// Read 1 byte on I2C
*ID = (unsigned char)(_receive[0]);
return VCNL40x0_ERROR_OK;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
VCNL40x0Error_e VCNL40x0::SetCurrent (unsigned char Current) {
_send[0] = REGISTER_PROX_CURRENT;
_send[1] = Current;
_i2c.write(VCNL40x0_ADDRESS,_send, 2);
// VCNL40x0 IR LED Current register
// Write 2 bytes on I2C
return VCNL40x0_ERROR_OK;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
VCNL40x0Error_e VCNL40x0::ReadCurrent (unsigned char *Current) {
_send[0] = REGISTER_PROX_CURRENT;
_i2c.write(VCNL40x0_ADDRESS,_send, 1);
_i2c.read(VCNL40x0_ADDRESS+1,_receive, 1);
// VCNL40x0 IR LED current register
// Write 1 byte on I2C
// Read 1 byte on I2C
*Current = (unsigned char)(_receive[0]);
return VCNL40x0_ERROR_OK;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
VCNL40x0Error_e VCNL40x0::SetProximityRate (unsigned char ProximityRate) {
_send[0] = REGISTER_PROX_RATE;
_send[1] = ProximityRate;
_i2c.write(VCNL40x0_ADDRESS,_send, 2);
// VCNL40x0 Proximity rate register
// Write 2 bytes on I2C
return VCNL40x0_ERROR_OK;
}
APPLICATION NOTE
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
VCNL40x0Error_e VCNL40x0::SetAmbiConfiguration (unsigned char AmbiConfiguration) {
_send[0] = REGISTER_AMBI_PARAMETER;
_send[1] = AmbiConfiguration;
_i2c.write(VCNL40x0_ADDRESS,_send, 2);
// VCNL40x0 Ambient light configuration
// Write 2 bytes on I2C
return VCNL40x0_ERROR_OK;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
VCNL40x0Error_e VCNL40x0::SetInterruptControl (unsigned char InterruptControl) {
_send[0] = REGISTER_INTERRUPT_CONTROL;
_send[1] = InterruptControl;
_i2c.write(VCNL40x0_ADDRESS,_send, 2);
// VCNL40x0 Interrupt Control register
// Write 2 bytes on I2C
return VCNL40x0_ERROR_OK;
}
Revision: 07-Dec-12
Document Number: 84140
6
For technical questions, contact: [email protected]
THIS DOCUMENT IS SUBJECT TO CHANGE WITHOUT NOTICE. THE PRODUCTS DESCRIBED HEREIN AND THIS DOCUMENT
ARE SUBJECT TO SPECIFIC DISCLAIMERS, SET FORTH AT www.vishay.com/doc?91000
Application Note
www.vishay.com
Vishay Semiconductors
C++ Software Code for
VCNL4010, VCNL4020, and VCNL3020
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
VCNL40x0Error_e VCNL40x0::ReadInterruptControl (unsigned char *InterruptControl) {
_send[0] = REGISTER_INTERRUPT_CONTROL;
_i2c.write(VCNL40x0_ADDRESS,_send, 1);
_i2c.read(VCNL40x0_ADDRESS+1,_receive, 1);
// VCNL40x0 Interrupt Control register
// Write 1 byte on I2C
// Read 1 byte on I2C
*InterruptControl = (unsigned char)(_receive[0]);
return VCNL40x0_ERROR_OK;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
VCNL40x0Error_e VCNL40x0::SetInterruptStatus (unsigned char InterruptStatus) {
_send[0] = REGISTER_INTERRUPT_STATUS;
_send[1] = InterruptStatus;
_i2c.write(VCNL40x0_ADDRESS,_send, 2);
// VCNL40x0 Interrupt Status register
// Write 2 bytes on I2C
return VCNL40x0_ERROR_OK;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
VCNL40x0Error_e VCNL40x0::SetModulatorTimingAdjustment (unsigned char ModulatorTimingAdjustment) {
_send[0] = REGISTER_PROX_TIMING;
_send[1] = ModulatorTimingAdjustment;
_i2c.write(VCNL40x0_ADDRESS,_send, 2);
// VCNL40x0 Modulator Timing Adjustment register
// Write 2 bytes on I2C
return VCNL40x0_ERROR_OK;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
VCNL40x0Error_e VCNL40x0::ReadInterruptStatus (unsigned char *InterruptStatus) {
_send[0] = REGISTER_INTERRUPT_STATUS;
_i2c.write(VCNL40x0_ADDRESS,_send, 1);
_i2c.read(VCNL40x0_ADDRESS+1,_receive, 1);
// VCNL40x0 Interrupt Status register
// Write 1 byte on I2C
// Read 1 byte on I2C
*InterruptStatus = (unsigned char)(_receive[0]);
return VCNL40x0_ERROR_OK;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
VCNL40x0Error_e VCNL40x0::ReadProxiValue (unsigned int *ProxiValue) {
_send[0] = REGISTER_PROX_VALUE;
// VCNL40x0 Proximity Value register
_i2c.write(VCNL40x0_ADDRESS, _send, 1);
// Write 1 byte on I2C
_i2c.read(VCNL40x0_ADDRESS+1, _receive, 2);
// Read 2 bytes on I2C
*ProxiValue = ((unsigned int)_receive[0] << 8 | (unsigned char)_receive[1]);
return VCNL40x0_ERROR_OK;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
APPLICATION NOTE
VCNL40x0Error_e VCNL40x0::ReadAmbiValue (unsigned int *AmbiValue) {
_send[0] = REGISTER_AMBI_VALUE;
_i2c.write(VCNL40x0_ADDRESS, _send, 1);
_i2c.read(VCNL40x0_ADDRESS+1, _receive, 2);
*AmbiValue = ((unsigned int)_receive[0] << 8 | (unsigned
// VCNL40x0 Ambient Light Value register
// Write 1 byte on I2C
// Read 2 bytes on I2C
char)_receive[1]);
return VCNL40x0_ERROR_OK;
}
Revision: 07-Dec-12
Document Number: 84140
7
For technical questions, contact: [email protected]
THIS DOCUMENT IS SUBJECT TO CHANGE WITHOUT NOTICE. THE PRODUCTS DESCRIBED HEREIN AND THIS DOCUMENT
ARE SUBJECT TO SPECIFIC DISCLAIMERS, SET FORTH AT www.vishay.com/doc?91000
Application Note
www.vishay.com
Vishay Semiconductors
C++ Software Code for
VCNL4010, VCNL4020, and VCNL3020
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
VCNL40x0Error_e VCNL40x0::SetLowThreshold (unsigned int LowThreshold) {
unsigned char LoByte=0, HiByte=0;
LoByte = (unsigned char)(LowThreshold & 0x00ff);
HiByte = (unsigned char)((LowThreshold & 0xff00)>>8);
_send[0] = REGISTER_INTERRUPT_LOW_THRES;
_send[1] = HiByte;
_i2c.write(VCNL40x0_ADDRESS,_send, 2);
_send[0] = REGISTER_INTERRUPT_LOW_THRES+1;
_send[1] = LoByte;
_i2c.write(VCNL40x0_ADDRESS,_send, 2);
// VCNL40x0 Low Threshold Register, Hi Byte
// Write 2 bytes on I2C
// VCNL40x0 Low Threshold Register, Lo Byte
// Write 2 bytes on I2C
return VCNL40x0_ERROR_OK;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
VCNL40x0Error_e VCNL40x0::SetHighThreshold (unsigned int HighThreshold) {
unsigned char LoByte=0, HiByte=0;
LoByte = (unsigned char)(HighThreshold & 0x00ff);
HiByte = (unsigned char)((HighThreshold & 0xff00)>>8);
_send[0] = REGISTER_INTERRUPT_HIGH_THRES;
_send[1] = HiByte;
_i2c.write(VCNL40x0_ADDRESS,_send, 2);
// Write 2 bytes on I2C
// VCNL40x0 High Threshold Register, Hi Byte
_send[0] = REGISTER_INTERRUPT_HIGH_THRES+1;
_send[1] = LoByte;
_i2c.write(VCNL40x0_ADDRESS,_send, 2);
// Write 2 bytes on I2C
// VCNL40x0 High Threshold Register, Lo Byte
return VCNL40x0_ERROR_OK;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
VCNL40x0Error_e VCNL40x0::ReadProxiOnDemand (unsigned int *ProxiValue) {
unsigned char Command=0;
// enable prox value on demand
SetCommandRegister (COMMAND_PROX_ENABLE | COMMAND_PROX_ON_DEMAND);
// wait on prox data ready bit
do {
ReadCommandRegister (&Command);
} while (!(Command & COMMAND_MASK_PROX_DATA_READY));
// read command register
ReadProxiValue (ProxiValue);
// read prox value
SetCommandRegister (COMMAND_ALL_DISABLE);
// stop prox value on demand
return VCNL40x0_ERROR_OK;
}
APPLICATION NOTE
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
VCNL40x0Error_e VCNL40x0::ReadAmbiOnDemand (unsigned int *AmbiValue) {
unsigned char Command=0;
// enable ambi value on demand
SetCommandRegister (COMMAND_PROX_ENABLE | COMMAND_AMBI_ON_DEMAND);
// wait on ambi data ready bit
do {
ReadCommandRegister (&Command);
} while (!(Command & COMMAND_MASK_AMBI_DATA_READY));
// read command register
ReadAmbiValue (AmbiValue);
// read ambi value
SetCommandRegister (COMMAND_ALL_DISABLE);
// stop ambi value on demand
return VCNL40x0_ERROR_OK;
}
Revision: 07-Dec-12
Document Number: 84140
8
For technical questions, contact: [email protected]
THIS DOCUMENT IS SUBJECT TO CHANGE WITHOUT NOTICE. THE PRODUCTS DESCRIBED HEREIN AND THIS DOCUMENT
ARE SUBJECT TO SPECIFIC DISCLAIMERS, SET FORTH AT www.vishay.com/doc?91000
Application Note
www.vishay.com
Vishay Semiconductors
C++ Software Code for
VCNL4010, VCNL4020, and VCNL3020
The five text files included within the zip show the results that would be seen on-screen when running the five separate listed
applications.
These five examples are related to the “main #1” to “main #5” routines within the main.ccp:
Logging_Main1.txt
In addition to the proximity on demand value, this “main #1” also shows the ambient light on demand value and calculated
illuminance.
...
Proxi:
Proxi:
Proxi:
Proxi:
3012
2974
3003
3074
cts
cts
cts
cts
Ambi:
Ambi:
Ambi:
Ambi:
710
709
709
708
cts
cts
cts
cts
Illuminance:
Illuminance:
Illuminance:
Illuminance:
177.50
177.25
177.25
177.00
lx
lx
lx
lx
...
To get the illuminance value from the counts, just divide them by four, e.g.: 708 cts/4 = 177.00 lx.
Logging_Main2.txt
This “main #2” shows some results of proximity measurement in self-timed mode with 4 measurements/s.
...
Proxi:
Proxi:
Proxi:
2458 cts
2390 cts
2444 cts
...
The endless loop enables evaluation of the offset counts within the used application, as well as testing for the amount of counts
that will be read out with the object in a defined distance.
Logging_Main3.txt
The “main #3” shows results of proximity measurement in self-timed mode with 31 measurements/s and also the corresponding
interrupt status.
...
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
2930
3002
3043
3218
3257
3307
cts
cts
cts
cts
cts
cts
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
0
0
0
1
1
1
...
Within the line where the interrupt status changes from “0” to “1,” one can also see the change to 3218 counts from just
3043 which is more than 100 over the defined limit.
APPLICATION NOTE
Logging_Main4.txt
The “main #4” shows the results of parallel proximity measurements AND ambient light measurements in self-timed mode with
31 measurements/s for proximity and 2 measurements/s for ambient light.
The interrupt is assigned to proximity and a subroutine is averaging 30 samples to define currently available offset counts. The
upper threshold is set at 100 counts higher than this averaged result.
Only this fourth example “Logging_Main4.txt” is shown here completely, and explained on the next page.
Revision: 07-Dec-12
Document Number: 84140
9
For technical questions, contact: [email protected]
THIS DOCUMENT IS SUBJECT TO CHANGE WITHOUT NOTICE. THE PRODUCTS DESCRIBED HEREIN AND THIS DOCUMENT
ARE SUBJECT TO SPECIFIC DISCLAIMERS, SET FORTH AT www.vishay.com/doc?91000
Application Note
www.vishay.com
Vishay Semiconductors
C++ Software Code for
VCNL4010, VCNL4020, and VCNL3020
Logging_Main4.txt
VCNL4010/4020/3020 Proximity/Ambient Light Sensor
library tested with mbed LPC1768 (ARM Cortex-M3 core) on www.mbed.org
Version: 1.2 01/2012
Demonstration #4:
Proximity Measurement and Ambient light Measurement in selftimed mode
Proximity with 31 measurements/s, Ambient light with 2 measurement/s
Interrupt waiting on proximity value > upper threshold limit
Product ID Revision Register: 33
IR LED Current: 20
Upper Threshold Value: 2570 cts
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
2492
2416
2479
2475
2483
2487
2437
2446
2454
2479
2423
cts
cts
cts
cts
cts
cts
cts
cts
cts
cts
cts
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
0
0
0
0
0
0
0
0
0
0
0
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
2483
2436
2407
2437
2407
2438
2451
2489
2432
2504
2504
2443
2424
2428
2481
cts
cts
cts
cts
cts
cts
cts
cts
cts
cts
cts
cts
cts
cts
cts
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
Proxi:
2538
2435
2521
2482
2533
2523
2544
2575
2632
2658
2671
2780
2754
2808
2863
2848
cts
cts
cts
cts
cts
cts
cts
cts
cts
cts
cts
cts
cts
cts
cts
cts
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
InterruptStatus:
0
0
0
0
0
0
0
1
1
1
1
1
1
1
1
1
Proxi:
Proxi:
2926 cts
2889 cts
From here the actual proximity offset count is printed as available
with the used application. It is about 2470 (± 60 cts).
Ambi: 1118
For every 15/16 proximity values, one ambient result is visible.
This is due to programs
PROX_MEASUREMENT_RATE_31
and
AMBI_PARA_MEAS_RATE_2
Ambi: 1075
With the evaluated offset count of 2470 above (30 measurements
averaged (see main.cpp)) and the definition of the high threshold to
be at offset cts + 100 cts = 2570, at 2575 cts this threshold is
exceeded and the interrupt is set.
What is also possible to see here:
The closer the hand, the higher the proximity counts AND the lower
the ambient counts.
This is due to shadowing created by the light source straight above.
APPLICATION NOTE
Ambi: 779
InterruptStatus: 1
InterruptStatus: 1
Logging_Main5.txt
The “main #5” shows the proximity on demand values. It is a modified version of “main #1” in order to be used with the
VCNL3020.
...
Proxi:
Proxi:
Proxi:
Proxi:
3012
2974
3003
3074
cts
cts
cts
cts
...
Revision: 07-Dec-12
Document Number: 84140
10
For technical questions, contact: [email protected]
THIS DOCUMENT IS SUBJECT TO CHANGE WITHOUT NOTICE. THE PRODUCTS DESCRIBED HEREIN AND THIS DOCUMENT
ARE SUBJECT TO SPECIFIC DISCLAIMERS, SET FORTH AT www.vishay.com/doc?91000
Application Note
www.vishay.com
Vishay Semiconductors
C++ Software Code for
VCNL4010, VCNL4020, and VCNL3020
The controller used for developing this code was an ARM Processor (NXP LPC1768), which is available on a so-called
“mbed Board” and can be ordered here: http://mbed.org/handbook/order.

More useful information is available under: www.mbed.org.
Microcontroller board: mbed NXP LPC1768
Rapid Prototyping for general microcontroller applications, Ethernet, USB and 32-bit ARM® Cortex™-M3 based designs


The I2C bus of the VCNL40x0 demo board is connected towards P28 (SDA) and P27 (SCL), as shown within the mbed
NXP LPC1768 board picture above.
APPLICATION NOTE
The Interrupt pin is not routed from the VCNL40x0 towards the mbed board because the red LED at the VCNL40x0 demo board
indicates this event.
LED0 indicates an available proximity measurement and LED1 an available ambient light measurement.









Revision: 07-Dec-12
Document Number: 84140
11
For technical questions, contact: [email protected]
THIS DOCUMENT IS SUBJECT TO CHANGE WITHOUT NOTICE. THE PRODUCTS DESCRIBED HEREIN AND THIS DOCUMENT
ARE SUBJECT TO SPECIFIC DISCLAIMERS, SET FORTH AT www.vishay.com/doc?91000
Application Note
www.vishay.com
Vishay Semiconductors
C++ Software Code for
VCNL4010, VCNL4020, and VCNL3020
APPLICATION NOTE
Copyright (c) 2012 Vishay GmbH, www.vishay.com
Author: Ds, version 1.2

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
files (the “software”), to deal in the software without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the software, and to permit persons to whom the software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIED OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Revision: 07-Dec-12
Document Number: 84140
12
For technical questions, contact: [email protected]
THIS DOCUMENT IS SUBJECT TO CHANGE WITHOUT NOTICE. THE PRODUCTS DESCRIBED HEREIN AND THIS DOCUMENT
ARE SUBJECT TO SPECIFIC DISCLAIMERS, SET FORTH AT www.vishay.com/doc?91000