Simple test
Ensure your device works with this simple test.
examples/vcnl4010_simpletest.py
1# SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4# Simple demo of the VCNL4010 proximity and light sensor.
5# Will print the proximity and ambient light every second.
6import time
7
8import board
9
10import adafruit_vcnl4010
11
12i2c = board.I2C() # uses board.SCL and board.SDA
13# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
14sensor = adafruit_vcnl4010.VCNL4010(i2c)
15
16# You can optionally adjust the sensor LED current. The default is 200mA
17# which is the maximum value. Note this is only set in 10mA increments.
18# sensor.led_current_mA = 120 # Set 120 mA LED current
19
20# You can also adjust the measurement frequency for the sensor. The default
21# is 390.625 khz, but these values are possible to set too:
22# - FREQUENCY_3M125: 3.125 Mhz
23# - FREQUENCY_1M5625: 1.5625 Mhz
24# - FREQUENCY_781K25: 781.25 Khz
25# - FREQUENCY_390K625: 390.625 Khz (default)
26# sensor.frequency = adafruit_vcnl4010.FREQUENCY_3M125 # 3.125 Mhz
27
28# Main loop runs forever printing the proximity and light level.
29while True:
30 proximity = sensor.proximity # Proximity has no units and is a 16-bit
31 # value. The LOWER the value the further
32 # an object from the sensor (up to ~200mm).
33 print(f"Proximity: {proximity}")
34 ambient_lux = sensor.ambient_lux
35 print(f"Ambient light: {ambient_lux} lux")
36 time.sleep(1.0)
DisplayIO Simpletest
This is a simple test for boards with built-in display.
examples/vcnl4010_displayio_simpletest.py
1# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
2# SPDX-FileCopyrightText: 2024 Jose D. Montoya
3#
4# SPDX-License-Identifier: MIT
5
6# Simple demo of the VCNL4010 proximity sensor using a built-in display.
7import time
8
9import board
10from adafruit_display_text.bitmap_label import Label
11from displayio import Group
12from terminalio import FONT
13
14import adafruit_vcnl4010
15
16# create a main_group to hold anything we want to show on the display.
17main_group = Group()
18# Initialize I2C bus and sensor.
19i2c = board.I2C() # uses board.SCL and board.SDA
20sensor = adafruit_vcnl4010.VCNL4010(i2c)
21
22# Create a Label to show the readings. If you have a very small
23# display you may need to change to scale=1.
24display_output_label = Label(FONT, text="", scale=2)
25
26# place the label in the middle of the screen with anchored positioning
27display_output_label.anchor_point = (0, 0)
28display_output_label.anchored_position = (4, board.DISPLAY.height // 2)
29
30# add the label to the main_group
31main_group.append(display_output_label)
32
33# set the main_group as the root_group of the built-in DISPLAY
34board.DISPLAY.root_group = main_group
35
36# begin main loop
37while True:
38 # Update the label.text property to change the text on the display
39 display_output_label.text = (
40 f"Proximity: {sensor.proximity} Ambient light: {sensor.ambient_lux} lux"
41 )
42 # wait for a bit
43 time.sleep(1.0)