APDS-9930 ambient light and proximity sensor example

The APDS-9930 provides digital ambient light sensing (ALS), 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 (without front glass) 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

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

Typically you will by a breakout similar to this one

APS9930

Connection

Pin Description
GND Wemos Ground
VCC Wemos 3.3v
SCL Wemos D1
SDA Wemos D2

 

Code

Download the library from here. Download and install the Arduino IDE.

Open the Arduino IDE, open the Sketch menu, Include library, and hit Add .ZIP library. Select the zip file of the library you just downloaded. The Arduino IDE will automatically install it.

There are several examples, I used this one

#define PWM_LED_PIN       10
#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;
float max_light = 0;
 
void setup() {
  //analogReference(EXTERNAL);
  pinMode(PWM_LED_PIN, OUTPUT);
 
  // Initialize Serial port
  Serial.begin(9600);
  Serial.println();
  Serial.println(F("--------------------------------"));
  Serial.println(F("APDS-9930 - Ambient light sensor"));
  Serial.println(F("--------------------------------"));
 
  // 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!"));
  }
 
#ifdef DUMP_REGS
  /* Register dump */
  uint8_t reg;
  uint8_t val;
 
  for(reg = 0x00; reg <= 0x19; reg++) {
    if( (reg != 0x10) && \
        (reg != 0x11) )
    {
      apds.wireReadDataByte(reg, val);
      Serial.print(reg, HEX);
      Serial.print(": 0x");
      Serial.println(val, HEX);
    }
  }
  apds.wireReadDataByte(0x1E, val);
  Serial.print(0x1E, HEX);
  Serial.print(": 0x");
  Serial.println(val, HEX);
#endif
 
  // 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);
 
    if ( ambient_light > max_light ) {
      max_light = ambient_light;
    }
    ambient_light = map(ambient_light, 0, max_light, 0, 1023);
    analogWrite(PWM_LED_PIN, ambient_light);
 
  }
 
  // Wait 1 second before next reading
  delay(50);
}

 

Open the serial monitor and block the sensor and adjust the light on the sensor

Links

APDS-9930 Proximity Sensor Approaching and Non Contact Proximity Breakout on Amazon

LEAVE A REPLY

Please enter your comment!
Please enter your name here

*