0

proximity sensor VCNL4040 I2C read always gives zeros
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

proximity sensor VCNL4040 I2C read always gives zeros

by PHAM on Thu Jan 07, 2016 11:55 am

Hello,

I'm developing a portable device using proximity sensor VCNL4040. In fact, the previous prototype uses VCNL4010, and I had no problem with it.
Changing to VCNL4040, I could not communicate with the sensor, even though there is ACK when calling the slave address of VCNL4040.

According to VCNL4040 datasheet, reading from the sensor does not require a Stop condition as for VCNL4010.
I tried to read the device ID from VCNL4040 with the code below:
Code: Select all | TOGGLE FULL SIZE
// Read ID from VNCL4040
  int rev_4040 = 0;
  Wire.beginTransmission(96); // transmit to device #96 (0x60)
  Wire.write(byte(0x0C)); // VCNL4040 ID
  Wire.requestFrom(96, 2);    // request 2 bytes from slave device #96
  if (2 <= Wire.available()) { // if two bytes were received
    rev_4040 = Wire.read();  // receive high byte (overwrites previous reading)
    rev_4040 = rev_4040 << 8;    // shift high byte to be high 8 bits
    rev_4040 |= Wire.read(); // receive low byte as lower 8 bits
    Serial.print("Read from 4040: ");
    Serial.println(rev_4040);   // print the reading
  }


For comparison, the code for reading ID from VCNL4010 on the same I2C bus is:
Code: Select all | TOGGLE FULL SIZE
// Read ID from VNCL4010
  int rev_4010 = 0;
  Wire.beginTransmission(19); // transmit to device #19 (0x13)
  Wire.write(byte(0x81)); // VCNL4040 ID
  Wire.endTransmission();
  Wire.requestFrom(19, 1);    // request 2 bytes from slave device #96
  while (!Wire.available());
  rev_4010 = Wire.read();
  Serial.print("Read from 4010: ");
  Serial.println(rev_4010);   // print the reading


The output is:
Read from 4040: 0
Read from 4010: 33
screen1.jpg
screen1.jpg (209.87 KiB) Viewed 972 times


I have no idea why the read from all registers of VCNL4040 (0x00 to 0x0C) always gives zeros while I can correctly communicate with other I2C slave on the same bus.
I need your help!
Phong

PHAM
 
Posts: 4
Joined: Thu Jan 07, 2016 11:31 am

Re: proximity sensor VCNL4040 I2C read always gives zeros

by adafruit_support_rick on Fri Jan 08, 2016 12:53 pm

You've tried reading from the 4040 the same way you're reading from the 4010? That didn't work?

adafruit_support_rick
 
Posts: 35095
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: proximity sensor VCNL4040 I2C read always gives zeros

by PHAM on Mon Jan 18, 2016 7:06 am

We finally solved the problem. Thanks anyway!
In fact, the VCNL4040 is different from older VCNL40xx. It does not require a STOP for reading from the sensor. For repeat START in reading, we added wire.endtransmission(false). We also figured out there was a soldering problem. Our bad!
Thanks to people from Vishay, who supplied us useful stuffs, we could solve our problem.
Cheers!

PHAM
 
Posts: 4
Joined: Thu Jan 07, 2016 11:31 am

Re: proximity sensor VCNL4040 I2C read always gives zeros

by Shayo on Fri Feb 19, 2016 2:14 am

Hi PHAM,

Can you send the full code you managed to get the 4040 to work?

Thanks,

Shay

Shayo
 
Posts: 12
Joined: Sat Jan 23, 2016 10:59 am

Re: proximity sensor VCNL4040 I2C read always gives zeros

by PHAM on Tue Mar 01, 2016 8:00 am

Here you are:
Code: Select all | TOGGLE FULL SIZE
void initVCNL4040() {
  // Enable PS on VCNL4040
  Wire.beginTransmission(96); // transmit to device #96 (0x60)
  Wire.write(byte(0x03)); // command code 0x03: PS_CONF1 and PS_CONF2
  Wire.write(byte(0x00)); // PS_CONF1 set bit 7 (PS_SD) to 0 to power on PS
  Wire.write(byte(0x00)); // PS_CONF2
  Wire.endTransmission();
}

int readVCNL404() {
  int rev_4040 = 0;
  // Read PS data
  Wire.beginTransmission(96); // transmit to device #96 (0x60)
  Wire.write(byte(0x08)); // VCNL4040 ID
  Wire.endTransmission(false);
  Wire.requestFrom(96, 2);    // request 2 bytes from slave device #96
  if (2 <= Wire.available()) { // if two bytes were received
    while (!Wire.available());
    rev_4040 = Wire.read();  // receive high byte (overwrites previous reading)
    while (!Wire.available());
    int tmp = Wire.read();
    tmp = tmp << 8;    // shift high byte to be high 8 bits
    rev_4040 |= tmp; // receive low byte as lower 8 bits
    Serial.print("Read from 4040: ");
    Serial.println(rev_4040);  // print the reading
  }
  return rev_4040;
}


Hope this helps!
Cheers,
Phong

PHAM
 
Posts: 4
Joined: Thu Jan 07, 2016 11:31 am

Re: proximity sensor VCNL4040 I2C read always gives zeros

by kavee on Thu Jun 09, 2016 12:29 am

Yes, PHAM is correct. The chip does not need stop condition. Wire.endTransmission(false) does the trick.

kavee
 
Posts: 1
Joined: Thu Jun 09, 2016 12:28 am

Re: proximity sensor VCNL4040 I2C read always gives zeros

by Dinoden on Tue Oct 18, 2016 2:03 pm

Hi PHAM.

Is that program for the VCNL4040 complete? I am having the same problem and saw your post. I copied the program on a LEONARDO and I get programming errors. Ive included "wire.h". Is there something else I am missing? Below are my errors.

