Davide's Blog

Use an APDS-9930 Ambient Light and Proximity Sensor with Python (Raspberry PI/PC)

This is a mobile optimized version of this page, view original page.

A few weeks ago I posted an article on how to find your computer’s hidden I2C port. Today I’m going to take it one step further and show you how to actually use it.

After making the Arduino library for the APDS-9930 ambient light and proximity sensor, I decided to port it to Python. In this post I’m going to show you how to use it.First, download the library from GitHub.

APDS-9930 Python Module at GitHub

Then install it.

sudo python setup.py install

Make sure you have followed the instructions on how to properly hook up the I2C device to your computer (look up a Raspberry PI pinout if you want to use that one instead) and enable it on Linux (while connection on the Raspberry PI and on computers will be different, the usage on a Linux OS is the same).

Then run this sample Python script as root:

It will print out some values gotten from the sensor. Note that if you import the library in a Python shell, you can type the name of an attribute and it will be formatted in a human-readable way. Their values, however, will still be usable in Python (ex. try printing them: an integer will be shown; you can call repr on them to get the human-readable string instead). Try these examples (note that all the constants are available in the apds9930.values module):

>>> a.mode
0b00000000

>>> a.proximity_gain
3 = PGAIN_8X

>>> a.led_drive
0 = LED_DRIVE_100MA

>>> print a.mode
0

>>> print a.proximity_gain
3

The complete documentation for the module is available at ReadTheDocs.

There is currently no way of attaching callbacks to the device’s interrupts, unless you’re using a Raspberry PI and have some normal I/O pins available. However I’m going to add some properties to be able to know if an interrupt is asserted by the device (no way to attach callbacks anyways).

That’s it for today! Make sure you let me know your thoughts in the comments below and report bugs/issues on GitHub!