Help for porting VCNL40x0 library to arduino


I would like to port the mbed library of the VCNL40x0 sensor family to Arduino and provide it for the community. I know that there is an Adafruit VCNL4010 library but it does not support all functions of the sensor, for example interrupts, high threshold, low threshold.

I have already started to port it but it seems to be to difficult for a beginner like me. I would be very grateful if you could show me how to port one of the read and set methods so I can do it for the rest and learn how to do it.

My progress so far:




My code has lots of errors and I don't really know where and how to continue. So I really appreciate your help! :slight_smile:

I don’t really know where and how to continue.

Well, dumping your code in the rubbish bin was probably not the correct way to start. Use Reply, not the Quick Reply field, and post you code AND errors HERE.

Thank you very much for your reply! :slight_smile:

The code compiles, but I think that

#define VCNL_ProximityRate PROX_MEASUREMENT_RATE_2


vcnl.setProximityRate (VCNL_ProximityRate);

is not working correctly.

I don’t really know how to access values which are defined in the .h file?

It would be awesome to get it working like the Embed Library and I really appreciate any help!

I can only post the library as an attachment because it is to big

vcnl4010test.ino (858 Bytes)

Adafruit_VCNL4010.cpp (7.94 KB)

Adafruit_VCNL4010.h (5.46 KB)

I have made some progress, the sketch compiles but I think it is not working in the way it should and I think I know the problem.

For example I have this function:


I am able to set the current with it and this seems to work because   Serial.println(vcnl.readCurrent()); returns the correct value.

But if I use one of the other functions I am making something wrong. For example vcnl.setCommandRegister (COMMAND_ALL_DISABLE);
COMMAND_ALL_DISABLE is declared in the .h file: #define COMMAND_ALL_DISABLE 0x00
So this should pass 0x00 to the function in the .cpp file.
I would be really grateful if you could show me an example how to achieve this!

I am also not sure if

void Adafruit_VCNL4010::setHighThreshold (uint8_t HighThreshold) {

	unsigned char LoByte=0, HiByte=0;
    LoByte = (unsigned char)(HighThreshold & 0x00ff);
    HiByte = (unsigned char)((HighThreshold & 0xff00)>>8);
	write8(VCNL4010_INTERRUPT_HIGH_THRES+1, LoByte);

this is correct. Especially the LoByte and HighByte lines.

Adafruit_VCNL4010.cpp (8.2 KB)

Adafruit_VCNL4010.h (5.43 KB)

vcnl4010test.ino (1.49 KB)