Home Code Raspberry Pi and VCNL4010 proximity and ambient light sensor python example

Raspberry Pi and VCNL4010 proximity and ambient light sensor python example

by shedboy71

In this article we look at another acceleration sensor – this time its the VCNL4010 and we will connect it to our Raspberry Pi and we will have a python example

First lets take a look at the sensor in question

The VCNL4010 is a fully integrated proximity and ambient light sensor. Fully integrated means that the infrared emitter is included in the package. It has 16 bit resolution. It includes a signal processing IC and features standard I2C communication interface. It features an interrupt function.

PROXIMITY FUNCTION
• Built-in infrared emitter and photo-pin-diode for proximity
function
• 16 bit effective resolution for proximity detection range
ensures excellent cross talk immunity
• Programmable LED drive current from 10 mA to 200 mA in
10 mA steps
• Excellent ambient light suppression by modulating the
infrared signal
• Proximity distance up to 200 mm

AMBIENT LIGHT FUNCTION
• Built-in ambient light photo-pin-diode with close-tohuman-eye sensitivity
• 16 bit dynamic range from 0.25 lx to 16 klx
• 100 Hz and 120 Hz flicker noise rejection

FEATURES

• Integrated modules: infrared emitter (IRED), ambient light sensor (ALS-PD), proximity sensor (PD), and signal conditioning IC
• Interrupt function
• Supply voltage range VDD: 2.5 V to 3.6 V
• Supply voltage range IR anode: 2.5 V to 5 V
• Communication via I2C interface
• I2C Bus H-level range: 1.7 V to 5 V
• Low stand by current consumption: 1.5 μA

Parts Required

Name Link
Raspberry Pi Raspberry Pi 4 Model B Development Board
VCNL4010 Multiple Function Sensor Development Tools Proximity/Light Sensor VCNL4000 (1 piece)
Connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

Schematic/Connection

pi and vcnl4010

pi and vcnl4010

Code Example

This uses a library from Adafruit which you can install like this from the terminal

  • sudo pip3 install adafruit-circuitpython-vcnl4010

I then opened the Mu editor and entered the following code example, which is the same as the default example

[codesyntax lang=”python”]

# Simple demo of the VCNL4010 proximity and light sensor.
# Will print the proximity and ambient light every second.
# Author: Tony DiCola
import time
 
import board
import busio
 
import adafruit_vcnl4010
 
 
# Initialize I2C bus and VCNL4010 module.
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_vcnl4010.VCNL4010(i2c)
 
# You can optionally adjust the sensor LED current.  The default is 200mA
# which is the maximum value.  Note this is only set in 10mA increments.
# sensor.led_current_mA = 120  # Set 120 mA LED current
 
# You can also adjust the measurement frequency for the sensor.  The default
# is 390.625 khz, but these values are possible to set too:
# - FREQUENCY_3M125: 3.125 Mhz
# - FREQUENCY_1M5625: 1.5625 Mhz
# - FREQUENCY_781K25: 781.25 Khz
# - FREQUENCY_390K625: 390.625 Khz (default)
# sensor.frequency = adafruit_vcnl4010.FREQUENCY_3M125  # 3.125 Mhz
 
# Main loop runs forever printing the proximity and light level.
while True:
    proximity = sensor.proximity  # Proximity has no units and is a 16-bit
    # value.  The LOWER the value the further
    # an object from the sensor (up to ~200mm).
    print("Proximity: {0}".format(proximity))
    ambient_lux = sensor.ambient_lux
    print("Ambient light: {0} lux".format(ambient_lux))
    time.sleep(1.0)

[/codesyntax]

Output

Run this example and you should see the following , cover the sensor and alter the light on it

Proximity: 2354
Ambient light: 133.0 lux
Proximity: 2355
Ambient light: 134.75 lux
Proximity: 46459
Ambient light: 31.75 lux
Proximity: 65535
Ambient light: 113.0 lux
Proximity: 65535
Ambient light: 114.5 lux

Links

https://www.vishay.com/docs/83462/vcnl4010.pdf

 

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More