Arduino Nano 33 Sense Rev2- APDS9960 Proximity sensor
What is APDS9960 Proximity sensor?
The APDS-9960 is a compact, multifunction digital sensor used for gesture recognition, proximity sensing, ambient light measurement, and RGB color detection. It is widely used in mobile devices, wearables, robotics, and Arduino/embedded projects. Know more.
| Feature | APDS-9960 | HC-SR04 |
|---|---|---|
| Sensor type | Optical — IR-based (proximity, gesture, RGB, ambient light) | Ultrasonic — sound pulse echo distance sensor |
| Primary function | Proximity & gesture detection; color & ambient light sensing | Distance measurement (range to an object) |
| Range | ≈ 0–10 cm (proximity); gesture ~10–20 cm depending on conditions | ≈ 2–400 cm (typical; best accuracy within ~2–200 cm) |
| Accuracy | Low–medium (suitable for presence/gesture, not precise distance) | Medium–high for distance (depends on surface and environment) |
| Interface | I²C (digital) | Digital Trigger / Echo pins (pulse timing) |
| Supply voltage | 3.3 V (common) | 5 V (often needs level shifting for 3.3 V MCUs) |
| Environment sensitivity | Affected by ambient IR/lighting and target reflectivity (dark/absorbent surfaces reduce response) | Unaffected by ambient light; affected by soft/porous or angled surfaces that absorb/deflect sound |
| Best use cases | Touchless gesture controls, near-field presence detection, color sensing, auto-brightness | Distance measurement, obstacle avoidance, simple robotics navigation |
| Extra features | Gesture recognition, RGB color sensing, ambient light sensor | None — dedicated to distance measurement |
| Typical cost | Moderate | Very low |
Library
To use this code you will need the Arduino_APDS9960 library.
We have a tutorial on how to install a library here.
Getting started - Proximity
To detect rotations and speed.
#include <Arduino_APDS9960.h>
int ledState = LOW;
unsigned long previousMillis = 0;
const long intervalLong = 1000;
const long intervalMed = 500;
const long intervalShort = 100;
void setup() {
Serial.begin(9600);
while (!Serial);
if (!APDS.begin()) {
Serial.println("Error initializing APDS9960 sensor!");
}
// set the LEDs pins as outputs
pinMode(LEDR, OUTPUT);
pinMode(LEDG, OUTPUT);
pinMode(LEDB, OUTPUT);
// turn all the LEDs off
digitalWrite(LEDR, HIGH);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, HIGH);
}
void loop() {
unsigned long currentMillis = millis();
// check if a proximity reading is available
if (APDS.proximityAvailable()) {
// read the proximity
// - 0 => close
// - 255 => far
// - -1 => error
int proximity = APDS.readProximity();
if (proximity > 150) {
if (currentMillis - previousMillis >= intervalLong) {
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the green LED with the ledState of the variable and turn off the rest
digitalWrite(LEDG, ledState);
digitalWrite(LEDR, HIGH);
digitalWrite(LEDB, HIGH);
}
}
else if(proximity > 50 && proximity <= 150){
if (currentMillis - previousMillis >= intervalMed) {
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the blue LED with the ledState of the variable and turn off the rest
digitalWrite(LEDB, ledState);
digitalWrite(LEDR, HIGH);
digitalWrite(LEDG, HIGH);
}
}
else {
if (currentMillis - previousMillis >= intervalShort) {
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the blue LED with the ledState of the variable and turn off the rest
digitalWrite(LEDR, ledState);
digitalWrite(LEDB, HIGH);
digitalWrite(LEDG, HIGH);
}
}
// print value to the Serial Monitor
Serial.println(proximity);
}
}
Getting started - Colour Detection
#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);
if (r > g & r > b)
{
digitalWrite(LEDR, LOW);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, HIGH);
}
else if (g > r & g > b)
{
digitalWrite(LEDG, LOW);
digitalWrite(LEDR, HIGH);
digitalWrite(LEDB, HIGH);
}
else if (b > g & b > r)
{
digitalWrite(LEDB, LOW);
digitalWrite(LEDR, HIGH);
digitalWrite(LEDG, HIGH);
}
else
{
digitalWrite(LEDR, HIGH);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, HIGH);
}
// print the values
Serial.print("Red light = ");
Serial.println(r);
Serial.print("Green light = ");
Serial.println(g);
Serial.print("Blue light = ");
Serial.println(b);
Serial.println();
// wait a bit before reading again
delay(500);
}
