Python & CircuitPython

It's easy to use the APDS9960 sensor with CircuitPython or Python and the Adafruit CircuitPython APDS9960 module.  This module allows you to easily write Python code that reads the proximity and other readings 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.

CircuitPython Microcontroller Wiring

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

  • Board 3V to sensor VIN
  • Board GND to sensor GND
  • Board SCL to sensor SCL
  • Board SDA to sensor SDA

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

  • Pi 3V3 to sensor VIN
  • Pi GND to sensor GND
  • Pi SCL to sensor SCL
  • Pi SDA to sensor SDA

CircuitPython Installation of APDS9960 Library

Next you'll need to install the Adafruit CircuitPython APDS9960 library on your CircuitPython board.  

First make sure you are running the latest version of Adafruit CircuitPython for your board.

Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle.  Our introduction guide has a great page on how to install the library bundle 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_apds9960
  • adafruit_bus_device
  • adafruit_register

You can also download the adafruit_apds9960 folder from its releases page on Github.

Before continuing make sure your board's lib folder or root filesystem has the adafruit_apds9960, adafruit_bus_device, adafruit_register folders copied over.

Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

Python Installation of APDS9960 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!

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

  • sudo pip3 install adafruit-circuitpython-apds9960

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 more from the board's Python REPL.  Run the following code to import the necessary modules to initialize the I2C bus and sensor:

Download: file
import board
import busio
import adafruit_apds9960.apds9960
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_apds9960.apds9960.APDS9960(i2c)

Proximity Reading

To sense proximity you first must enable it:

Download: file
sensor.enable_proximity = True

Then call the proximity() function to retrieve the currently sensed proximity value.  This is a number from 0 to 255 where the higher the number the closer an object is to the sensor.  You won't be able to translate this into an absolute value in units like inches or millimeters, you can only see how it changes relative to other values.

Download: file
sensor.proximity()

Color Reading

In addition to proximity you can also measure the color of an object directly in front of the sensor.  Like with proximity you must enable it first:

Download: file
sensor.enable_color = True

Then read the color_data property to retrieve the red, green, blue, and clear color values as a 4-tuple of 16-bit values:

Download: file
r, g, b, c = sensor.color_data
print('Red: {0}, Green: {1}, Blue: {2}, Clear: {3}'.format(r, g, b, c))

Remember these are 16-bit values that will range from 0 to 65535.  A value of 0 means the minimum amount of color and a value of 65535 is the maximum amount of color.

Gesture Reading

Finally you can read simple up, down, left, right gestures that are sensed by the chip too.  Again first you enable gesture sensing:

Download: file
sensor.enable_gesture = True

Now you can call the gesture() function to read if a gesture was detected and what gesture it was.  However you probably want to call this function over and over until it returns a non-zero value, this way you can 'catch' when a gesture happens and remember it.

Run the following code that will wait in a loop for a gesture to be detected:

Download: file
gesture = sensor.gesture()
while gesture == 0:
    gesture = sensor.gesture()
print('Saw gesture: {0}'.format(gesture))

Wave a finger very closely in front of the sensor and you should see the REPL return so you can print out the gesture value.

You can convert the gesture value into its meaning with these numbers:

  • 0 = No gesture detected
  • 1 = Up gesture detected
  • 2 = Down gesture detected
  • 3 = Left gesture detected
  • 4 = Right gesture detected

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

For a complete example of sensing gesture and printing the direction see the gesture.py example from the library (or see all the examples in the library for more usage).  Save this as main.py on your board (being careful to modify it to use bitbangio if your board requires it) and view the output in the REPL.  As you move a finger in front of the sensor it will print out the direction of the gesture!

Temporarily unable to load content:
This guide was first published on Sep 06, 2017. It was last updated on Sep 06, 2017. This page (Python & CircuitPython) was last updated on Jun 24, 2019.