Skip to content

Commit e547559

Browse files
committed
Add VCNL4200
1 parent 602bbd6 commit e547559

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed

platformio.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ lib_deps =
4444
adafruit/Adafruit Si7021 Library
4545
adafruit/Adafruit VCNL4020 Library
4646
adafruit/Adafruit VCNL4040
47+
adafruit/Adafruit VCNL4200 Library
4748
adafruit/Adafruit MCP3421
4849
adafruit/Adafruit MCP9808 Library
4950
adafruit/Adafruit MCP9600 Library

src/components/i2c/controller.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,11 @@ static const std::map<std::string, FnCreateI2CDriver> I2cFactory = {
338338
const char *driver_name) -> drvBase * {
339339
return new drvVncl4040(i2c, addr, mux_channel, driver_name);
340340
}},
341+
{"vncl4200",
342+
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
343+
const char *driver_name) -> drvBase * {
344+
return new drvVncl4200(i2c, addr, mux_channel, driver_name);
345+
}},
341346
{"vl53l0x",
342347
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
343348
const char *driver_name) -> drvBase * {

src/components/i2c/controller.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
#include "drivers/drvVl6180x.h"
7171
#include "drivers/drvVncl4020.h"
7272
#include "drivers/drvVncl4040.h"
73+
#include "drivers/drvVncl4200.h"
7374

7475
#define SCAN_DEVICE \
7576
"UNKNOWN_SCAN" ///< Name for I2C devices found by an i2c scan
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*!
2+
* @file drvVncl4200.h
3+
*
4+
* Device driver for the VCNL4200 light + proximity sensor.
5+
*
6+
* Adafruit invests time and resources providing this open source code,
7+
* please support Adafruit and open-source hardware by purchasing
8+
* products from Adafruit!
9+
*
10+
* Copyright (c) Tyeth Gundry 2025 for Adafruit Industries.
11+
*
12+
* MIT license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
#ifndef DRV_VCNL4200_H
16+
#define DRV_VCNL4200_H
17+
18+
#include "drvBase.h"
19+
#include <Adafruit_VCNL4200.h>
20+
21+
/**************************************************************************/
22+
/*!
23+
@brief Class that provides a driver interface for a VCNL4200 sensor.
24+
*/
25+
/**************************************************************************/
26+
class drvVncl4200 : public drvBase {
27+
public:
28+
/*******************************************************************************/
29+
/*!
30+
@brief Constructor for a VCNL4200 sensor.
31+
@param i2c
32+
The I2C interface.
33+
@param sensorAddress
34+
7-bit device address.
35+
@param mux_channel
36+
The I2C multiplexer channel.
37+
@param driver_name
38+
The name of the driver.
39+
*/
40+
/*******************************************************************************/
41+
drvVncl4200(TwoWire *i2c, uint16_t sensorAddress, uint32_t mux_channel,
42+
const char *driver_name)
43+
: drvBase(i2c, sensorAddress, mux_channel, driver_name) {
44+
// Initialization handled by drvBase constructor
45+
}
46+
47+
/*******************************************************************************/
48+
/*!
49+
@brief Destructor for an VCNL4200 sensor.
50+
*/
51+
/*******************************************************************************/
52+
~drvVncl4200() { delete _vcnl4200; }
53+
54+
/*******************************************************************************/
55+
/*!
56+
@brief Initializes the VCNL4200 sensor and begins I2C.
57+
@returns True if initialized successfully, False otherwise.
58+
*/
59+
/*******************************************************************************/
60+
bool begin() {
61+
_vcnl4200 = new Adafruit_VCNL4200();
62+
bool status = false;
63+
// Attempt to initialize and configure VCNL4200
64+
if (!_vcnl4200->begin(_address, _i2c)) {
65+
return false;
66+
}
67+
status = _vcnl4200->setALSshutdown(false);
68+
status &= _vcnl4200->setProxShutdown(false);
69+
status &= _vcnl4200->setProxHD(true); // 16bit instead of 12bit
70+
status &= _vcnl4200->setALSIntegrationTime(VCNL4200_ALS_IT_400MS);
71+
status &= _vcnl4200->setProxDuty(VCNL4200_PS_DUTY_1_160);
72+
status &= _vcnl4200->setProxLEDCurrent(VCNL4200_LED_I_200MA);
73+
status &= _vcnl4200->setProxIntegrationTime(VCNL4200_PS_IT_9T);
74+
return status;
75+
}
76+
77+
/*******************************************************************************/
78+
/*!
79+
@brief Performs a light sensor read using the Adafruit
80+
Unified Sensor API.
81+
@param lightEvent
82+
Light sensor reading, in lux.
83+
@returns True if the sensor event was obtained successfully, False
84+
otherwise.
85+
*/
86+
/*******************************************************************************/
87+
bool getEventLight(sensors_event_t *lightEvent) {
88+
// Get sensor event populated in lux via AUTO integration and gain
89+
lightEvent->light = _vcnl4200->readALSdata();
90+
return true;
91+
}
92+
93+
/*******************************************************************************/
94+
/*!
95+
@brief Reads the VCNL4200's proximity value into an event (no unit).
96+
@param proximityEvent
97+
Pointer to an Adafruit_Sensor event.
98+
@returns True if the proximity was obtained successfully, False
99+
otherwise.
100+
*/
101+
/*******************************************************************************/
102+
bool getEventProximity(sensors_event_t *proximityEvent) {
103+
proximityEvent->data[0] = (float)_vcnl4200->readProxData();
104+
return true;
105+
}
106+
107+
protected:
108+
Adafruit_VCNL4200 *_vcnl4200; ///< Pointer to VCNL4200 light sensor object
109+
};
110+
111+
#endif // DRV_VCNL4200_H

0 commit comments

Comments
 (0)