(2/2) Switching LEDs using Proximity Sensor APDS-9960 module

Overview

Continuing our investigation about the APDS-9960 module, the integrated proximity sensor can sense distances up to a few centimeters (~6 centimeters).

In this simple project, 4 LEDs will be turned on via distance thresholds.

Hardware Used

You can buy all this Hardware at Createlabz.

Software Used

Libraries Used

Since we are using the same module, the Adafruit Library will be enough for this simple mini-project.

Application Description

According to www.ia.omron.com, there are three types of proximity dection systems : sensing metallic objects via eddy currents, recognizing electrical capacity changes in approaching objects, and utilizing magnets and reed switches.

For this particular module, An IR LED emits an infrared radiation (IR light) which bounces off on any nearby object or surface. The proximity distance is measured by the reflected IR light received by a photodiode. If the distance between the object and sensor is near, the reflected IR light intensity will be greater than distances afar. Thus the photodiode voltage increases in proportion to the IR light received, or distance.

Proximity Sensor in Action From (https://www.quora.com/How-do-proximity-sensors-work)

Since we are using the similar set-up from the gesture sensor, the only changes are the arrangements of pins and its corresponding pin numbers.

 

Set-up the Hardware

The hardware is pretty straightforward to set-up.

Code

 

/***************************************************************************
  This is a library for the APDS9960 digital proximity, ambient light, RGB, and gesture sensor
  This sketch puts the sensor in proximity mode and enables the interrupt
  to fire when proximity goes over a set value
  Designed specifically to work with the Adafruit APDS9960 breakout
  ----> http://www.adafruit.com/products/3595
  These sensors use I2C to communicate. The device's I2C address is 0x39
  Adafruit invests time and resources providing this open source code,
  please support Adafruit andopen-source hardware by purchasing products
  from Adafruit!
  Written by Dean Miller for Adafruit Industries.
  BSD license, all text above must be included in any redistribution
 ***************************************************************************/

#include "Adafruit_APDS9960.h"

//Initialization of variables
const int ledA = 8, ledB=9, ledC=10, ledD=11;

//create the APDS9960 object
Adafruit_APDS9960 apds;
int proximity_data = 0;
void setup() {
  Serial.begin(9600);
 
  pinMode(ledA, OUTPUT);
  pinMode(ledB, OUTPUT);
  pinMode(ledC, OUTPUT);
  pinMode(ledD, OUTPUT);

  if(!apds.begin()){
    Serial.println("failed to initialize device! Please check your wiring.");
  }
  else Serial.println("Device initialized!");
 apds.setProxGain(APDS9960_PGAIN_8X);
  //enable proximity mode
  apds.enableProximity();
}

void loop() {
  
  proximity_data = apds.readProximity(); //Fetch data from sensor
  if(proximity_data >  192){  
    digitalWrite(ledD, HIGH);
  }else{
    digitalWrite(ledD, LOW);
  }if(proximity_data >  64){
    digitalWrite(ledC, HIGH);
  }else{
    digitalWrite(ledC, LOW);
  }if(proximity_data > 32){
    digitalWrite(ledB, HIGH);
  }else{
    digitalWrite(ledB, LOW);
  }if(proximity_data > 16){
    digitalWrite(ledA, HIGH);
  }else{
    digitalWrite(ledA, LOW);
  }

  Serial.println(proximity_data);
  delay(250);
}

 

Code Breakdown

 

apds.begin()

This function returns TRUE when the the APDS-9960 is detected. Otherwise it will return FALSE.

apds.enableProximity();

As the name implies, it enables the proximity sensor of the module.

apds.readProximity();

This function returns the converted analog signal to discrete digital levels, which ranges from 0 to 255.

if(proximity_data >  192){  
    digitalWrite(ledD, HIGH);
  }else{
    digitalWrite(ledD, LOW);
  }if(proximity_data >  64){
    digitalWrite(ledC, HIGH);
  }else{
    digitalWrite(ledC, LOW);
  }if(proximity_data > 32){
    digitalWrite(ledB, HIGH);
  }else{
    digitalWrite(ledB, LOW);
  }if(proximity_data > 16){
    digitalWrite(ledA, HIGH);
  }else{
    digitalWrite(ledA, LOW);
  }

This snippet of code compares if the “proximity_data” is greater than the set distance thresholds. If it is greater, then the corresponding LED lights up. Otherwise the LED is turned off.

More library codes/functions are not used but explored below.

APDS9960_PGAIN_1X    /**<  1x gain  */
  APDS9960_PGAIN_2X    /**<  2x gain  */
  APDS9960_PGAIN_4X    /**<  4x gain */
  APDS9960_PGAIN_8X    /**< 8x gain  */

The arguments above adjusts the proximity sensor’s gain. To use the arguments, call the function in setup().

void setProxGain(GAIN);

where “GAIN” is one of the arguments shown above.

enableProximityInterrupt();
disableProximityInterrupt();

These functions will enable the Proximity Interrupt feature of the module. If enabled, the following functions can be used.

setProximityInterruptThreshold(uint8_t low, uint8_t high, uint8_t persistance = 4);
getProximityInterrupt();

 

Conclusion

The Gesture sensor can sense proximity and, with a few calibrations, return distance in centimeters or inches. It works by  using a photodiodes to sense the reflected IR light. As such, ambient light (from nearby light sources) can affect the proximity threshold.

 

References

https://learn.adafruit.com/adafruit-apds9960-breakout/arduino-wiring-and-test

https://cdn.sparkfun.com/assets/learn_tutorials/3/2/1/Avago-APDS-9960-datasheet.pdf

https://www.ia.omron.com/support/guide/41/introduction.html

The post (2/2) Switching LEDs using Proximity Sensor APDS-9960 module appeared first on CreateLabz.

Apds-9960Apds-9960 proximityArduinoGesture sensorKnowledgebaseLight sensorProximityProximity sensor

Leave a comment

All comments are moderated before being published