APDS-9960 Light Sensor

This documentation includes installation guidelines and sample code for your hardware.

Python Library

Turta_APDS9960 library is responsible for communicating with the APDS-9960 sensor.

To install the library from the (PyPI) Python Package Index, enter the following command to the console.

pip install turta-iothat

Add the following statement to the top of your Python code.

from turta_iothat import Turta_APDS9960

Then, create an instance of the Turta_APDS9960 class.

apds9960 = Turta_APDS9960.APDS9960Sensor()

Now you're ready to access the library by calling the apds9960 instance.

Initialization

The library automatically initializes the required components when its instance is being created, so there is no need to do a manual initialization.

Basic Members

Read Ambient Light

Reads the ambient light value.

int read_ambient_light()
  • Parameters

    • None

  • Returns

    • Int: Ambient light.

Read RGB Light

Reads the RGB light values.

int[] read_rgb_light()
  • Parameters

    • None

  • Returns

    • Int[]: Red, green and blue light values respectively.

Read Proximity

Reads the proximity value.

int read_proximity()
  • Parameters

    • None

  • Returns

    • Int: Proximity value.

Advanced Members

Set Mode

Toggles the sensor modes.

set_mode(ambient_and_rgb_light_enabled, proximity_detection_enabled, gesture_recognition_enabled):
  • Parameters

    • Bool ambient_and_rgb_light_enabled: Ambient light and RGB light sense.

    • Bool proximity_detection_enabled: Proximity detection.

    • Bool gesture_recognition_enabled: Gesture recognition. (Not implemented.)

  • Returns

    • None

Samples

You can copy the example code from https://github.com/Turta-io/IoTHAT/tree/master/Samples/Raspbian/Python address, and then copy it to the Raspberry Pi. There is one example of this library.

APDS9960 Sample App

This application demonstrates ambient light, RGB light, and proximity readings.

To run the example:

  1. Copy the library and sample code to a folder on the Raspberry Pi.

  2. Open terminal, and go to the folder you copied the codes.

  3. Run the sample with the following command:

python APDS9960SampleApp.py

Sample Code

APDS9960SampleApp.py
#! /usr/bin/python

import time
from turta_iothat import Turta_APDS9960
#Install IoT HAT library with "pip install turta-iothat"

#Initialize
apds9960 = Turta_APDS9960.APDS9960Sensor()

try:
    while True:
        #Read & print ambient light
        print("Ambient Light...: " + str(apds9960.read_ambient_light()))

        #Read & print color values
        rgb = apds9960.read_rgb_light()
        print("Red.............: " + str(rgb[0]))
        print("Green...........: " + str(rgb[1]))
        print("Blue............: " + str(rgb[2]))

        #Read & print proximity
        print("Proximity.......: " + str(apds9960.read_proximity()))

        #Rest a bit
        print("-----")
        time.sleep(0.5)

#Exit on CTRL+C
except KeyboardInterrupt:
    print('Bye.')

Result

When you run the sample, it prints ambient light, RGB light and proximity readings to the terminal. The example output should be like this:

Ambient Light...: 287
Red.............: 124
Green...........: 345
Blue............: 212
Proximity.......: 132
-----

To stop the Python script, just press CTRL + C.

Last updated