Reference   Language | Libraries | Comparison | Changes

ArduinoAPDS9960 : APDS class

readColor()

Description

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 - is the red component of the read color
g - is the green component of the read color
b - is the blue component of the read color
a - is the ambient light intensity

Returns

None

Example

/*
  APDS9960 - Color Sensor

  This example reads Color data from the on-board APDS9960 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 APDS9960 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



Reference Home

Corrections, suggestions, and new documentation should be posted to the Forum.

The text of the Arduino reference is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.