Arduino_APDS9960 - readProximity()

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

Syntax

APDS.readProximity()

Parameters

None.

Returns

The detected proximity that may range from 0 to 255 where 0 is the closest and 255 is the farthest. The function returns -1 in case of error.

Example

/*
  APDS-9960 - Proximity Sensor

  This example reads proximity data from the on-board APDS-9960 sensor of the
  Nano 33 BLE Sense and prints the proximity value to the Serial Monitor
  every 100 ms.

  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 proximity reading is available.
  if (APDS.proximityAvailable()) {
    // Read the proximity where:
    // - 0   => close
    // - 255 => far
    // - -1  => error
    int proximity = APDS.readProximity();

    // Print value to the Serial Monitor.
    Serial.println(proximity);
  }

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

See also