Ks0267 keyestudio APDS-9930 Attitude Sensor Module

From Keyestudio Wiki
Jump to: navigation, search

Keyestudio APDS-9930 Attitude Sensor Module



Keyestudio APDS-9930 attitude sensor module mainly uses APDS-9930 chip. APDS-9930 in a single 8 pin package can provide the ambient light sensor which is compatible with I2C interface and the infrared LED proximity sensor. The ambient light sensor uses double light diode to approximate the visual response of low lumen human under 0.01 lux illumination, and its high sensitivity allows the device to operate in the back of dark glass. The proximity sensor which is completely adjusted can detect 100 mm object, and exempt the factory calibration requirements of terminal equipment as well as sub-components. From the bright sunlight to the dark room, proximity sensor’s proximity detection function can operate well. This module added micro optical lens can provide infrared energy efficient transmission and reception, which can reduce the overall power consumption. In addition, its internal state machine can make the device into a low power mode, bringing a very low average power consumption.isconnected normally.


Performance Parameters

  • Working Voltage:DC 3.3-3.8V
  • Output Current:0-20mA
  • Temperature Range:-40℃ to +85℃


  • Optical module integrated with ALS, infrared LED and proximity detector;
  • Ambient Light Sensing,similar to the human eye’s visual response;  
  • Programmable interruption function with upper and lower thresholds;
  • Up to 16-bit resolution;
  • High sensitivity of operation in the back of dark glass;
  • 0.01lux low lumen performance;
  • Proximity detection, fully calibrated to 100 mm detection;
  • Integrate infrared LED and synchronous LED driver;
  • Eliminate factory calibration for proximity sensors;
  • Programmable waiting timer, waiting state’s power consumption - 90μA (typical value);
  • Programmable range is from 2.7 milliseconds to 8 seconds;
  • Compatible with I2C interface, up to 400kHz (I2C fast mode);
  • Dedicated interruption pin;
  • Sleep mode power - 2.2μA (typical value).

Connection Diagram


Sample Code

Libraries Download of Wire and APDS9930: [1]

#define DUMP_REGS
#include <Wire.h>
#include <APDS9930.h>
// Pins
#define APDS9930_INT    2  // Needs to be an interrupt pin
#define LED_PIN         13 // LED for showing interrupt
// Constants
#define PROX_INT_HIGH   600 // Proximity level for interrupt
#define PROX_INT_LOW    0  // No far interrupt
// Global variables
APDS9930 apds = APDS9930();
float ambient_light = 0; // can also be an unsigned long
uint16_t ch0 = 0;
uint16_t ch1 = 1;
uint16_t proximity_data = 0;
volatile bool isr_flag = false;
void setup() {
	// Set LED as output
	pinMode(LED_PIN, OUTPUT);
	pinMode(APDS9930_INT, INPUT);
	// Initialize Serial port
	Serial.println(F("APDS-9930 - ProximityInterrupt"));
	// Initialize interrupt service routine
	attachInterrupt(digitalPinToInterrupt(APDS9930_INT), interruptRoutine, FALLING);
	// 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!"));
	// Adjust the Proximity sensor gain
	if (!apds.setProximityGain(PGAIN_2X)) {
		Serial.println(F("Something went wrong trying to set PGAIN"));
	// Set proximity interrupt thresholds
	if (!apds.setProximityIntLowThreshold(PROX_INT_LOW)) {
		Serial.println(F("Error writing low threshold"));
	if (!apds.setProximityIntHighThreshold(PROX_INT_HIGH)) {
		Serial.println(F("Error writing high threshold"));
	// Start running the APDS-9930 proximity sensor (interrupts)
	if (apds.enableProximitySensor(true)) {
		Serial.println(F("Proximity sensor is now running"));
	else {
		Serial.println(F("Something went wrong during sensor 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);

void loop() {
	// If interrupt occurs, print out the proximity level
	if (isr_flag) {
		// Read proximity level and print it out
		if (!apds.readProximity(proximity_data)) {
			Serial.println("Error reading proximity value");
		else {
			Serial.print("Proximity detected! Level: ");
			Serial.print("   ");
		// 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(F("  Ch0: "));
			Serial.print(F("  Ch1: "));
		// Turn on LED for a half a second
		digitalWrite(LED_PIN, HIGH);
		digitalWrite(LED_PIN, LOW);
		// Reset flag and clear APDS-9930 interrupt (IMPORTANT!)
		isr_flag = false;
		if (!apds.clearProximityInt()) {
			Serial.println("Error clearing interrupt");
void interruptRoutine() {
	isr_flag = true;

Test Result

Wiring as the above diagram and burning the code, after powered-on, open the serial monitor, as the graph shown below.



PDF File:

Libraries Download of APDS9930:

Libraries Download of Wire:

Buy from

Official Website