Using the VCNL4200 breakout with Arduino involves wiring up the breakout to your Arduino-compatible microcontroller, installing the Adafruit_VCNL4200 library, and running the provided example code.
Wiring
Wire as shown for a 5V board like an Uno. If you are using a 3V board, like an Adafruit Feather, wire the board's 3V pin to the sensor VIN.
Here is an Adafruit Metro wired up to the sensor using the STEMMA QT connector:
-
Board 5V to sensor VIN (red wire)
-
Board GND to sensor GND (black wire)
-
Board SCL to sensor SCL (yellow wire)
- Board SDA to sensor SDA (blue wire)
Here is an Adafruit Metro wired up using a solderless breadboard:
-
Board 5V to sensor VIN (red wire)
-
Board GND to sensor GND (black wire)
-
Board SCL to sensor SCL (yellow wire)
- Board SDA to sensor SDA (blue wire)
Library Installation
You can install the Adafruit_VCNL4200 library for Arduino using the Library Manager in the Arduino IDE.
Click the Manage Libraries ... menu item, search for Adafruit_VCNL4200, and select the Adafruit VCNL4200 library:
If asked about dependencies, click "Install all".
If the "Dependencies" window does not come up, then you already have the dependencies installed.
If the dependencies are already installed, you must make sure you update them through the Arduino Library Manager before loading the example!
// SPDX-FileCopyrightText: 2024 ladyada for Adafruit Industries // // SPDX-License-Identifier: MIT #include "Adafruit_VCNL4200.h" Adafruit_VCNL4200 vcnl4200; void setup() { Serial.begin(115200); while (!Serial) { delay(10); // wait for native USB } Serial.println("Adafruit VCNL4200 ALS simple test"); if (!vcnl4200.begin()) { Serial.println("Could not find a valid VCNL4200 sensor, check wiring!"); while (1) { delay(10); } } Serial.println("VCNL4200 found!"); vcnl4200.setALSshutdown(false); vcnl4200.setALSIntegrationTime(VCNL4200_ALS_IT_100MS); vcnl4200.setALSPersistence(VCNL4200_ALS_PERS_2); vcnl4200.setProxShutdown(false); vcnl4200.setProxHD(false); vcnl4200.setProxLEDCurrent(VCNL4200_LED_I_200MA); vcnl4200.setProxIntegrationTime(VCNL4200_PS_IT_8T); } void loop() { // Read the proximity sensor data uint16_t proxData = vcnl4200.readProxData(); Serial.print("Prox Data: "); Serial.println(proxData); // Read the ambient light sensor (ALS) data uint16_t alsData = vcnl4200.readALSdata(); Serial.print("ALS Data: "); Serial.print(alsData); // Read the raw white sensor data uint16_t whiteData = vcnl4200.readWhiteData(); Serial.print(", White Data: "); Serial.println(whiteData); delay(100); }
Upload the sketch to your board and open up the Serial Monitor (Tools -> Serial Monitor) at 115200 baud. You'll see the VCNL4200 recognized over I2C. Then, proximity, ambient light and white light readings will be printed to the Serial Monitor. You'll see these values changes as you affect your proximity to the sensor and the ambient light around the sensor.
Text editor powered by tinymce.