Circuit Python tutorials in Robotics II
View the Project on GitHub MrPrattASH/Robotics-II-Circuit-Python
In this tutorial, you’ll learn how to use the APDS-9960 sensor in color mode. This sensor can detect red, green, blue, and clear light. You’ll learn how to wire the sensor, understand how it works, see a code example, and complete some challenges to test your understanding.
To wire the APDS-9960 sensor using StemmaQT, follow these steps:
The APDS-9960 sensor has an integrated color light sensor that can measure the intensity of red, green, blue, and clear light.
The sensor connects on a new type of communication protocol, i2c.
I2C (Inter-Integrated Circuit) is a communication protocol commonly used in microcontrollers and sensors to facilitate communication between devices. It allows multiple “slave” devices to communicate with one or more “master” controllers using only two wires, making it a simple and efficient method for data transfer. Effectively we could connect 20+ sensors to these single 2 pins and be able to interact with them all, rather than needing 20 individual digital/analog pins.
Here’s a basic code example to get started with the APDS-9960 sensor in color mode:
Ensure you have the adafruit_adps9960 library on your CIRCUITPY
Lib folder.
import board
import busio
import time
import adafruit_apds9960.apds9960
# Set up I2C connection
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_apds9960.apds9960.APDS9960(i2c)
# Enable color sensing
sensor.enable_color = True
while True:
r, g, b, c = sensor.color_data
print("Red:", r, "Green:", g , "Blue:", b, "Clear:", c)
time.sleep(1)
Wire up constantly-on Red, Green, and Blue LEDs to test the color detection capabilities of the APDS-9960. If you’ve forgotten how to wire LEDs in constant power, refer back to this tutorial. Try changing the ambient light conditions and see how the sensor readings change.
Create a function that takes the red, green, and blue values as inputs and returns a string representing the detected color (e.g., “Red”, “Green”, “Blue”, “Unknown”).