Home Code Arduino and APDS-9960 sensor example

Arduino and APDS-9960 sensor example

by shedboy71
[lebox id="1"]

In this example we will connect a APDS-9960 sensor to an Arduino Uno

Lets look at some information about the APDS-9960

The Broadcom APDS-9960 is a digital RGB, ambient light, proximity and gesture sensor device in a single 8-pin package. The device has an I2C compatible interface providing red, green, blue, clear (RGBC), proximity and gesture sensing with IR LED. The RGB and ambient light sensing feature detects light intensity under various lighting conditions and through various attentuation materials including darkened glass. In addition, the integrated UV-IR blocking filter enables accurate ambient light and correlated color temperature sensing.

The proximity and gesture feature is factory-trimmed and calibrated to 100mm proximity detection distance without requiring customer calibrations. Gesture detection utilizes four directional photodiodes, integrated with visible blocking filter, to accurately sense simple UP-DOWN-RIGHT-LEFT gestures or more complex gestures. The addition of micro-optics lenses within the module provides high efficient transmission and reception of infrared energy. An internal state machine allows the device to be put into a low power state between RGBC, proximity and gesture measurements providing very low power consumption.

Connection

 

Arduino and APDS-9960

Arduino and APDS-9960

Parts List

Part Link
Arduino Uno UNO R3 CH340G/ATmega328P, compatible for Arduino UNO
Dupont cable Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire
APDS-9960 sensor (cjmcu-88) APDS-9960 proximity detection and non-contact gesture detection RGB and Gesture

Code

This is the gesture example from the sparkfun library – https://github.com/sparkfun/APDS-9960_RGB_and_Gesture_Sensor

[codesyntax lang=”cpp”]

#include <Wire.h>
#include <SparkFun_APDS9960.h>

// Pins
#define APDS9960_INT    2 // Needs to be an interrupt pin

// Constants

// Global Variables
SparkFun_APDS9960 apds = SparkFun_APDS9960();
int isr_flag = 0;

void setup() {

  // Set interrupt pin as input
  pinMode(APDS9960_INT, INPUT);

  // Initialize Serial port
  Serial.begin(9600);
  Serial.println();
  Serial.println(F("--------------------------------"));
  Serial.println(F("SparkFun APDS-9960 - GestureTest"));
  Serial.println(F("--------------------------------"));
  
  // Initialize interrupt service routine
  attachInterrupt(0, interruptRoutine, FALLING);

  // Initialize APDS-9960 (configure I2C and initial values)
  if ( apds.init() ) {
    Serial.println(F("APDS-9960 initialization complete"));
  } else {
    Serial.println(F("Something went wrong during APDS-9960 init!"));
  }
  
  // Start running the APDS-9960 gesture sensor engine
  if ( apds.enableGestureSensor(true) ) {
    Serial.println(F("Gesture sensor is now running"));
  } else {
    Serial.println(F("Something went wrong during gesture sensor init!"));
  }
}

void loop() {
  if( isr_flag == 1 ) {
    detachInterrupt(0);
    handleGesture();
    isr_flag = 0;
    attachInterrupt(0, interruptRoutine, FALLING);
  }
}

void interruptRoutine() {
  isr_flag = 1;
}

void handleGesture() {
    if ( apds.isGestureAvailable() ) {
    switch ( apds.readGesture() ) {
      case DIR_UP:
        Serial.println("UP");
        break;
      case DIR_DOWN:
        Serial.println("DOWN");
        break;
      case DIR_LEFT:
        Serial.println("LEFT");
        break;
      case DIR_RIGHT:
        Serial.println("RIGHT");
        break;
      case DIR_NEAR:
        Serial.println("NEAR");
        break;
      case DIR_FAR:
        Serial.println("FAR");
        break;
      default:
        Serial.println("NONE");
    }
  }
}

[/codesyntax]

 

Output

Here is the output that I saw in the serial monitor

APDS-9960 initialization complete
Gesture sensor is now running
UP
NEAR
FAR
FAR
UP
NONE
NONE
NEAR
NEAR
LEFT
NONE
RIGHT
LEFT
NONE
LEFT
RIGHT

 

Link

https://docs.broadcom.com/docs/AV02-4191EN

Share
[lebox id="2"]

You may also like

Leave a Comment