# Using VCNL4010 Proximity Sensor

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/049/723/medium800/proximity_466-02.jpg?1514515631)

The VCNL4010 sensor is a nice way to add a small-distance proximity sensor to your microcontroller project. For longer distances (in the range of cm, you can use a [SHARP IR distance sensor](https://www.adafruit.com/products/164), but those are only good if the object is over 10 cm away. The VCNL4010 is designed for much shorter distances, no more than 200mm (about 7.5") and under our experimentation we found it worked best at distances of about 10-150mm. It would be good for say detecting when a hand moved nearby, or before a robot smacks into a wall. The sensor also has an ambient light sensor built in.

![](https://cdn-learn.adafruit.com/assets/assets/000/049/724/medium800/proximity_466-04.jpg?1514515639)

The sensor uses an infrared LED to bounce light off objects in front of it and gauge the returned intensity.&nbsp; You could do all this yourself with LEDs and light sensors, but the VCNL4010 wraps all that logic up into a stand-alone chip for you!&nbsp; With the VCNL4010 you can easily read the proximity (i.e. if an object is near or far) and even ambient light level over a simple I2C connection.&nbsp;

Best of all the with a CircuitPython module you can use the VCNL4010 from Python code!&nbsp; This guide shows how to connect and use the VCNL4010 with a CircuitPython or Arduino board.

![](https://cdn-learn.adafruit.com/assets/assets/000/049/717/medium800/proximity_IMG_6658.jpg?1514499114)

This sensor is easy to use with any microcontroller that has i2c capability. It is 5 volt compliant so you can use it with 3.3V or 5V logic with no risk of damage. There is an onboard 3.3V ultra low dropout regulator so you can power it with 3.3 to 5.0V. However, if you can give it 5.0V that is ideal since the VIN voltage powers the IR LED and the higher the voltage you can give it, the more powerful it is.

![](https://cdn-learn.adafruit.com/assets/assets/000/049/725/medium800/proximity_466-05.jpg?1514515708)

# Using VCNL4010 Proximity Sensor

## Pinouts

![](https://cdn-learn.adafruit.com/assets/assets/000/049/726/medium800/proximity_466-05.jpg?1514515717)

## Power Pins:

- **Vin** - this is the power pin. Since the sensor chip uses 3 VDC for logic, we have included a voltage regulator on board that will take 3-5VDC and safely convert it down. To power the board, give it the same power as the logic level of your microcontroller - e.g. for a 5V micro like Arduino, use 5V
- **3Vo** - this is the 3.3V output from the voltage regulator, you can grab up to 50mA from this if you like
- **GND** - common ground for power and logic

# Data Pins

- **SCL** - I2C clock pin, connect to your microcontrollers I2C clock line. Can use 3V or 5V logic, and has a weak pullup to **Vin**
- **SDA** - I2C data pin, connect to your microcontrollers I2C data line. Can use 3V or 5V logic, and has a weak pullup to **Vin**
- **INT** - Interrupt output, you can use this to give you an alert when the sensor detects something - but it isn't included in our library code  

# Using VCNL4010 Proximity Sensor

## Arduino

You'll need the following hardware to follow this guide:

- **[VCNL4010 sensor](https://www.adafruit.com/product/466).**
- **A board that is Arduino Compatible.** &nbsp; Must have an I2C interface port (often labeled SDA & SCL)
- [**Breadboard**](https://www.adafruit.com/product/65) **and [jumper wires](https://www.adafruit.com/product/153).**&nbsp; You'll need these parts to connect components to your development board.

Start by soldering the headers to the VCNL4010 breakout board.&nbsp; Check out the [Adafruit guide to excellent soldering](../../../../adafruit-guide-excellent-soldering/tools) if you're new to soldering.&nbsp; Then continue on below to learn how to wire it to a Metro or Arduino compatible.

The sensor uses **I2C address 0x13** and cannot be changed.

Connect the sensor to your board. The following example shows how to connect it to a Metro:

- **Board 5V** to **Sensor Vin**. (Metro is a 5V logic chip)
- **Board ground / GND** to **sensor ground / GND**.
- **Board SCL** to **sensor SCL**.
- **Board SDA** to **sensor SDA**.

![proximity_vncl4010_arduino_wiring.png](https://cdn-learn.adafruit.com/assets/assets/000/059/046/medium640/proximity_vncl4010_arduino_wiring.png?1534012876)

# Install Adafruit\_VCNL4010 library

To begin reading sensor data, you will need to [install the Adafruit\_VCNL4010 library (code on our github repository)](https://github.com/adafruit/Adafruit_VCNL4010 "Link: https://github.com/adafruit/Adafruit\_VCNL4010"). It is available from the Arduino library manager so we recommend using that.

From the IDE open up the library manager...

![](https://cdn-learn.adafruit.com/assets/assets/000/049/732/medium800/proximity_managelib.png?1514568580)

And type in **adafruit vcnl4010** to locate the library. Click **Install**

![](https://cdn-learn.adafruit.com/assets/assets/000/049/733/medium800/proximity_lib.png?1514569030)

# Load Demo

Open up **File-\>Examples-\>Adafruit\_VCNL4010**** -\>vcnl4010test** and upload to your microcontroller wired up to the sensor

![](https://cdn-learn.adafruit.com/assets/assets/000/049/734/medium800/proximity_test.png?1514569357)

Then open up the serial console at 9600 baud, you'll see ambient light and proximity data printed out! Note that both are unitless, it isn't like the proximity is in millimeters or inches, its just higher the closer you are.

![](https://cdn-learn.adafruit.com/assets/assets/000/049/735/medium800/proximity_pid466.png?1514574652)

# Using VCNL4010 Proximity Sensor

## Arduino Library Docs

# Using VCNL4010 Proximity Sensor

## Python & CircuitPython

It's easy to use the VCNL4010 sensor with Python and CircuitPython, and the [Adafruit CircuitPython VCNL4010](https://github.com/adafruit/Adafruit_CircuitPython_VCNL4010) module.&nbsp; This module allows you to easily write Python code that reads small-distance proximity from the sensor.

You can use this sensor with any CircuitPython microcontroller board or with a computer that has GPIO and Python [thanks to Adafruit\_Blinka, our CircuitPython-for-Python compatibility library](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux).

# CircuitPython Microcontroller Wiring

First wire up a VCNL4010 to your board exactly as shown in the previous pages for Arduino. Here's an example of wiring a Feather M0 to the sensor with I2C:

- **Board 3.3V** to **sensor Vin** (Feather is 3.3V logic)
- **Board ground / GND** to **sensor ground / GND**.
- **Board SCL** to **sensor SCL**.
- **Board SDA** to **sensor SDA**.

![proximity_feather_vncl4010.png](https://cdn-learn.adafruit.com/assets/assets/000/059/047/medium640/proximity_feather_vncl4010.png?1534013112)

# Python Computer Wiring

Since there's _dozens_ of Linux computers/boards you can use we will show wiring for Raspberry Pi. For other platforms, [please visit the guide for CircuitPython on Linux to see whether your platform is supported](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux).&nbsp;

Here's the Raspberry Pi wired with I2C:

- **Pi 3V3** to **sensor VIN**
- **Pi GND** to **sensor GND**
- **Pi SCL** to **sensor SCK**
- **Pi SDA** to **sensor SDA**

![proximity_raspi_vncl4010_i2c_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/059/051/medium640/proximity_raspi_vncl4010_i2c_bb.jpg?1534092454)

# CircuitPython Installation of VCNL4010 Library

To use the VCNL4010 you'll need to install the&nbsp;[Adafruit CircuitPython VCNL4010](https://github.com/adafruit/Adafruit_CircuitPython_VCNL4010)&nbsp;library on your CircuitPython board.

First make sure you are running the&nbsp;[latest version of Adafruit CircuitPython](../../../../welcome-to-circuitpython/installing-circuitpython)&nbsp;for your board.

Next you'll need to install the necessary libraries&nbsp;to use the hardware--carefully follow the steps to find and install these libraries from&nbsp;[Adafruit's CircuitPython library bundle](https://github.com/adafruit/Adafruit_CircuitPython_Bundle).&nbsp; Our introduction guide has&nbsp;[a great page on how to install the library bundle](../../../../welcome-to-circuitpython/circuitpython-libraries)&nbsp;for both express and non-express boards.

Remember for non-express boards like the, you'll need to manually install the necessary libraries from the bundle:

- **adafruit\_vcnl4010.mpy**
- **adafruit\_bus\_device**

Before continuing make sure your board's lib folder or root filesystem has the&nbsp; **adafruit\_vcnl4010.mpy,&nbsp;** and **&nbsp;adafruit\_bus\_device**** &nbsp; **files and folders** &nbsp;**copied over.

Next&nbsp;[connect to the board's serial REPL&nbsp;](../../../../welcome-to-circuitpython/the-repl)so you are at the CircuitPython&nbsp; **\>\>\>** &nbsp;prompt.

# Python Installation of VCNL4010 Library

You'll need to install the Adafruit\_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. [Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux)!

Once that's done, from your command line run the following command:

- `sudo pip3 install adafruit-circuitpython-vcnl4010`

If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!

# CircuitPython & Python Usage

To demonstrate the usage of the sensor we'll initialize it and read the proximity and ambient light from the Python REPL.

Run the following code to import the necessary modules and initialize the I2C connection with the sensor:

```
import board
import busio
import adafruit_vcnl4010
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_vcnl4010.VCNL4010(i2c)
```

Now you're ready to read values from the sensor using any of these properties:

- **proximity** &nbsp;- The value of the proximity sensor.&nbsp; This has no units and is **inversely** proportional to the distance of an object from the sensor.&nbsp; I.e. the **farther** an object from the sensor the **lower** this value.&nbsp; The range of values are 0 (very far away, up to ~200mm) to 65535 (right in front of the sensor).
- **ambient\_lux -** The detected ambient light as a value in lux.
- **ambient** - The raw ambient light sensor value--this has no units but is proportional to the brightness.&nbsp; You probably want to use the lux property above instead of this raw value!

```
print('Proximity: {0}'.format(sensor.proximity))
print('Ambient light: {0} lux'.format(sensor.ambient_lux))
```

![](https://cdn-learn.adafruit.com/assets/assets/000/049/715/medium800/proximity_Screen_Shot_2017-12-28_at_1.59.32_PM.png?1514498392)

In addition there are a couple properties you can both read and write to change the behavior of the sensor:

- **led\_current** and **led\_current\_mA** - These properties control the amount of current given to the sensor's LED for proximity detection.&nbsp; The sensor supports 0 - 200mA of current and the led\_current property is set with units of 10mA (i.e. a value of 20 is 200mA), while the led\_current\_mA property takes the value in milliamps directly (like 200mA).&nbsp; The default is 200mA of current for the brightest LED.
- **frequency** - This property controls the frequency of measurement timings.&nbsp; You can set it to a value of:
  - **adafruit\_vcnl4010.FREQUENCY\_3M125** - 3.125 mhz
  - **adafruit\_vcnl4010.FREQUENCY\_1M5625** - 1.5625 mhz
  - **adafruit\_vcnl4010.FREQUENCY\_781K25** - 781.25 khz
  - **adafruit\_vcnl4010.FREQUENCY\_390K625** - 390.625 khz (the default)

Read the [VCNL4010 datasheet](https://cdn-shop.adafruit.com/product-files/466/vcnl4010.pdf) to understand how LED current and measurement frequency impact power consumption and accuracy!

```
sensor.led_current_mA = 120
sensor.frequency = adafruit_vcnl4010.FREQUENCY_3M125
```

![](https://cdn-learn.adafruit.com/assets/assets/000/049/716/medium800/proximity_Screen_Shot_2017-12-28_at_2.00.14_PM.png?1514498430)

That's all there is to using the VCNL4010 sensor with CircuitPython!

Here's a complete example of reading the proximity and ambient light every second and printing it at the serial terminal.&nbsp; Save this as **code.py** on your board and open the serial terminal to see the output.

# Full Example Code
https://github.com/adafruit/Adafruit_CircuitPython_VCNL4010/blob/main/examples/vcnl4010_simpletest.py

# Using VCNL4010 Proximity Sensor

## Python Docs

# Using VCNL4010 Proximity Sensor

## Downloads

# Files:

- [VCNL4010 Datasheet / App Note / Product Sheet](https://cdn-shop.adafruit.com/product-files/466/vcnl4010.pdf)
- [EagleCAD PCB files on GitHub](https://github.com/adafruit/VCNL4000)
- [Fritzing library](https://github.com/adafruit/Fritzing-Library)

Datasheets for the older VCNL4000 version of this board:

- [VCNL4000 datasheet](https://cdn-shop.adafruit.com/datasheets/vcnl4000.pdf)
- [VCNL4000 App Note](https://cdn-shop.adafruit.com/datasheets/vcnl4000AN.pdf)
- [VCNL4000 Product sheet](https://cdn-shop.adafruit.com/datasheets/vcnl4000ps.pdf)

# Schematic and Fab Print
![](https://cdn-learn.adafruit.com/assets/assets/000/096/411/medium800/proximity_VCNL4010_sch.png?1603729862)

![](https://cdn-learn.adafruit.com/assets/assets/000/096/412/medium800/proximity_VCNL4010_fab_print.png?1603729871)


## Featured Products

### VCNL4010 Proximity/Light sensor

[VCNL4010 Proximity/Light sensor](https://www.adafruit.com/product/466)
The VCNL4010 sensor is a nice way to add a small-distance proximity sensor to your microcontroller project. For longer distances (in the range of cm, you can use a [SHARP IR distance sensor](https://www.adafruit.com/products/164), but those are only good if the object is over 10 cm...

In Stock
[Buy Now](https://www.adafruit.com/product/466)
[Related Guides to the Product](https://learn.adafruit.com/products/466/guides)

## Related Guides

- [Festive Feather Holiday Lights](https://learn.adafruit.com/festive-feather-holiday-lights.md)
- [I2C Addresses and Troublesome Chips](https://learn.adafruit.com/i2c-addresses.md)
- [Adafruit Proximity Trinkey](https://learn.adafruit.com/adafruit-proximity-trinkey.md)
- [Adafruit INA228 I2C Power Monitor](https://learn.adafruit.com/adafruit-ina228-i2c-power-monitor.md)
- [Bluetooth Restroom Key Proximity Tracker](https://learn.adafruit.com/bluetooth-restroom-key-proximity-tracker.md)
- [New Years Eve Ball Drop](https://learn.adafruit.com/new-years-eve-ball-drop.md)
- [Game & Watch Octopus](https://learn.adafruit.com/game-watch-octopus.md)
- [PyPortal ISS Tracker](https://learn.adafruit.com/pyportal-iss-tracker.md)
- [CircuitPython Animated Holiday Wreath Lights](https://learn.adafruit.com/circuitpython-animated-holiday-wreath-lights.md)
- [Larsio Paint Music](https://learn.adafruit.com/larsio-paint-music.md)
- [Feather RP2040 DVI CircuitPython Day 2024 Countdown Clock](https://learn.adafruit.com/feather-rp2040-dvi-circuitpython-day-2024-countdown-clock.md)
- [Adafruit Trinkey QT2040](https://learn.adafruit.com/adafruit-trinkey-qt2040.md)
- [Adafruit MicroSD SPI or SDIO Card Breakout Board](https://learn.adafruit.com/adafruit-microsd-spi-sdio.md)
- [A CircuitPython BLE Remote Control On/Off Switch](https://learn.adafruit.com/circuitpython-ble-remote-control-on-off.md)
- [CircuitPython USB Workflow Code Editor Quick Start](https://learn.adafruit.com/circuitpython-usb-workflow-code-editor-quick-start.md)
