VCNL4020
This VCNL4020 (About Modules) module is used to initialize and setup an Vishay VCNL4020 Integrated Proximity and Ambient Light Sensor with Infrared Emitter. For testing I used a NUCLEO-STM32F401/411 + VCNL4020 Eval Board.
Required Resources
This Module uses an I2C interface. 5V and 3.3V Power Supply is required. In addition one port can be used as interrupt in.
How to use my module
Below you find two examples how to use this module. You will find more examples in the web if you look for the Vishay VCNL40xx C/C++ Library. Examples and function Names are defined according to this Library. This will allow you to use also the features not covered in the examples below.
Example 1
This example initialise an I2C interface and connect the VCNL4020 to this interface. After 1 second the time-out function will set the current for the IR transmitter to 100mA. Then read Ambient light and Proximity.
Please be aware: if you read proximity you only get a value which corresponds to the reflected IR-light, not a real distance. Distance can be estimated and depends on surface of the material which is used for reflection! Each measurement needs at least 3 I2C transaction. On Demand Measurement can't be mixed with auto measurement!
Each time you press the button on the Board a new measurement will be done.
I2C1.setup({ sda:B9, scl:B8, bitrate:100000} );
var vcnl = require("VCNL4020").connect(I2C1);
setTimeout( function() {
vcnl.SetCurrent(10);
vcnl.logReg();
console.log( "Ambient" + vcnl.ReadAmbiOnDemand() );
console.log( "Proxi" + vcnl.ReadProxiOnDemand() );
}, 1000);
function io_interrupt() {
console.log( "Ambient" + vcnl.ReadAmbiOnDemand() );
console.log( "Proxi" + vcnl.ReadProxiOnDemand().toString(16) );
}
setWatch(io_interrupt, BTN1, {repeat:true, edge:"rising"});
Example 2
This example uses the auto measurement function. The sensor performs 2 Measurements per second. When reading the value from the sensor you always get the latest one. Advantage of this approach: Only one I2C transaction is required to get the value.
After download you fist have to save this program, before it works!
// Constants
var DEF = {
COMMAND_SELFTIMED_MODE_ENABLE : (0x01),
COMMAND_PROX_ENABLE : (0x02),
// Bits in Prox Measurement Rate register : (0x82),
PROX_MEASUREMENT_RATE_2 : (0x00), // DEFAULT
PROX_MEASUREMENT_RATE_4 : (0x01),
};
// variables
var ProxiValue;
var CommandValue;
var vcnl;
// Button Interrupt
function io_interrupt() {
console.log( "Proxi " + vcnl.ReadProxiValue().toString(16) );
}
setWatch(io_interrupt, BTN1, {repeat:true, edge:"rising"});
// init for execution out of reset
E.on('init', function () {
I2C1.setup({ sda:B9, scl:B8, bitrate:100000} );
vcnl = require("VCNL4020").connect(I2C1);
// Reset copmmand register
vcnl.SetCommandRegister (0x00);
// print information on screen
console.log("VCNL4010/4020/3020 Proximity/Ambient Light Sensor");
console.log("Product ID Revision Register:" + vcnl.ReadID().toString(16));
// Set current to 100mA
vcnl.SetCurrent (10);
console.log("IR LED Current: " + vcnl.ReadCurrent () );
// set proximity rate to 2/s
vcnl.SetProximityRate (DEF.PROX_MEASUREMENT_RATE_2);
// enable prox in selftimed mode
vcnl.SetCommandRegister (DEF.COMMAND_PROX_ENABLE | DEF.COMMAND_SELFTIMED_MODE_ENABLE);
}
);
Defines
Here you find all the defines for the VCNL 4020. You can copy this list into your project to have the correct parameters to call a function. Please consider that this will increase your project dramatically. A better approach might be to copy only those values which you really need. Alternate solution is to take the values and use them directly. This approach might cause code which is not so good to read and understand for others.
var DEF = {
// Bits in Command register (0x80)
COMMAND_ALL_DISABLE : (0x00),
COMMAND_SELFTIMED_MODE_ENABLE : (0x01),
COMMAND_PROX_ENABLE : (0x02),
COMMAND_AMBI_ENABLE : (0x04),
COMMAND_PROX_ON_DEMAND : (0x08),
COMMAND_AMBI_ON_DEMAND : (0x10),
COMMAND_MASK_PROX_DATA_READY : (0x20),
COMMAND_MASK_AMBI_DATA_READY : (0x40),
COMMAND_MASK_LOCK : (0x80),
// Bits in Product ID Revision Register : (0x81),
PRODUCT_MASK_REVISION_ID : (0x0f),
PRODUCT_MASK_PRODUCT_ID : (0xf0),
// Bits 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),
PROX_MASK_LED_CURRENT : (0x3f), // DEFAULT = 2
PROX_MASK_FUSE_PROG_ID : (0xc0),
// Bits 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),
AMBI_PARA_AUTO_OFFSET_ENABLE : (0x08), // DEFAULT enable
AMBI_MASK_PARA_AUTO_OFFSET : (0x08),
AMBI_PARA_MEAS_RATE_1 : (0x00),
AMBI_PARA_MEAS_RATE_2 : (0x10), // DEFAULT
AMBI_PARA_MEAS_RATE_3 : (0x20),
AMBI_PARA_MEAS_RATE_4 : (0x30),
AMBI_PARA_MEAS_RATE_5 : (0x40),
AMBI_PARA_MEAS_RATE_6 : (0x50),
AMBI_PARA_MEAS_RATE_8 : (0x60),
AMBI_PARA_MEAS_RATE_10 : (0x70),
AMBI_MASK_PARA_MEAS_RATE : (0x70),
AMBI_PARA_CONT_CONV_ENABLE : (0x80),
AMBI_MASK_PARA_CONT_CONV : (0x80), // DEFAULT disable
// Bits in Interrupt Control Register : (x89),
INTERRUPT_THRES_SEL_PROX : (0x00),
INTERRUPT_THRES_SEL_ALS : (0x01),
INTERRUPT_THRES_ENABLE : (0x02),
INTERRUPT_ALS_READY_ENABLE : (0x04),
INTERRUPT_PROX_READY_ENABLE : (0x08),
INTERRUPT_COUNT_EXCEED_1 : (0x00), // DEFAULT
INTERRUPT_COUNT_EXCEED_2 : (0x20),
INTERRUPT_COUNT_EXCEED_4 : (0x40),
INTERRUPT_COUNT_EXCEED_8 : (0x60),
INTERRUPT_COUNT_EXCEED_16 : (0x80),
INTERRUPT_COUNT_EXCEED_32 : (0xa0),
INTERRUPT_COUNT_EXCEED_64 : (0xc0),
INTERRUPT_COUNT_EXCEED_128 : (0xe0),
INTERRUPT_MASK_COUNT_EXCEED : (0xe0),
// Bits 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 : (0x0)
};
Reference
exports.connect = function (vcnl_i2c) { ... }
VCNL4020.prototype.SetCommandRegister = function (mCommand) { ... }
VCNL4020.prototype.ReadCommandRegister = function () { ... }
VCNL4020.prototype.ReadID = function () { ... }
VCNL4020.prototype.SetCurrent = function (Current) { ... }
VCNL4020.prototype.ReadCurrent = function () { ... }
VCNL4020.prototype.SetProximityRate = function (ProximityRate) { ... }
VCNL4020.prototype.SetAmbiConfiguration = function (AmbiConfiguration) { ... }
VCNL4020.prototype.SetInterruptControl = function (InterruptControl) { ... }
VCNL4020.prototype.ReadInterruptControl = function () { ... }
VCNL4020.prototype.SetInterruptStatus = function (InterruptStatus) { ... }
VCNL4020.prototype.SetModulatorTimingAdjustment = function (ModulatorTimingAdjustment) { ... }
VCNL4020.prototype.ReadInterruptStatus = function () { ... }
VCNL4020.prototype.ReadProxiValue = function () { ... }
VCNL4020.prototype.ReadAmbiValue = function () { ... }
VCNL4020.prototype.SetLowThreshold = function (LowThreshold) { ... }
VCNL4020.prototype.SetHighThreshold = function (HighThreshold) { ... }
VCNL4020.prototype.ReadProxiOnDemand = function () { ... }
VCNL4020.prototype.ReadAmbiOnDemand = function () { ... }
/* logReg
* Dump all Control registers
*/
VCNL4020.prototype.logReg = function () { ... }
/* r
* read one byte of data from a register.
*/
VCNL4020.prototype.r = function (addr) { ... }
/* r2
* reading two bytes will read two consecutive registers!
*/
VCNL4020.prototype.r2 = function (addr) { ... }
/* w
* write data to a register
*/
VCNL4020.prototype.w = function (addr, data) { ... }
This page is auto-generated from GitHub. If you see any mistakes or have suggestions, please let us know.