Home Code Arduino and APDS-9930 sensor example

Arduino and APDS-9930 sensor example

by shedboy71

The APDS-9930 provides digital ambient light sensing, IR LED and a complete proximity detection system in a single 8 pin package. The proximity function offers plug and play detection to 100 mm thus eliminating the need for factory calibration of the end equipment or sub-assembly. The proximity detection feature operates well from bright sunlight to dark rooms.
The wide dynamic range also allows for operation in short distance detection behind dark glass such as a cell phone.
In addition, an internal state machine provides the ability to put the device into a low power mode in between ALS and proximity measurements providing very low average power consumption. The ALS provides a photopic response to light intensity in very low light condition or behind a dark faceplate.

The proximity function is targeted specifically towards near field proximity applications. In cell phones, the proximity detection can detect when the user positions the phone close to their ear. The device is fast enough to provide proximity information at a high repetition rate needed when answering a phone call. This provides both improved “green” power saving capability and the added security to lock the computer when the user is not present.
The addition of the micro-optics lenses within the module, provide highly efficient transmission and reception of infrared energy which lowers overall power dissipation.

Features

– ALS, IR LED and Proximity Detector in an Optical Module
– Ambient Light Sensing (ALS)
– Approximates Human Eye Response
– Programmable Interrupt Function with Upper and Lower Threshold
– Up to 16-Bit Resolution
– High Sensitivity Operates Behind Darkened Glass
– Low Lux Performance at 0.01 lux Proximity Detection
– Fully Calibrated to 100 mm Detection
– Integrated IR LED and Synchronous LED Driver
– Wait State Power – 90 µA Typical
– Programmable from 2.7 ms to > 8 sec
– Up to 400 kHz (I2C Fast-Mode)
– Dedicated Interrupt Pin

Here is a module I bought

apds-9930

Connection and Layout

Pin Label Description
VL Optional power to the IR LED. Must be 3.0 – 4.5V
GND  ground.
VCC Must be 2.4 – 3.6V
SDA I2C data
SCL I2C clock
INT External interrupt pin.

And now a layout drawing to show this

arduino-and-apds9930_bb

Code

You need to install the following library – https://github.com/Davideddu/APDS9930 . This is an ambient light example, there are also proximity examples

I also recommend you take a look at the sparkfun library – https://github.com/sparkfun/APDS-9960_RGB_and_Gesture_Sensor/ and take a look at those examples as well

[codesyntax lang=”cpp”]

/****************************************************************

 Arduino Pin  APDS-9930 Board  Function
 
 3.3V         VCC              Power
 GND          GND              Ground
 A4           SDA              I2C Data
 A5           SCL              I2C Clock
****************************************************************/

#define DUMP_REGS

#include <Wire.h>
#include <APDS9930.h>

// Global Variables
APDS9930 apds = APDS9930();
float ambient_light = 0; // can also be an unsigned long
uint16_t ch0 = 0;
uint16_t ch1 = 1;

void setup() 
{
  
  // Initialize Serial port
  Serial.begin(9600);
  Serial.println();
  
  // Initialize APDS-9930 (configure I2C and initial values)
  if ( apds.init() ) 
  {
    Serial.println(F("APDS-9930 initialization complete"));
  } 
  else 
  {
    Serial.println(F("Something went wrong during APDS-9930 init!"));
  }
  
  // Start running the APDS-9930 light sensor (no interrupts)
  if ( apds.enableLightSensor(false) ) 
  {
    Serial.println(F("Light sensor is now running"));
  } 
  else 
  {
    Serial.println(F("Something went wrong during light sensor init!"));
  }

  // Wait for initialization and calibration to finish
  delay(500);
}


void loop() 
{
  
  // Read the light levels (ambient, red, green, blue)
  if (  !apds.readAmbientLightLux(ambient_light) ||
        !apds.readCh0Light(ch0) || 
        !apds.readCh1Light(ch1) ) {
    Serial.println(F("Error reading light values"));
  } 
  else 
  {
    Serial.print(F("Ambient: "));
    Serial.print(ambient_light);
    Serial.print(F("  Ch0: "));
    Serial.print(ch0);
    Serial.print(F("  Ch1: "));
    Serial.println(ch1);
  }
  
  // Wait 1 second before next reading
  delay(1000);
}

[/codesyntax]

 

 

Testing

Open the serial monitor

Ambient: 704.13 Ch0: 743 Ch1: 318
Ambient: 750.47 Ch0: 779 Ch1: 332
Ambient: 716.68 Ch0: 785 Ch1: 334
Ambient: 763.03 Ch0: 791 Ch1: 337
Ambient: 727.30 Ch0: 761 Ch1: 325
Ambient: 37.65 Ch0: 58 Ch1: 27
Ambient: 39.25 Ch0: 58 Ch1: 27
Ambient: 523.88 Ch0: 661 Ch1: 295
Ambient: 50.05 Ch0: 63 Ch1: 28
Ambient: 581.18 Ch0: 685 Ch1: 301
Ambient: 593.25 Ch0: 727 Ch1: 316
Ambient: 629.62 Ch0: 714 Ch1: 311

 

Links
Non-contact detection of proximity and gesture and posture RGB sensor APDS-9930

Share

You may also like

Leave a Comment