Skip to main content

CircuitPython driver for APSD9960 Gesture breakout board

Project description

Introduction

Documentation Status Discord Build Status Code Style: Black

The APDS-9960 is a specialized chip that detects hand gestures, proximity and ambient light color over I2C. Its available from Adafruit as a breakout and as a built-in sensor on several Adafruit development boards.

This driver provides easy access to proximity, gesture and color data from the APDS-9960 sensor with a minimal footprint to allow it to work on all CircuitPython platforms.

Installation and Dependencies

This driver depends on:

Please ensure all dependencies are available on the CircuitPython filesystem.

This is easily achieved by downloading the Adafruit library and driver bundle.

Installing from PyPI

On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally from PyPI.

To install for current user:

pip3 install adafruit-circuitpython-apds9960

To install system-wide (this may be required in some cases):

sudo pip3 install adafruit-circuitpython-apds9960

To install in a virtual environment in your current project:

mkdir project-name && cd project-name
python3 -m venv .venv
source .venv/bin/activate
pip3 install adafruit-circuitpython-apds9960

Usage Example

import board
import digitalio
from adafruit_apds9960.apds9960 import APDS9960

i2c = board.I2C()
int_pin = digitalio.DigitalInOut(board.D5)
int_pin.switch_to_input(pull=digitalio.Pull.UP)
apds = APDS9960(i2c)

apds.enable_proximity_interrupt = True
apds.proximity_interrupt_threshold = (0, 175)
apds.enable_proximity = True

while True:
    if not int_pin.value:
        print(apds.proximity)
        apds.clear_interrupt()

Hardware Set-up

If you’re using a board with a built-in APDS-9960, no hardware setup will be required.

If you’re using a breakout board via the pin header, connect Vin to a 3.3 V or 5 V power source, connect GND to ground, then connect SCL and SDA to the appropriate pins.

Optionally, if you’d like to use the sensor’s interrupt pin connect INT to any available digital I/O pin.

Basics

To get started, import board and, and this library:

import board
from adafruit_apds9960.apds9960 import APDS9960

To set up the sensor to gather data, initialize the I2C bus via board.I2C() then initialize the APDS-9960 library.

i2c = board.I2C()
apds = APDS9960(i2c)

Proximity

To get a proximity result, enable the proximity engine then read the proximity value.

This will return a value between 0 and 255, with higher values indicating that something is close to the sensor.

apds.enable_proximity = True

while True:
  print(apds.proximity)

Gestures

First, enable both the proximity and gesture engines. The gesture engine relies on the proximity engine to determine when to start itself up and, as a result, proximity readings won’t be reliable while the gesture engine is enabled.

To get a gesture, use the gesture() function to see if a gesture has been detected. If a value greater than 0 is returned, a gesture has been detected.

# Uncomment and set the rotation if depending on how your sensor is mounted.
# apds.rotation = 270 # 270 for CLUE

apds.enable_proximity = True
apds.enable_gesture = True

while True:
  gesture = apds.gesture()
  if gesture == 1:
    print("up")
  if gesture == 2:
    print("down")
  if gesture == 3:
    print("left")
  if gesture == 4:
    print("right")

Color/Light Measurement

To get a color measurement, first enable the color/light engine, wait for color data to arrive, then read the color_data values.

apds.enable_color = True

while True:
    while not apds.color_data_ready:
        time.sleep(0.005)

    r, g, b, c = apds.color_data
    print("r: {}, g: {}, b: {}, c: {}".format(r, g, b, c))

Interrupt Pin

This sensor has an interrupt pin can be asserted (pulled low) if proximity is detected outside of a specified window of values.

For boards with a built-in APDS-9960 this interupt pin will already be defined. For example, on the Clue and Feather nRF52840 Sense boards this pin is mapped to board.PROXIMITY_LIGHT_INTERRUPT and on the Proximity Trinkey it is mapped to board.INTERRUPT.

int_pin = digitalio.DigitalInOut(board.D5)
int_pin.switch_to_input(pull=digitalio.Pull.UP)

Proximity Detection

With the interrupt pin set up we can define a threshold and enable the assertion of the sensor’s interrupt pin by the proximity engine before enabling the proximity engine itself.

In this configuration, the sensor’s interrupt pin will be asserted when an object is close to the sensor. After checking on the interrupt it can be cleared using clear_interrupt()

apds.enable_proximity = True

# set the interrupt threshold to fire when proximity reading goes above 175
apds.proximity_interrupt_threshold = (0, 175)

# assert interrupt pin on internal proximity interrupt
apds.enable_proximity_interrupt = True

# enable the sensor's proximity engine
apds.enable_proximity = True

while True:
  if not interrupt_pin.value:
    print(apds.proximity)

    # clear the interrupt
    apds.clear_interrupt()

Initiaization Options

By default, when the driver is initialized, the APDS-9960 sensor’s internal settings are reset and sensible defaults are applied to several low-level settings that should work well for most use cases.

If either the “reset” or “set defaults” behaviors (or both) aren’t desired, they can be individually disabled via init kwargs.

apds = APDS9960(i2c, reset=False, set_defaults=False)

Documentation

API documentation for this library can be found on Read the Docs.

For information on building library documentation, please check out this guide.

Contributing

Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.

Building locally

To build this library locally you’ll need to install the circuitpython-travis-build-tools package.

Once installed, make sure you are in the virtual environment:

Then run the build:

Sphinx documentation

Sphinx is used to build the documentation based on rST files and comments in the code. First, install dependencies (feel free to reuse the virtual environment from above):

python3 -m venv .venv
source .venv/bin/activate
pip install Sphinx sphinx-rtd-theme

Now, once you have the virtual environment activated:

cd docs
sphinx-build -E -W -b html . _build/html

This will output the documentation to docs/_build/html. Open the index.html in your browser to view them. It will also (due to -W) error out on any warning like Travis will. This is a good way to locally verify it will pass.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

adafruit-circuitpython-apds9960-3.1.10.tar.gz (40.2 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file adafruit-circuitpython-apds9960-3.1.10.tar.gz.

File metadata

File hashes

Hashes for adafruit-circuitpython-apds9960-3.1.10.tar.gz
Algorithm Hash digest
SHA256 7d0c7bada20bf3aedc249242175ca450aac8c2e208fba4ffbe79b935c6901f7d
MD5 5381f8fe405892ec87199f876410e8a7
BLAKE2b-256 054afac181f2c21d2bdc377c1ddc6f7c0b16e1486a628a357c66f6aa1841f323

See more details on using hashes here.

File details

Details for the file adafruit_circuitpython_apds9960-3.1.10-py3-none-any.whl.

File metadata

File hashes

Hashes for adafruit_circuitpython_apds9960-3.1.10-py3-none-any.whl
Algorithm Hash digest
SHA256 4be1f8b247634d9faf345f9d0bbed692ce19b0ad5af23f883fa2d4101305c757
MD5 ec2361b0c1783fc5def3e165375e968b
BLAKE2b-256 4b6e207f10ad9f91701f281bfe7cd5769ba23d8d00e09061917cf7538f56ba65

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page