VCNL4000 with Leonardo

I've been having trouble using the VCNL4000 Light sensor / proximity sensor with a Leonardo. It works just fine for me when I switch to UNO.

And yes, I know the SCL and SDA pins are a different setup.

On the UNO, I connect:
Vin to Arduino 5v
Gnd to Arduino Ground
SDA to A4
SCL to A5
3.3 not used

On the Leonardo:
Vin to Arduino 5v
Gnd to Arduino Ground
SDA to Arduino SDA
SCL to Arduino SCL
3.3 not used

I also tried using the software serial setup when using the Leonardo. I think I did get it to work a bit, but it would crash after a few readings. Seems pretty stable on the Uno. For the time being, I'm just using the Uno, but I'd like to know if there's something else I could be doing to get the Leonardo to work.

What sketch are you using? If you use a library provide a link to it.

I also tried using the software serial setup when using the Leonardo.

I don't see where the SoftwareSerial could be of any help with an I2C sensor. Please explain.

Pylon,

I'm using the adafruit example sketch. I'll include it at the bottom of this post, because it's pretty long.
I've also tried a sketces from bildr and sparkfun.

I tried using SoftwareSerial because I know the Leonardo handles serial communication differently and I thought it might help.

Here's the full code (which works fine with an Uno):

// Downloaded from GitHub - adafruit/Adafruit-VCNL40X0-PCB: Arduino code for the VCNL4000 all-in-one proximity/light sensor

// Example sketch for talking to the VCNL4000 i2c proximity/light sensor
// Written by Adafruit! Public domain.
// To use: Connect VCC to 3.3-5V (5V is best if it is available), GND to
// ground, SCL to i2c clock (on classic arduinos, Analog 5), SDA
// to i2c data (on classic arduinos Analog 4). The 3.3v pin is
// an ouptut if you need 3.3V
// This sensor is 5V compliant so you can use it with 3.3 or 5V micros

// You can pick one up at the Adafruit shop: VCNL4010 Proximity/Light sensor : ID 466 : $7.50 : Adafruit Industries, Unique & fun DIY electronics and kits

#include <Wire.h>

// the i2c address
#define VCNL4000_ADDRESS 0x13

// commands and constants
#define VCNL4000_COMMAND 0x80
#define VCNL4000_PRODUCTID 0x81
#define VCNL4000_IRLED 0x83
#define VCNL4000_AMBIENTPARAMETER 0x84
#define VCNL4000_AMBIENTDATA 0x85
#define VCNL4000_PROXIMITYDATA 0x87
#define VCNL4000_SIGNALFREQ 0x89
#define VCNL4000_PROXINITYADJUST 0x8A

#define VCNL4000_3M125 0
#define VCNL4000_1M5625 1
#define VCNL4000_781K25 2
#define VCNL4000_390K625 3

#define VCNL4000_MEASUREAMBIENT 0x10
#define VCNL4000_MEASUREPROXIMITY 0x08
#define VCNL4000_AMBIENTREADY 0x40
#define VCNL4000_PROXIMITYREADY 0x20

void setup() {
Serial.begin(9600);

Serial.println("VCNL");
Wire.begin();

uint8_t rev = read8(VCNL4000_PRODUCTID);

if ((rev & 0xF0) != 0x10) {
Serial.println("Sensor not found :(");
while (1);
}

write8(VCNL4000_IRLED, 20); // set to 20 * 10mA = 200mA
Serial.print("IR LED current = ");
Serial.print(read8(VCNL4000_IRLED) * 10, DEC);
Serial.println(" mA");

//write8(VCNL4000_SIGNALFREQ, 3);
Serial.print("Proximity measurement frequency = ");
uint8_t freq = read8(VCNL4000_SIGNALFREQ);
if (freq == VCNL4000_3M125) Serial.println("3.125 MHz");
if (freq == VCNL4000_1M5625) Serial.println("1.5625 MHz");
if (freq == VCNL4000_781K25) Serial.println("781.25 KHz");
if (freq == VCNL4000_390K625) Serial.println("390.625 KHz");

write8(VCNL4000_PROXINITYADJUST, 0x81);
Serial.print("Proximity adjustment register = ");
Serial.println(read8(VCNL4000_PROXINITYADJUST), HEX);

// arrange for continuous conversion
//write8(VCNL4000_AMBIENTPARAMETER, 0x89);

}

