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