VCNL4040 linear Proximity Output

Hello,
i have a problem and maybe someone can help me.
I want to use a VCNL4040 Proximity Sensor for measuring a linear movement, later thios movement should get translated into midi CC data.
The Problem is, the Output of the Proximity Sensor is not linear, i guess its maybe exponential? I could find anything about this in the datasheet, but i have to admit that i dont understand everything in this sheet..
anyway, maybe some knows how i can get a linear output from this sensor or how i can translate it with math into linear output?
I Use the Adafruit VCNL4040 Library.

Thanks Lukas

So you use a look up table to convert your sensor output into the range of values you want to use.

I think you will be better off using a time of flight sensor for distance like this one.
Time Of Flight sensor.

I have used one of these to send MIDI pitch bend messages, but the big problem is the beam width of your sensor making it influenced by changes in background movement as well as your intended target.

Hi Grampy_Mike,
thank you for your response!
Yes right, i want to convert the sensor output in the range value of midiCC.

The Sensor is built into a case, and i want to measure a linear movement of 35mm (contactless). So the Sensor has to be very precise in this range from 0mm to 35mm.

After my experiments yesterday, i think the VCNL4040 Proximity measuremtent output is exponential the numbers are rising super high the closer i approach to the sensor.

I guess the Time Of Flight sensor is not precise enough for my apllication, right?

Well given you want the physical range to be zero to 35 mm then yes a TOF sensor will not work that close. There is a minimum distance.

Given that MIDI control change messages take only the numbers from 0 to 127 then you need to use the map function to cut it down to size once you have linearised the sensor's output.

So find out if it is a exponential function by plotting a few points on a graph. Use either log / lin graph paper to get a straight line graph or take the log ( base 10 or base e) before you plot it on a linear graph. Mabe you could do this with a spread sheet.

Once you have the exact curve worked out then simply apply the inverse of this transform to linearise your sensor reading. Then apply the map function call to split it into the range of 0 to 127.

ok cool, that's pretty much what I tried last night, but I wasn't sure if that was the right approach. The result is ok i would say, it's not exactly linear but close.

Unfortunately, it turned out that the sensor reads quite slowly, or it has vllt. rather to do with the library. Do you have any experience with the Adafruit VCNL4040 library?

Sorry no. I haven't even used the VCNL4040. I just took a look at the data sheet and I note that it not a chip for measuring distances but a proximity sensor that gives you an output which shows if it is greater than or less than a specific distance. It also has some hysteresis as well.

So if you are looking for a continuous readout of the distance maybe the library gives you that function by moving the thresholds up and down and reporting back where it found the trigger point. This might explain why the library seems slow.

I notice that the 4040 will run I2C at 400KHz, do you know if you are using it at that speed? If not there might be a four fold speed increase you can get.

Hey Grumpy Mike,
thanks for your help and sorry for the late reply.The latency came from

vcnl4040.setProximityLEDDutyCycle(VCNL4040_LED_DUTY_1_320);

setting,
honestly i don't know what this setting is for, but it made the program extremely slow and that's why i didn't find the error at first, in order for the new setting to be applied, the teensy has to be restarted.
Now it works very well and smoothly.
To linearize the values I used the log() function from the math.h library, the result are relatively linear values.
I also connected two vcnl4040 sensors via a TCA9548A multiplexer at the same time and mapped the respective signal as midi. I would like to share the code here.
Thanks for your help :slight_smile:
VCNL4040 with Multiplexer and log function.ino (8.0 KB)

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.