How to use an ESP32 development board to read from a VCNL4010 Proximity/Light sensor

If you have a Proximity/Light sensor, you will be able to detect how close an object is. When your range of detection is within 200mm, you can use the VCNL4010 Proximity/Light sensor.

Given that characteristic, you can use the VCNL4010 Proximity/Light sensor as a contactless switch to trigger some action.

For example, you can use it in a soap dispenser that will dispense some soap when a palm is near the nozzle.

In case you need it, this is how to use an ESP32 development board to read from a VCNL4010 Proximity/Light sensor.

VCNL4010 Proximity/Light sensor

Soldering the GPIO header pins onto the VCNL4010 Proximity/Light sensor

First, solder the GPIO header pins onto the VCNL4010 Proximity/Light sensor:
VCNL4010 Proximity/Light sensor with GPIO pins soldered on it

Connecting your VCNL4010 Proximity/Light sensor to your ESP32 development board

Once you had soldered the GPIO pins onto your VCNL4010 Proximity/Light sensor, connect it to your ESP32 development board:
VCNL4010 Proximity/Light sensor with Vin, GND, SCL and SDA pins connected to GPIO cables
Underside of Espressif ESP32 devkit v4 with GPIO wires connected to GND, 5v, GPIO21 and GPIO22 pins

As shown above, I had connected:

  • GND pin of VCNL4010 Proximity/Light sensor to one of the GND pin on the ESP32 development board
  • Vin pin of VCNL4010 Proximity/Light sensor to one of the 5v pin on the ESP32 development board
  • SDA pin of VCNL4010 Proximity/Light sensor to GPIO21 on the ESP32 development board
  • SCL pin of VCNL4010 Proximity/Light sensor to GPIO22 pin on the ESP32 development board

Writing an Arduino Sketch to read from a VCNL4010 Proximity/Light sensor connected to ESP32 board

At this point in time, you can write the software to read from the VCNL4010 Proximity/Light sensor.

If you enable ESP32 Development on Arduino IDE, then you can upload an Arduino Sketch to your ESP32 board to read from the VCNL4010 Proximity/Light sensor.

Installing the Arduino Library to read from a VCNL4010 Proximity/Light sensor

After you had started your Arduino IDE, go to Tools -> Manage Libraries.... When you do that, the Library Manager window will appear.

Given that, search for Adafruit VCNL4010 and install the Adafruit_VCNL4010 library:
Searching for Adafruit VCNL4010 in Arduino IDE 1.8.9 libary manager

Arduino sketch to read from a VCNL4010 Proximity/Light sensor connected to an ESP32 development board

Once you had installed the Adafruit VCNL4010 library, you will be able to read from a VCNL4010 Proximity/Light sensor. In order to give us a reference on how to do so, the library included an example sketch:

#include <Wire.h>
#include <Adafruit_VCNL4010.h>

Adafruit_VCNL4010 vcnl;

void setup() {
  Serial.begin(9600);
  Serial.println("VCNL4010 test");

  if (! vcnl.begin()){
    Serial.println("Sensor not found :(");
    while (1);
  }
  Serial.println("Found VCNL4010");
}


void loop() {
   Serial.print("Ambient: "); Serial.println(vcnl.readAmbient());
   Serial.print("Proximity: "); Serial.println(vcnl.readProximity());
   delay(100);
}

So what is the above code doing?

When the setup function is called, we configure the board's serial to transmit data at a baud rate of 9600. After that, we call vcnl.begin() to start the proximity/light sensor. If we can successfully start the proximity/light sensor, we then send an indication to the board's serial.

Once we have prepared the instance of Adafruit_VCNL4010, we use it in the loop function to read the ambient and proximity values detected by the sensor.

While ambient is proportional to the brightness level, proximity value is inversely proportional to the distance an object is away from the sensor.

For example, if you cover the sensor with your hand, vcnl.readAmbient() return 0 and vcnl.readProximity() return 65535.

Sample output of Arduino Sketch to read from a VCNL4010 Proximity/Light sensor connected to an ESP32 development board

When I ran the sample sketch for a while, the following output was captured in my serial monitor: Adafruit_VNCL4010 example program sample output on Serial Monitor of Arduino IDE

How to use an ESP32 development board to read from a VCNL4010 Proximity Light sensor

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.