Robotics-II-Circuit-Python

Circuit Python tutorials in Robotics II

View the Project on GitHub MrPrattASH/Robotics-II-Circuit-Python

Table of Contents

APDS-9960 Color Sensor Tutorial

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.

Video Tutorial


Text Tutorial

Wiring Diagram

To wire the APDS-9960 sensor using StemmaQT, follow these steps:

  1. Connect the Blue Wire (Data): Connect the blue wire to the data (SDA) pin on your microcontroller board.
  2. Connect the Yellow Wire (Clock): Connect the yellow wire to the clock (SCL) pin on your microcontroller board.
  3. Power and Ground:
    • Connect the red wire to the 3.3V or 5V power pin.
    • Connect the black wire to the ground (GND) pin.

How the Color Sensor Works

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 Communication

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.

Key Components of I2C:

  1. SCL (Serial Clock Line):
    • SCL is the clock line that is controlled by the master device.
    • It synchronizes the data transfer between the master and slave devices.
    • Both the master and slave devices rely on SCL to know when to read or write data.
  2. SDA (Serial Data Line):
    • SDA is the data line used to transfer data between devices.
    • It is a bidirectional line, meaning it can be used by both master and slave devices to send and receive data.
    • Data on the SDA line is transferred in synchronization with the clock pulses on the SCL line.

How I2C Works:

Code Example

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)


Challenges

Challenge 1: Light Up RGB LEDs

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.

Challenge 2: Create a Color Detector

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”).

Click to reveal a hint for Challenge 3 You can use a simple threshold to decide if a color is dominant, e.g., if red > 100, green < 50, blue < 50, it's likely red. Experiment with different thresholds based on your readings.