Arduino_APDS9960 - readColor()

Retrieve the color read from the sensor. You can check if a color has been read by the sensor and may be retrieved using the APDS.colorAvailable() function.

Syntax

Int r, g, b;
APDS.readColor(r, g, b);
Int a;
APDS.readColor(r, g, b, a);

Parameters

This function requires 3 or 4 integer variables as arguments where the read color will be stored:

  • r: the red component of the read color.
  • g: the green component of the read color.
  • b: the blue component of the read color.
  • a. the ambient light intensity.

Returns

None.

Example

/*
  APDS-9960 - Color Sensor

  This example reads color data from the on-board APDS-9960 sensor of the
  Nano 33 BLE Sense and prints the color RGB (red, green, blue) values
  to the Serial Monitor once a second.

  The circuit:
  - Arduino Nano 33 BLE Sense.

  This example code is in the public domain.
*/

#include <Arduino_APDS9960.h>

void setup() {
  Serial.begin(9600);
  while (!Serial);

  if (!APDS.begin()) {
    Serial.println("Error initializing APDS-9960 sensor.");
  }
}

void loop() {
  // Check if a color reading is available.
  while (!APDS.colorAvailable()) {
    delay(5);
  }

  int r, g, b;

  // Read the color.
  APDS.readColor(r, g, b);

  // Print the values:
  Serial.print("r = ");
  Serial.println(r);
  Serial.print("g = ");
  Serial.println(g);
  Serial.print("b = ");
  Serial.println(b);
  Serial.println();

  // Wait a bit before reading again.
  delay(1000);
}

See also