Qwiic_Proximity_Py¶
Python module to interface with the Qwiic Proximity board.
This package is a port of the SparkFun VCNL4040 Proximity Sensor Arduino Library
This package can be used in conjunction with the overall SparkFun qwiic Python Package
New to qwiic? Take a look at the entire SparkFun qwiic ecosystem.
Supported Platforms¶
The qwiic Proximity Python package current supports the following platforms:
Dependencies¶
This driver package depends on the qwiic I2C driver: Qwiic_I2C_Py
Documentation¶
The SparkFun qwiic Proximity module documentation is hosted at ReadTheDocs
PyPi Installation¶
This repository is hosted on PyPi as the sparkfun-qwiic-proximity package. On systems that support PyPi installation via pip, this library is installed using the following commands
For all users (note: the user must have sudo privileges):
sudo pip install sparkfun-qwiic-proximity
For the current user:
pip install sparkfun-qwiic-proximity
Local Installation¶
To install, make sure the setuptools package is installed on the system.
Direct installation at the command line:
python setup.py install
To build a package for use with pip:
python setup.py sdist
A package file is built and placed in a subdirectory called dist. This package file can be installed using pip.
cd dist
pip install sparkfun_qwiic_proximity-<version>.tar.gz
Example Use¶
See the examples directory for more detailed use examples.
import qwiic_proximity
import time
import sys
def runExample():
print("\nSparkFun Proximity Sensor VCN4040 Example 1\n")
oProx = qwiic_proximity.QwiicProximity()
if oProx.isConnected() == False:
print("The Qwiic Proximity device isn't connected to the system. Please check your connection", \
file=sys.stderr)
return
oProx.begin()
while True:
proxValue = oProx.getProximity()
print("Proximity Value: %d" % proxValue)
time.sleep(.4)
runExample()
Table of Contents¶
API Reference¶
qwiic_proximity¶
Python module for the [SparkFun Qwiic Proximity Sensor Breakout](https://www.sparkfun.com/products/15177)
This python package is a port of the existing [SparkFun VCNL4040 Proximity Sensor Arduino Library](https://github.com/sparkfun/SparkFun_VCNL4040_Arduino_Library)
This package can be used in conjunction with the overall [SparkFun qwiic Python Package](https://github.com/sparkfun/Qwiic_Py)
New to qwiic? Take a look at the entire [SparkFun qwiic ecosystem](https://www.sparkfun.com/qwiic).
-
class
qwiic_proximity.
QwiicProximity
(address=None, i2c_driver=None)[source]¶ Parameters: - address – The I2C address to use for the device. If not provided, the default address is used.
- i2c_driver – An existing i2c driver object. If not provided a driver object is created.
Returns: The Proximity device object.
Return type: Object
-
ambient
¶ Read the Ambient light value
Returns: The current ambient value value Return type: integer
-
begin
()[source]¶ Initialize the operation of the Proximity module
Returns: Returns true of the initializtion was successful, otherwise False. Return type: bool
-
connected
¶ Determine if a Proximity device is conntected to the system..
Returns: True if the device is connected, otherwise False. Return type: bool
-
disable_prox_logic_mode
()[source]¶ Disable the proximity detection logic output mode
Returns: No return value
-
enable_active_force_mode
()[source]¶ Enable active force mode An extreme power saving way to use PS is to apply PS active force mode. Anytime host would like to request one proximity measurement, enable the active force mode. This triggers a single PS measurement, which can be read from the PS result registers. VCNL4040 stays in standby mode constantly.
Returns: No return value
-
enable_prox_logic_mode
()[source]¶ Enable the proximity detection logic output mode When this mode is selected, the INT pin is pulled low when an object is close to the sensor (value is above high threshold) and is reset to high when the object moves away (value is below low threshold). Register: PS_THDH / PS_THDL define where these threshold levels are set.
Returns: No return value
-
enable_smart_persistance
()[source]¶ Enable smart persistance To accelerate the PS response time, smart persistence prevents the misjudgment of proximity sensing but also keeps a fast response time.
Returns: No return value
-
get_ambient
()[source]¶ Read the Ambient light value
Returns: The current ambient value value Return type: integer
-
get_proximity
()[source]¶ Get the current proximity value
Returns: The current proximity value Return type: integer
-
get_white
()[source]¶ Read the White light value
Returns: The current white value value Return type: integer
-
is_away
¶ Returns true if the prox value drops below the lower threshold
Returns: True if away Return type: boolean
-
is_close
¶ Returns true if the prox value rises above the upper threshold
Returns: True if close Return type: boolean
-
is_connected
()[source]¶ Determine if a Proximity device is conntected to the system..
Returns: True if the device is connected, otherwise False. Return type: bool
-
is_dark
¶ Returns true if the prox value drops below the lower threshold
Returns: True if dark Return type: boolean
-
is_light
¶ Returns true if the prox value rises above the upper threshold
Returns: True if value light Return type: boolean
-
power_off_ambient
()[source]¶ Power off the ambient light sensing portion of the sensor
Returns: No return value
-
power_off_proximity
()[source]¶ Power off the prox sensing portion of the device
Returns: No return value
-
power_on_ambient
()[source]¶ Power on the ambient light sensing portion of the sensor
Returns: No return value
-
power_on_proximity
()[source]¶ Power on the prox sensing portion of the device
Returns: No return value
-
proximity
¶ Get the current proximity value
Returns: The current proximity value Return type: integer
-
sensor_id
¶ Read the sensor ID
Returns: The sensor ID Return type: integer
-
set_als_high_threshold
(threshold)[source]¶ Value that ALS must go above to trigger an interrupt
Parameters: threshold – the new trigger threshold value for ALS Returns: No return value
-
set_als_low_threshold
(threshold)[source]¶ Value that ALS must go below to trigger an interrupt
Parameters: threshold – the new trigger threshold value for ALS Returns: No return value
-
set_ambient_integration_time
(timeValue)[source]¶ Sets the integration time for the ambient light sensor
Parameters: timeValue – The integration time Returns: No return value
-
set_ambient_interrupt_persistance
(persValue)[source]¶ Set the Ambient interrupt persistance value The ALS persistence function (ALS_PERS, 1, 2, 4, 8) helps to avoid false trigger of the ALS INT. It defines the amount of consecutive hits needed in order for a ALS interrupt event to be triggered.
Parameters: persValue – The ambiant interrupt persistance value Returns: No return value
-
set_ir_dutycycle
(dutyValue)[source]¶ Set the duty cycle of the IR LED. The higher the duty ratio, the faster the response time achieved with higher power consumption. For example, PS_Duty = 1/320, peak IRED current = 100 mA, averaged current consumption is 100 mA/320 = 0.3125 mA.
Parameters: dutyValue – The duty cycle value for the IR LED on the sensor Returns: No return value
-
set_led_current
(currentValue)[source]¶ Set the IR LED sink current to one of 8 settings
Parameters: currentValue – The new current value. Valid values are VCNL4040_LED_50MA thru VCNL4040_LED_200MA at 25MA increments Returns: No return value
-
set_prox_cancellation
(cancelValue)[source]¶ Set the proximity sensing cancelation value - helps reduce cross talk with ambient light
Parameters: cancelValue – the new cancelation value Returns: No return value
-
set_prox_high_threshold
(threshold)[source]¶ Value that Proximity Sensing must go above to trigger an interrupt
Parameters: threshold – The new Proximity High Value Returns: No return value
-
set_prox_integration_time
(timeValue)[source]¶ Sets the integration time for the proximity sensor
Parameters: timeValue – The integration time Returns: No return value
-
set_prox_interrupt_persistance
(persValue)[source]¶ Set the Prox interrupt persistance value The PS persistence function (PS_PERS, 1, 2, 3, 4) helps to avoid false trigger of the PS INT. It defines the amount of consecutive hits needed in order for a PS interrupt event to be triggered.
Parameters: persValue – The persistance value Returns: No return value
-
set_prox_interrupt_type
(interruptValue)[source]¶ Sets the proximity interrupt type
Parameters: interruptValue – The interupt type Returns: No return value
-
set_prox_low_threshold
(threshold)[source]¶ Value that Proximity Sensing must go below to trigger an interrupt
Parameters: threshold – The new Proximity Low Value Returns: No return value
-
set_prox_resolution
(resolutionValue)[source]¶ Sets the proximity resolution
Parameters: resolutionValue – The resolution time Returns: No return value
-
take_single_prox_measurement
()[source]¶ Set trigger bit so sensor takes a force mode measurement and returns to standby
Returns: No return value
-
white_light
¶ Read the White light value
Returns: The current white value value Return type: integer
Example 1¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | #!/usr/bin/env python
#-----------------------------------------------------------------------------
# qwiic_proximity_ex1.py
#
# Simple Example for the Qwiic Proximity Device
#------------------------------------------------------------------------
#
# Written by SparkFun Electronics, May 2019
#
# This python library supports the SparkFun Electroncis qwiic
# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
# board computers.
#
# More information on qwiic is at https://www.sparkfun.com/qwiic
#
# Do you like this library? Help support SparkFun. Buy a board!
#
#==================================================================================
# Copyright (c) 2019 SparkFun Electronics
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#==================================================================================
# Example 1
#
# - Setup the device
# - Output the proximity value
from __future__ import print_function
import qwiic_proximity
import time
import sys
def runExample():
print("\nSparkFun Proximity Sensor VCN4040 Example 1\n")
oProx = qwiic_proximity.QwiicProximity()
if oProx.connected == False:
print("The Qwiic Proximity device isn't connected to the system. Please check your connection", \
file=sys.stderr)
return
oProx.begin()
while True:
proxValue = oProx.get_proximity()
print("Proximity Value: %d" % proxValue)
time.sleep(.4)
if __name__ == '__main__':
try:
runExample()
except (KeyboardInterrupt, SystemExit) as exErr:
print("\nEnding Example 1")
sys.exit(0)
|
Example 2¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | #!/usr/bin/env python
#-----------------------------------------------------------------------------
# qwiic_proximity_ex2.py
#
# Simple Example for the Qwiic Proximity Device
#------------------------------------------------------------------------
#
# Written by SparkFun Electronics, May 2019
#
# This python library supports the SparkFun Electroncis qwiic
# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
# board computers.
#
# More information on qwiic is at https://www.sparkfun.com/qwiic
#
# Do you like this library? Help support SparkFun. Buy a board!
#
#==================================================================================
# Copyright (c) 2019 SparkFun Electronics
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#==================================================================================
# Example 2 - Is something there
#
from __future__ import print_function
import qwiic_proximity
import time
import sys
def runExample():
print("\nSparkFun Proximity Sensor VCN4040 Example 2\n")
oProx = qwiic_proximity.QwiicProximity()
if oProx.connected == False:
print("The Qwiic Proximity device isn't connected to the system. Please check your connection", \
file=sys.stderr)
return
# begin Setup
oProx.begin()
oProx.set_led_current(200)
oProx.set_prox_integration_time(8) # 1 to 8 is valid
# Take 8 readings and average them
startingProxValue=0
for x in range(8):
startingProxValue += oProx.get_proximity()
startingProxValue /= 8
deltaNeeded = startingProxValue * 0.05 # Look for %5 change
if deltaNeeded < 5:
deltaNeeded = 5 # set a min value
# Begin operation loop
nothingThere = True
while True:
proxValue = oProx.get_proximity()
print("Proximity Value: %d" % proxValue)
if proxValue > startingProxValue + deltaNeeded:
nothingThere = False
print("\tSomething is there!")
elif not nothingThere:
print("\tI don't see anything")
nothingThere=True
time.sleep(.4)
if __name__ == '__main__':
try:
runExample()
except (KeyboardInterrupt, SystemExit) as exErr:
print("\nEnding Example 2")
sys.exit(0)
|
Example 3¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | #!/usr/bin/env python
#-----------------------------------------------------------------------------
# qwiic_proximity_ex3.py
#
# Simple Example for the Qwiic Proximity Device
#------------------------------------------------------------------------
#
# Written by SparkFun Electronics, May 2019
#
# This python library supports the SparkFun Electroncis qwiic
# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
# board computers.
#
# More information on qwiic is at https://www.sparkfun.com/qwiic
#
# Do you like this library? Help support SparkFun. Buy a board!
#
#==================================================================================
# Copyright (c) 2019 SparkFun Electronics
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#==================================================================================
# Example 3 - Ambient Light
#
from __future__ import print_function
import qwiic_proximity
import time
import sys
def runExample():
print("\nSparkFun Proximity Sensor VCN4040 Example 3\n")
oProx = qwiic_proximity.QwiicProximity()
if oProx.connected == False:
print("The Qwiic Proximity device isn't connected to the system. Please check your connection", \
file=sys.stderr)
return
# begin Setup
oProx.begin()
oProx.power_off_proximity() # Power down the proximity portion of the sensor
oProx.power_on_ambient() # Power Up the ambient sensor
while True:
ambientValue = oProx.get_ambient()
print("Ambient Value: %d" % ambientValue)
time.sleep(.4)
if __name__ == '__main__':
try:
runExample()
except (KeyboardInterrupt, SystemExit) as exErr:
print("\nEnding Example 3")
sys.exit(0)
|
Example 4¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | #!/usr/bin/env python
#-----------------------------------------------------------------------------
# qwiic_proximity_ex4.py
#
# Simple Example for the Qwiic Proximity Device
#------------------------------------------------------------------------
#
# Written by SparkFun Electronics, May 2019
#
# This python library supports the SparkFun Electroncis qwiic
# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
# board computers.
#
# More information on qwiic is at https://www.sparkfun.com/qwiic
#
# Do you like this library? Help support SparkFun. Buy a board!
#
#==================================================================================
# Copyright (c) 2019 SparkFun Electronics
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#==================================================================================
# Example 4 - All Readings
#
from __future__ import print_function
import qwiic_proximity
import time
import sys
def runExample():
print("\nSparkFun Proximity Sensor VCN4040 Example 4\n")
oProx = qwiic_proximity.QwiicProximity()
if oProx.connected == False:
print("The Qwiic Proximity device isn't connected to the system. Please check your connection", \
file=sys.stderr)
return
# begin Setup
oProx.begin()
oProx.power_on_proximity() # Power up the proximity portion of the sensor
oProx.power_on_ambient() # Power Up the ambient sensor
oProx.enable_white_channel()
while True:
proxValue = oProx.get_proximity()
print("Proximity Value: \t[%5d]" % proxValue)
ambientValue = oProx.get_ambient()
print("Ambient Value: \t\t[%5d]" % ambientValue)
whiteValue = oProx.get_white()
print("White Value: \t\t[%5d]\n" % whiteValue)
time.sleep(.5)
if __name__ == '__main__':
try:
runExample()
except (KeyboardInterrupt, SystemExit) as exErr:
print("\nEnding Example 4")
sys.exit(0)
|
Example 5¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | #!/usr/bin/env python
#-----------------------------------------------------------------------------
# qwiic_proximity_ex5.py
#
# Simple Example for the Qwiic Proximity Device
#------------------------------------------------------------------------
#
# Written by SparkFun Electronics, May 2019
#
# This python library supports the SparkFun Electroncis qwiic
# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
# board computers.
#
# More information on qwiic is at https://www.sparkfun.com/qwiic
#
# Do you like this library? Help support SparkFun. Buy a board!
#
#==================================================================================
# Copyright (c) 2019 SparkFun Electronics
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#==================================================================================
# Example 5 - Advanced Settings
#
from __future__ import print_function
import qwiic_proximity
import time
import sys
def runExample():
print("\nSparkFun Proximity Sensor VCN4040 Example 5\n")
oProx = qwiic_proximity.QwiicProximity()
if oProx.connected == False:
print("The Qwiic Proximity device isn't connected to the system. Please check your connection", \
file=sys.stderr)
return
# begin Setup
oProx.begin()
oProx.power_on_ambient() # Power Up the ambient sensor
# Set the integration time for the proximity sensor
# 1 to 8 is valid
oProx.prox_integration_time = 8
# Set the integration time for the ambient light sensor in milliseconds
# 80 to 640ms is valid
oProx.ambient_integration_time = 80
# If sensor sees more than this, interrupt pin will go low
oProx.prox_high_threshold = 2000
# The int pin will stay low until the value goes below the low threshold value
oProx.prox_low_threshold = 150
# Enable both 'away' and 'close' interrupts
oProx.prox_interrupt_type = oProx.VCNL4040_PS_INT_BOTH
# This causes the int pin to go low every time a reading is outside the thresholds
# Get a multimeter and probe the INT pin to see this feature in action
oProx.enable_prox_logic_mode()
while True:
proxValue = oProx.proximity
print("Proximity Value: \t[%5d]" % proxValue)
ambientValue = oProx.ambient
print("Ambient Value: \t\t[%5d]\n" % ambientValue)
time.sleep(1)
if __name__ == '__main__':
try:
runExample()
except (KeyboardInterrupt, SystemExit) as exErr:
print("\nEnding Example 5")
sys.exit(0)
|
Example 8¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | #!/usr/bin/env python
#-----------------------------------------------------------------------------
# qwiic_proximity_ex8.py
#
# Simple Example for the Qwiic Proximity Device
#------------------------------------------------------------------------
#
# Written by SparkFun Electronics, May 2019
#
# This python library supports the SparkFun Electroncis qwiic
# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
# board computers.
#
# More information on qwiic is at https://www.sparkfun.com/qwiic
#
# Do you like this library? Help support SparkFun. Buy a board!
#
#==================================================================================
# Copyright (c) 2019 SparkFun Electronics
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#==================================================================================
# Example 8 - Unit test example
from __future__ import print_function
import qwiic_proximity
import time
import sys
def runExample():
print("\nSparkFun Proximity Sensor VCN4040 Example 1\n")
oProx = qwiic_proximity.QwiicProximity()
if oProx.connected == False:
print("The Qwiic Proximity device isn't connected to the system. Please check your connection", \
file=sys.stderr)
return
oProx.begin()
while True:
if oProx.connected:
oProx.power_on_proximity()
proxValue = oProx.get_proximity()
print("Good Proximity Value: %d" % proxValue)
else:
print("Not Connected")
time.sleep(.4)
if __name__ == '__main__':
try:
runExample()
except (KeyboardInterrupt, SystemExit) as exErr:
print("\nEnding Example 8")
sys.exit(0)
|