Simple test
Ensure your device works with this simple test.
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5import board
6from adafruit_apds9960.apds9960 import APDS9960
7
8i2c = board.I2C() # uses board.SCL and board.SDA
9# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
10apds = APDS9960(i2c)
11
12apds.enable_proximity = True
13
14while True:
15 print(apds.proximity)
16 time.sleep(0.2)
Proximity Example
Example illustrating proximity detection
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import board
5import digitalio
6from adafruit_apds9960.apds9960 import APDS9960
7
8i2c = board.I2C() # uses board.SCL and board.SDA
9# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
10int_pin = digitalio.DigitalInOut(board.D5)
11int_pin.switch_to_input(pull=digitalio.Pull.UP)
12apds = APDS9960(i2c)
13
14# set the interrupt threshold to fire when proximity reading goes above 175
15apds.proximity_interrupt_threshold = (0, 175)
16
17# assert the interrupt pin when the proximity interrupt is triggered
18apds.enable_proximity_interrupt = True
19
20# enable the sensor's proximity engine
21apds.enable_proximity = True
22
23while True:
24 # print the proximity reading when the interrupt pin goes low
25 if not int_pin.value:
26 print(apds.proximity)
27
28 # clear the interrupt
29 apds.clear_interrupt()
Gesture Example
Example illustrating gesture detection
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import board
5from adafruit_apds9960.apds9960 import APDS9960
6
7i2c = board.I2C() # uses board.SCL and board.SDA
8# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
9
10apds = APDS9960(i2c)
11apds.enable_proximity = True
12apds.enable_gesture = True
13
14# Uncomment and set the rotation if depending on how your sensor is mounted.
15# apds.rotation = 270 # 270 for CLUE
16
17while True:
18 gesture = apds.gesture()
19
20 if gesture == 0x01:
21 print("up")
22 elif gesture == 0x02:
23 print("down")
24 elif gesture == 0x03:
25 print("left")
26 elif gesture == 0x04:
27 print("right")
Color Example
Example illustrating color detection
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5import board
6from adafruit_apds9960.apds9960 import APDS9960
7from adafruit_apds9960 import colorutility
8
9i2c = board.I2C() # uses board.SCL and board.SDA
10# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
11apds = APDS9960(i2c)
12apds.enable_color = True
13
14
15while True:
16 # wait for color data to be ready
17 while not apds.color_data_ready:
18 time.sleep(0.005)
19
20 # get the data and print the different channels
21 r, g, b, c = apds.color_data
22 print("red: ", r)
23 print("green: ", g)
24 print("blue: ", b)
25 print("clear: ", c)
26
27 print("color temp {}".format(colorutility.calculate_color_temperature(r, g, b)))
28 print("light lux {}".format(colorutility.calculate_lux(r, g, b)))
29 time.sleep(0.5)