API Reference
adafruit_apds9999
CircuitPython driver for Broadcom APDS-9999 Light + RGB + Proximity
Author(s): Tim Cocks
Implementation Notes
Hardware:
Software and Dependencies:
Adafruit CircuitPython firmware for the supported boards: https://circuitpython.org/downloads
Adafruit’s Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
Adafruit’s Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
- class adafruit_apds9999.APDS9999(i2c_bus: I2C, address: int = _APDS9999_DEFAULT_ADDR)
CircuitPython driver for the Broadcom APDS-9999 Digital Proximity and RGB Color Sensor.
- Parameters:
Quickstart: Importing and using the device
Here is an example of using the
APDS9999. First you will need to import the libraries to use the sensorimport board from adafruit_apds9999 import APDS9999
Once this is done you can define your
board.I2Cobject and define your sensor objecti2c = board.I2C() # uses board.SCL and board.SDA sensor = APDS9999(i2c)
Now you have access to the sensor data
r, g, b, ir = sensor.color_data proximity = sensor.proximity
- calculate_lux(green_count: int) float
Calculate illuminance in lux from a raw green channel count.
Reads the current
light_gainandlight_resolutionsettings from the device and applies the appropriate scale factor from the lookup table below.RES_13BITis not supported and will raise aValueError.Lux factor table (rows = gain, columns = resolution):
20-bit
19-bit
18-bit
17-bit
16-bit
GAIN_1X0.136
0.273
0.548
1.099
2.193
GAIN_3X0.045
0.090
0.180
0.359
0.722
GAIN_6X0.022
0.045
0.090
0.179
0.360
GAIN_9X0.015
0.030
0.059
0.119
0.239
GAIN_18X0.007
0.015
0.029
0.059
0.117
- property led_current: int
The IR LED drive current used during proximity measurements.
Must be a
LedCurrentvalue:LedCurrent.MA_1010 mALedCurrent.MA_2525 mA
- property led_frequency: int
The IR LED pulse modulation frequency used during proximity measurements.
Must be a
LedFrequencyvalue:LedFrequency.KHZ_60LedFrequency.KHZ_70LedFrequency.KHZ_80LedFrequency.KHZ_90LedFrequency.KHZ_100
- led_pulses
number of LED pulses emitted per proximity measurement
- property light_gain: int
The analogue gain applied to all light sensor channels.
Must be a
LightGainvalue:LightGain.GAIN_1XLightGain.GAIN_3XLightGain.GAIN_6XLightGain.GAIN_9XLightGain.GAIN_18X
- property light_interrupt_channel: int
The light sensor channel compared against the interrupt thresholds.
Must be a
LightInterruptChannelvalue:LightInterruptChannel.IRLightInterruptChannel.GREENLightInterruptChannel.REDLightInterruptChannel.BLUE
- light_interrupt_enabled
Enable or disable the light sensor interrupt
- property light_measurement_rate: int
How frequently the light sensor takes a reading.
Must be a
LightMeasurementRatevalue:LightMeasurementRate.RATE_25MSLightMeasurementRate.RATE_50MSLightMeasurementRate.RATE_100MS(default)LightMeasurementRate.RATE_200MSLightMeasurementRate.RATE_500MSLightMeasurementRate.RATE_1000MSLightMeasurementRate.RATE_2000MS
- property light_persistence: int
Number of consecutive out-of-threshold light readings before the interrupt flag is asserted.
Valid range is 0–15.
- property light_resolution: int
The ADC resolution of the light sensor channels.
Must be a
LightResolutionvalue:LightResolution.RES_20BIT20-bit, 400 ms conversionLightResolution.RES_19BIT19-bit, 200 ms conversionLightResolution.RES_18BIT18-bit, 100 ms conversionLightResolution.RES_17BIT17-bit, 50 ms conversionLightResolution.RES_16BIT16-bit, 25 ms conversionLightResolution.RES_13BIT13-bit, 3.125 ms conversion
- light_sensor_enabled
Enable or disable the light sensor
- light_sleep_after_interrupt
Disable the light sensor after interrupt. You must also read
main_statusafter the interrupt for the sleep to take effect. Once asleep, the sensor can be re-enabled withlight_sensor_enabled
- light_threshold_high
Light threshold high 20 bits
- light_threshold_low
Light threshold low 20 bits
- property light_variance: int
The count variance required to trigger a light interrupt when
light_variance_modeis enabled.Must be a
LightVariancevalue:LightVariance.VAR_8LightVariance.VAR_16LightVariance.VAR_32LightVariance.VAR_64LightVariance.VAR_128LightVariance.VAR_256LightVariance.VAR_512LightVariance.VAR_1024
- light_variance_mode
Variance mode (True) vs threshold mode (False) for the light interrupt.
- property main_status: Tuple[bool, bool, bool, bool, bool, bool]
Read all status flags from the MAIN_STATUS register in a single I2C transaction. Main status must be read after an interrupt is triggered to reset the interrupt.
Warning
Reading this register clears all status bits on the device.
Returns a tuple of six booleans:
(proximity_data_ready, proximity_interrupt, proximity_logic, light_data_ready, light_interrupt, power_on_reset)proximity_data_readynew proximity data is availableproximity_interruptproximity interrupt has been triggeredproximity_logiccurrent state of the proximity logic signallight_data_readynew light sensor data is availablelight_interruptlight sensor interrupt has been triggeredpower_on_reseta power-on reset has occurred since last read
- property proximity: int
The current proximity sensor reading as an 11-bit count (0–2047).
Each time this property is read the overflow flag is captured and can be checked immediately afterward via
proximity_read_overflow.
- proximity_analog_cancellation
Analog proximity cancellation level 5 bits
- proximity_cancellation
Digital proximity cancellation level 11 bits
- proximity_interrupt_enabled
Enable or disable the proximity interrupt. You must also read
main_statusafter the interrupt is triggered to reset it.
- property proximity_measurement_rate: int
How frequently the proximity sensor takes a reading.
Must be a
ProximityMeasurementRatevalue:ProximityMeasurementRate.RATE_6MS6.25 msProximityMeasurementRate.RATE_12MS12.5 msProximityMeasurementRate.RATE_25MS25 msProximityMeasurementRate.RATE_50MS50 msProximityMeasurementRate.RATE_100MS100 ms (default)ProximityMeasurementRate.RATE_200MS200 msProximityMeasurementRate.RATE_400MS400 ms
Note
CircuitPython makes no guarantee about realtime behavior, the faster measurement rates may take longer than indicated.
- property proximity_persistence: int
Number of consecutive out-of-threshold proximity readings before the interrupt flag is asserted.
Valid range is 0–15.
- property proximity_read_overflow: bool
Trueif the proximity sensor was saturated during the last read ofproximity. Updated on everyproximityread; read-only.
- property proximity_resolution: int
The ADC resolution of the proximity sensor.
Must be a
ProximityResolutionvalue:ProximityResolution.RES_8BIT(hardware default)ProximityResolution.RES_9BITProximityResolution.RES_10BITProximityResolution.RES_11BIT
- proximity_sensor_enabled
Enable or disable the proximity sensor
- proximity_sleep_after_interrupt
Disable the proximity sensor after interrupt. You must also read
main_statusafter the interrupt for the sleep to take effect. Once asleep, the sensor can be re-enabled withproximity_sensor_enabled
- proximity_threshold_high
Proximity threshold high 11 bits
- proximity_threshold_low
Proximity threshold low 11 bits
- reset()
Software reset the APDS9999
- property rgb: Tuple[int, int, int]
The RGB light sensor channels as a tuple
(red, green, blue)mapped to the range 0-255. For full 20bit resolution setlight_resolutiontoLightResolution.RES_20BITand usergb_irinstead ofrgb`- Returns:
(red, gree, blue)
- property rgb_ir: Tuple[int, int, int, int]
All four light sensor channels as a tuple
(red, green, blue, ir).Each value is up to 20-bit count (0–1048575) read in a single 12-byte burst starting at the IR data register. See
light_resolutionfor bit depth setting, lower resolution will result in lower maximum values.
- rgb_mode
Enable or disable RGB mode. True is RGB mode, False is ALS mode
- class adafruit_apds9999.CV
Constant value class helper for enums.
- class adafruit_apds9999.LedCurrent
LED drive current settings for the PS_VCSEL register (0x01), bits 2:0.
Controls the IR LED current used during proximity measurements.
Setting
Current
LedCurrent.MA_1010 mA
LedCurrent.MA_2525 mA
- class adafruit_apds9999.LedFrequency
LED pulse frequency settings for the PS_VCSEL register (0x01), bits 6:4.
Controls the modulation frequency of the IR LED pulses used during proximity measurements.
Setting
Frequency
LedFrequency.KHZ_6060 kHz
LedFrequency.KHZ_7070 kHz
LedFrequency.KHZ_8080 kHz
LedFrequency.KHZ_9090 kHz
LedFrequency.KHZ_100100 kHz
- class adafruit_apds9999.LightGain
Light sensor gain settings for the LS_GAIN register (0x05).
Controls the analogue gain applied to the light sensor channels.
Setting
Gain
LightGain.GAIN_1X1x gain
LightGain.GAIN_3X3x gain
LightGain.GAIN_6X6x gain
LightGain.GAIN_9X9x gain
LightGain.GAIN_18X18x gain
- class adafruit_apds9999.LightInterruptChannel
Light sensor interrupt channel settings for the INT_CFG register (0x19), bits 5:4.
Selects which light sensor channel is compared against the interrupt thresholds.
Setting
Channel
LightInterruptChannel.IRIR channel
LightInterruptChannel.GREENGreen channel
LightInterruptChannel.REDRed channel
LightInterruptChannel.BLUEBlue channel
- class adafruit_apds9999.LightMeasurementRate
Light sensor measurement rate settings for the LS_MEAS_RATE register (0x04), bits 2:0.
Controls how frequently the light sensor takes a reading. The measurement rate should be set equal to or slower than the resolution’s conversion time to avoid reading stale data.
Setting
Rate
LightMeasurementRate.RATE_25MS25 ms
LightMeasurementRate.RATE_50MS50 ms
LightMeasurementRate.RATE_100MS100 ms (default)
LightMeasurementRate.RATE_200MS200 ms
LightMeasurementRate.RATE_500MS500 ms
LightMeasurementRate.RATE_1000MS1000 ms
LightMeasurementRate.RATE_2000MS2000 ms
- class adafruit_apds9999.LightResolution
Light sensor ADC resolution settings for the LS_MEAS_RATE register (0x04), bits 6:4.
Higher resolution increases accuracy but also increases conversion time.
Setting
Resolution
Conversion time
LightResolution.RES_20BIT20-bit
400 ms
LightResolution.RES_19BIT19-bit
200 ms
LightResolution.RES_18BIT18-bit
100 ms
LightResolution.RES_17BIT17-bit
50 ms
LightResolution.RES_16BIT16-bit
25 ms
LightResolution.RES_13BIT13-bit
3.125 ms
- class adafruit_apds9999.LightVariance
Light sensor variance threshold settings for the LS_THRES_VAR register (0x27), bits 2:0.
Sets the count variance required to trigger a light interrupt when
light_variance_modeis enabled.Setting
Variance count
LightVariance.VAR_88
LightVariance.VAR_1616
LightVariance.VAR_3232
LightVariance.VAR_6464
LightVariance.VAR_128128
LightVariance.VAR_256256
LightVariance.VAR_512512
LightVariance.VAR_10241024
- class adafruit_apds9999.ProximityMeasurementRate
Proximity sensor measurement rate settings for the PS_MEAS_RATE register (0x03), bits 2:0.
Controls how frequently the proximity sensor takes a reading.
Note
CircuitPython makes no guarantee about realtime behavior, the faster measurement rates may take longer than indicated.
Setting
Rate
ProximityMeasurementRate.RATE_6MS6.25 ms
ProximityMeasurementRate.RATE_12MS12.5 ms
ProximityMeasurementRate.RATE_25MS25 ms
ProximityMeasurementRate.RATE_50MS50 ms
ProximityMeasurementRate.RATE_100MS100 ms (default)
ProximityMeasurementRate.RATE_200MS200 ms
ProximityMeasurementRate.RATE_400MS400 ms
- class adafruit_apds9999.ProximityResolution
Proximity sensor ADC resolution settings for the PS_MEAS_RATE register (0x03), bits 4:3.
Higher resolution increases accuracy at the cost of a longer conversion time.
Setting
Resolution
ProximityResolution.RES_8BIT8-bit
ProximityResolution.RES_9BIT9-bit
ProximityResolution.RES_10BIT10-bit
ProximityResolution.RES_11BIT11-bit