I am trying to use to VCNL 4000’s simultaneously. I can get one to work, but when I use the multiplexer to get the one on channel one to work the code uploads properly but nothing happens in the serial port.
The code I have for only channel one is:
#include <Wire.h>
#define VCNL4000_ADDRESS 0x13 //I2C Address of the board
// VCNL4000 Register Map
#define COMMAND_0 0x80 // starts measurments, relays data ready info
#define PRODUCT_ID 0x11 // product ID/revision ID, should read 0x11
#define IR_CURRENT 0x83 // sets IR current in steps of 10mA 0-200mA
#define AMBIENT_PARAMETER 0x84 // Configures ambient light measures
#define AMBIENT_RESULT_MSB 0x85 // high byte of ambient light measure
#define AMBIENT_RESULT_LSB 0x86 // low byte of ambient light measure
#define PROXIMITY_RESULT_MSB 0x87 // High byte of proximity measure
#define PROXIMITY_RESULT_LSB 0x88 // low byte of proximity measure
#define PROXIMITY_FREQ 0x89 // Proximity IR test signal freq, 0-3
#define PROXIMITY_MOD 0x8A // proximity modulator timing
#define mux_address 0xE0 // address of the multiplexer
#define channel_0 0xE4 // address for the channel 0 control register
#define channel_1 0xE5 // address for the channel 1 control register
int proximity;
void setup(){
Serial.begin(9600); // Serial's used to debug and print data
Wire.begin(); // initialize I2C stuff
initVCNL4000_0(); //initilize and setup the board on channel 0
}
void loop(){
unsigned int proximity_0 = readProximity_0();
Serial.println(proximity_0);
delay(100); //Just here to slow down the printing
//note that the readings take about 100ms to execute
}
void initVCNL4000_0()
{
byte temp = readVCNLByte(0x81);
if (temp != 0x11){ // Product ID Should be 0x11
Serial.print("initVCNL4000 failed to initialize");
Serial.println(temp, HEX);
}
/*VNCL400 init params
Feel free to play with any of these values, but check the datasheet first!*/
writeVCNLByte(0x84, 0x0F); // Configures ambient light measures - Single conversion mode, 128 averages
writeVCNLByte(0x83, 10); // sets IR current in steps of 10mA 0-200mA --> 200mA
writeVCNLByte(0x89, 2); // Proximity IR test signal freq, 0-3 - 781.25 kHz
writeVCNLByte(0x8A, 0x81); // proximity modulator timing - 129, recommended by Vishay
}
unsigned int readProximity_0()
{
// readProximity() returns a 16-bit value from the VCNL4000's proximity data registers
byte temp = readVCNLByte(0x80);
writeVCNLByte(0x80, temp | 0x08); // command the sensor to perform a proximity measure
while(!(readVCNLByte(0x80)&0x20)); // Wait for the proximity data ready bit to be set
unsigned int data = readVCNLByte(0x87) << 8;
data |= readVCNLByte(0x88);
return data;
}
void writeVCNLByte(byte address, byte data){
// writeVCNLByte(address, data) writes a single byte of data to address
Wire.beginTransmission(mux_address);
Wire.write(channel_0);
Wire.write(VCNL4000_ADDRESS);//Wire.beginTransmission(VCNL4000_ADDRESS);
Wire.write(address);
Wire.write(data);
Wire.endTransmission();
}
byte readVCNLByte(byte address){
// readByte(address) reads a single byte of data from address
Wire.beginTransmission(mux_address);
Wire.write(channel_0);
Wire.write(VCNL4000_ADDRESS);//Wire.beginTransmission(VCNL4000_ADDRESS);
Wire.write(address);
Wire.endTransmission();
delayMicroseconds(10000);
Wire.requestFrom(VCNL4000_ADDRESS, 1);
while(!Wire.available());
byte data = Wire.read();//gets 1 byte of data from request from
return data;
}
The data sheet for the multiplexer is attached.
PCA9540B.pdf (356 KB)