Sorry I am new to the Arduino.
Thanks,
Dinoden


*************************************************************************************************************************************************


Arduino: 1.6.11 (Windows 10), Board: "Arduino Leonardo"

C:\Users\dennis\AppData\Local\Temp\ccdibUWU.ltrans0.ltrans.o: In function `main':

ccdibUWU.ltrans0.o:(.text.startup+0x17a): undefined reference to `setup'

ccdibUWU.ltrans0.o:(.text.startup+0x182): undefined reference to `loop'

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino Leonardo.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Dinoden
 
Posts: 5
Joined: Tue Oct 18, 2016 1:54 pm

Re: proximity sensor VCNL4040 I2C read always gives zeros

by adafruit_support_rick on Tue Oct 18, 2016 2:56 pm

PHAM posted a function, not complete program. You still need to write a program.

adafruit_support_rick
 
Posts: 35095
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: proximity sensor VCNL4040 I2C read always gives zeros

by Dinoden on Tue Oct 18, 2016 3:53 pm

Hi PHAM.
Okay sorry. I did not know I'm very new to Arduino. I don't know what's missing.
Thanks
Dennis

Dinoden
 
Posts: 5
Joined: Tue Oct 18, 2016 1:54 pm

Re: proximity sensor VCNL4040 I2C read always gives zeros

by Dinoden on Tue Oct 18, 2016 11:00 pm

Hi PHAM.

I tried for a bit and got nothing.
Is it possible to receive the working sketch?

Sorry to ask but I would be very grateful!

Dinoden
 
Posts: 5
Joined: Tue Oct 18, 2016 1:54 pm

Re: proximity sensor VCNL4040 I2C read always gives zeros

by PHAM on Thu Oct 20, 2016 8:20 am

Hi Dinoden,

Sorry for my late reponse.
Here is the code to test the VCNL4040. Connect one LED to pin 9 to test if there is nearby object or not.
Hope this helps.
Cheers,

Code: Select all | TOGGLE FULL SIZE
#include <Wire.h>
#define ledPin 9
long previousMillis = 0;
boolean ledStat = true;

void setup() {

  Wire.begin();                // join i2c bus (address optional for master)
  Serial.begin(9600);          // start serial communication at 9600bps

  Serial.println("hello");
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  delay(500);
  digitalWrite(ledPin, LOW);

  initVCNL4040();
}

void loop() {
  int rev_4040 = readVCNL404();

  if (rev_4040 > 50) digitalWrite(ledPin, HIGH);
  else  digitalWrite(ledPin, LOW);

  Wire.beginTransmission(8); // transmit to device #8
  Wire.write("VCNL4040 is ");        // sends five bytes
  Wire.write(rev_4040);              // sends one byte
  Wire.endTransmission();    // stop transmitting

  delay(10);
}
// =================================================
void initVCNL4040() {
  Wire.beginTransmission(96); // transmit to device #96 (0x60)
  Wire.write(byte(0x03));
  Wire.write(byte(0x00));
  Wire.write(byte(0x00));
  Wire.endTransmission();
}
int readVCNL404() {
  int rev_4040 = 0;
  Wire.beginTransmission(96); // transmit to device #96 (0x60)
  Wire.write(byte(0x08)); // VCNL4040 ID
  Wire.endTransmission(false);
  Wire.requestFrom(96, 2);    // request 2 bytes from slave device #96
  if (2 <= Wire.available()) { // if two bytes were received
    while (!Wire.available());
    rev_4040 = Wire.read();  // receive high byte (overwrites previous reading)
    while (!Wire.available());
    int tmp = Wire.read();
    tmp = tmp << 8;    // shift high byte to be high 8 bits
    rev_4040 |= tmp; // receive low byte as lower 8 bits
    Serial.print("Read from 4040: ");
    Serial.println(rev_4040);  // print the reading
  }
  return rev_4040;
}

PHAM
 
Posts: 4
Joined: Thu Jan 07, 2016 11:31 am

Re: proximity sensor VCNL4040 I2C read always gives zeros

by Dinoden on Thu Oct 20, 2016 11:40 am

Hi PHAM.
Great no problem.
I cant wait to give it a try!

Dinoden
 
Posts: 5
Joined: Tue Oct 18, 2016 1:54 pm

Re: proximity sensor VCNL4040 I2C read always gives zeros

by Dinoden on Thu Oct 20, 2016 1:23 pm

Hi PHAM.

Thank you very much for your help. Works great!

Dinoden
 
Posts: 5
Joined: Tue Oct 18, 2016 1:54 pm

Re: proximity sensor VCNL4040 I2C read always gives zeros

by ronaldbenve on Wed Jul 24, 2019 10:32 am

Thank you, I had the same problem, your solution works. VCNL4040 REQUIRES a start without stop after the write command.

ronaldbenve
 
Posts: 1
Joined: Wed Jul 24, 2019 10:30 am

Re: proximity sensor VCNL4040 I2C read always gives zeros

by Gbouna on Mon Jul 29, 2019 8:22 am

Thank you for the code,it works great.

please i will like to know if anyone has tried using either SoftI2CMaster or SoftwareWire with VCNL4040, I am new to I2C and i have many examples on the web but i dont know how to successfully implement it with VCNL4040. Please if anyone has done it before, kindly help me with the complete code, thank you.

The reason why i need it is because i want to be able to use other pins of Arduino boards instead of the dedicated I2C pins to communicate with the VCNL4040. Either the above two ways or any other ways that will help me achieve this, please if anyone has an idea with the code help me.

Gbouna
 
Posts: 1
Joined: Mon Jul 29, 2019 8:13 am

Please be positive and constructive with your questions and comments.