MicroPython driver for the Broadcom APDS-9960 proximity, gesture, color, and ambient light sensor.
This library is a port of python-apds9960.
| Parameter | Value |
|---|---|
| Device | APDS-9960 |
| Interface | I²C |
| Default I²C address | 0x39 |
| Device ID | 0xAB |
| Functions | ALS, RGB, proximity, gesture |
| Proximity range | 0–255 (relative) |
| ALS resolution | 16-bit per channel |
- Ambient light sensing (ALS) — clear channel
- RGB color sensing — red, green, blue channels
- Proximity detection — object distance estimation
- Gesture detection — directional gestures (up, down, left, right, near, far)
- Power management (on/off)
- Configurable gains, LED drive, and thresholds
- Interrupt support for light, proximity, and gesture
from machine import I2C
from apds9960 import uAPDS9960
i2c = I2C(1)
sensor = uAPDS9960(i2c)
# Read ambient light
ambient = sensor.ambient_light()
print("Ambient light:", ambient)
# Read proximity
prox = sensor.proximity()
print("Proximity:", prox)sensor.device_id()— read device ID (expected0xAB)sensor.power_on()— enable the sensorsensor.power_off()— disable the sensorsensor.data_ready()—Truewhen light and proximity data are readysensor.get_mode()— read current mode registersensor.set_mode(mode, enable=True)— enable or disable a sensor mode
sensor.ambient_light()— read ambient (clear) light valuesensor.red_light()— read red channelsensor.green_light()— read green channelsensor.blue_light()— read blue channelsensor.light_ready()—Truewhen light data is available
sensor.enable_light_sensor(interrupts=True)— enable ALSsensor.disable_light_sensor()— disable ALS
sensor.get_ambient_light_gain()/sensor.set_ambient_light_gain(value)— ALS gain (0=1x, 1=4x, 2=16x, 3=64x)sensor.get_light_int_low_threshold()/sensor.set_light_int_low_threshold(value)— low interrupt thresholdsensor.get_light_int_high_threshold()/sensor.set_light_int_high_threshold(value)— high interrupt thresholdsensor.get_ambient_light_int_enable()/sensor.set_ambient_light_int_enable(enable)— interrupt enablesensor.clear_ambient_light_int()— clear light interrupt
sensor.proximity()— read proximity value (0–255)sensor.proximity_ready()—Truewhen proximity data is available
sensor.enable_proximity_sensor(interrupts=True)— enable proximitysensor.disable_proximity_sensor()— disable proximity
sensor.get_proximity_gain()/sensor.set_proximity_gain(gain)— proximity gain (0=1x, 1=2x, 2=4x, 3=8x)sensor.get_led_drive()/sensor.set_led_drive(drive)— LED drive strength (0=100mA, 1=50mA, 2=25mA, 3=12.5mA)sensor.get_led_boost()/sensor.set_led_boost(boost)— LED boostsensor.get_prox_int_low_thresh()/sensor.set_prox_int_low_thresh(value)— low proximity thresholdsensor.get_prox_int_high_thresh()/sensor.set_prox_int_high_thresh(value)— high proximity thresholdsensor.get_proximity_int_low_threshold()/sensor.set_proximity_int_low_threshold(value)— low threshold (alternative)sensor.get_proximity_int_high_threshold()/sensor.set_proximity_int_high_threshold(value)— high threshold (alternative)sensor.get_proximity_int_enable()/sensor.set_proximity_int_enable(enable)— interrupt enablesensor.clear_proximity_int()— clear proximity interruptsensor.get_prox_photo_mask()/sensor.set_prox_photo_mask(mask)— photodiode masksensor.get_prox_gain_comp_enable()/sensor.set_prox_gain_comp_enable(enable)— gain compensation
sensor.enable_gesture_sensor()
if sensor.is_gesture_available():
gesture = sensor.gesture()sensor.gesture()— read detected gesturesensor.is_gesture_available()—Truewhen a gesture is pending
Note: Gesture detection is not auto-enabled. Call enable_gesture_sensor() before polling gesture().
sensor.enable_gesture_sensor(interrupts=True)— enable gesture detectionsensor.disable_gesture_sensor()— disable gesture detection
sensor.get_gesture_enter_thresh()/sensor.set_gesture_enter_thresh(value)— entry thresholdsensor.get_gesture_exit_thresh()/sensor.set_gesture_exit_thresh(value)— exit thresholdsensor.get_gesture_gain()/sensor.set_gesture_gain(value)— gesture gainsensor.get_gesture_led_drive()/sensor.set_gesture_led_drive(value)— gesture LED drivesensor.get_gesture_wait_time()/sensor.set_gesture_wait_time(value)— wait time between gesture cyclessensor.get_gesture_int_enable()/sensor.set_gesture_int_enable(enable)— gesture interrupt enablesensor.get_gesture_mode()/sensor.set_gesture_mode(enable)— gesture mode
sensor.reset_gesture_parameters()— reset internal gesture statesensor.process_gesture_data()— process raw gesture FIFO datasensor.decode_gesture()— decode gesture direction from processed data
APDS9960InvalidDevId— raised when device ID does not match0xABAPDS9960InvalidMode— raised for invalid mode selection
| File | Description |
|---|---|
ambient_light.py |
Read ambient light and RGB values |
proximity.py |
Detect nearby objects |
gesture.py |
Detect directional gestures |
color_lamp.py |
Reactive color lamp with auto-calibration and OLED display. Dependency: ssd1327 |
light_thermein.py |
Theremin controlled by light using the buzzer |
mpremote mount lib/apds9960 run lib/apds9960/examples/ambient_light.py- Ambient light and proximity reads automatically enable their respective sensors when needed (lazy activation).
- Gesture detection must be explicitly enabled before polling.
- Both
APDS9960anduAPDS9960(MicroPython-optimized) classes are exported. - The light theremin example requires the buzzer connected on the PWM port, pin "SPEAKER"