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

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.

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):

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!

There is a mobile optimized version of this page, view mobile Version.

3 Replies to “Use an APDS-9930 Ambient Light and Proximity Sensor with Python (Raspberry PI/PC)”

  1. Hi, I am trying to use your library and I am having trouble calling ambient_light_int_low_threshold() to set the value. It is giving and object is not callable error.

    1. Hi Alex,
      the reason why you’re having the issue is that you do not have to call the function, because it’s a property. Properties in Python are used just like normal attributes: when they’re accessed (for example print myobject.some_property), Python internally calls the “getter” method of that property (the method decorated with @property); when they’re written into (for instance myobject.some_property = "hello world"), Python calls the “setter” method and passes it the value you provided; when they’re deleted (ex. del myobject.some_property), Python calls the “deleter” method (which I haven’t implemented as it’s not needed in this use case).
      If you try to call the getter/setter yourself, you’ll get an error, as Python makes them unavailable. So if you want to set the low threshold, just use a.ambient_light_int_low_threshold = YOUR_VALUE

      You can learn more about properties (and become a fan of them just like me :D) here and here.
      I think they’re very useful in this specific use case as they allow to interface with remote values in a super-simple, so simple that it almost seems they’re stored locally.

Leave a Reply