uint16_t readProximity() {
write8(VCNL4000_COMMAND, VCNL4000_MEASUREPROXIMITY);
while (1) {
uint8_t result = read8(VCNL4000_COMMAND);
//Serial.print("Ready = 0x"); Serial.println(result, HEX);
if (result & VCNL4000_PROXIMITYREADY) {
return read16(VCNL4000_PROXIMITYDATA);
}
delay(1);
}
}

void loop() {

// read ambient light!
write8(VCNL4000_COMMAND, VCNL4000_MEASUREAMBIENT | VCNL4000_MEASUREPROXIMITY);

while (1) {
uint8_t result = read8(VCNL4000_COMMAND);
//Serial.print("Ready = 0x"); Serial.println(result, HEX);
if ((result & VCNL4000_AMBIENTREADY)&&(result & VCNL4000_PROXIMITYREADY)) {

Serial.print("Ambient = ");
Serial.print(read16(VCNL4000_AMBIENTDATA));
Serial.print("\t\tProximity = ");
Serial.println(read16(VCNL4000_PROXIMITYDATA));
break;
}
delay(10);
}

delay(100);
}

// Read 1 byte from the VCNL4000 at 'address'
uint8_t read8(uint8_t address)
{
uint8_t data;

Wire.beginTransmission(VCNL4000_ADDRESS);
#if ARDUINO >= 100
Wire.write(address);
#else
Wire.send(address);
#endif
Wire.endTransmission();

delayMicroseconds(170); // delay required

Wire.requestFrom(VCNL4000_ADDRESS, 1);
while(!Wire.available());

#if ARDUINO >= 100
return Wire.read();
#else
return Wire.receive();
#endif
}

// Read 2 byte from the VCNL4000 at 'address'
uint16_t read16(uint8_t address)
{
uint16_t data;

Wire.beginTransmission(VCNL4000_ADDRESS);
#if ARDUINO >= 100
Wire.write(address);
#else
Wire.send(address);
#endif
Wire.endTransmission();

Wire.requestFrom(VCNL4000_ADDRESS, 2);
while(!Wire.available());
#if ARDUINO >= 100
data = Wire.read();
data <<= 8;
while(!Wire.available());
data |= Wire.read();
#else
data = Wire.receive();
data <<= 8;
while(!Wire.available());
data |= Wire.receive();
#endif

return data;
}

// write 1 byte
void write8(uint8_t address, uint8_t data)
{
Wire.beginTransmission(VCNL4000_ADDRESS);
#if ARDUINO >= 100
Wire.write(address);
Wire.write(data);
#else
Wire.send(address);
Wire.send(data);
#endif
Wire.endTransmission();
}

First: please edit your post and insert code tags! The forum system may hide some parts of it without these tags.

That part

Wire.requestFrom(VCNL4000_ADDRESS, 1);
while(!Wire.available());

is a bad idea (I know the code is not from you, it was a bad idea of the original programmer) because this is an endless loop if the sensor is not answering or if it has another problem. Remove the while line completely, it's simply wrong in this sketch. The Wire.requestFrom() already waits until the data is received or an error occurred.

What's the output of this sketch? Does it freeze after writing "VNCL" to the serial interface?
Can you post a photo of your setup (with the Leonardo)?

I tried using SoftwareSerial because I know the Leonardo handles serial communication differently and I thought it might help.

That's also a bad idea. For one Leonardo typically has less problems with serial communication because it has a separate hardware serial interface which is already used for the USB connection on the UNO (and other Arduinos), for the other, the UART (the asynchronous serial interface) is not involved in contacting the sensor. The I2C interface used for this is a synchronous serial interface which is completely separate from the UART.

Pylon,

Thanks for the tip about removing the While Loop.
The code seems to be working correctly on Leonardo now. I think it was a hardware problem. A combination of a bad sensor and my unfamiliarity with the Leonardo's virtual serial port. Seems to be running smoothly now.