HUM: APDS-9960

You are a beginner with Dasduino. Or electronics? A specific module caught your eye, but you do not know how to use it? Do not worry, HUM is here for you! How to Use Module (HUM) is a blog tutorials series by soldered where you will find all you need in order to begin working with your favorite module. Tutorials include: technical characteristics, work principle, instructions on how to connect module with Dasduino and the basic code. Everything else is left to your imagination.
INTRODUCTION
In this tutorial, we will introduce you to the APDS-9960 digital proximity, ambient light, RGB and gesture sensor. Gesture control allows us to detect hand movement to the left or right, and up or down, as well as some complex gestures such as moving your hand away from the sensor, or moving it towards the sensor. Due to its dimensions, abilities and consumption, it has found its purpose in mobile phones as a proximity detection sensor so to disable touchscreen (and consequently accidental touchscreen) while user is answering the phone, along with screenshot using hand movement and such. But, for starters, we will observe some characteristics of the sensor as well as its appearance.
Sensor characteristics:
• Power supply voltage: 5 VDC (header) 3.3 VDC (easyC)
• SDA and SCL input voltage: 0.8 VDC – 3.6 VDC
• LED diode’s voltage in the sensor: 3 VDC – 4.5 VDC
• Sensor current: 1 – 10 uA Sleep Mode, 38 uA Standby mode, 790 uA Proximity and gesture reading
• LED diode’s current in the sensor: 12.5 mA – 100 mA
• Type of communication: I2C (Two Wire)
• Maximum I2C frequency: 400 kHz
• Module dimensions: 36 x 22 mm
HOW DOES THE MODULE WORK?
The sensor on the module is composed of two parts: the sensor itself and the LED diode that serves to determine the proximity of an object. In the sensor, there are four photodiodes which serve to detect gestures (they are set up in a way to successfully detect hand motion in different directions) and four photodiodes that serve to detect the color and intensity of light.
Each of these four color photodiodes measures the intensity of light of a specific wavelenght ranges, so that we have a photodiode for red, green and blue color which is done in a way that there is a certain color filter in front of the photodiode. The last photodiode is clear, it only contains infrared and UV filter, and it detects the entire range of colors that photodiodes for red, green and blue detect together. It serves to determine the intensity of light coming to the sensor. In the image below, it is visible which range of colors does the individual photodiode detect.
Gesture detection is based on the observation of the amount of light intensity, provided by the infrared diode which is embedded in the sensor, that reflects back to photodiodes for gesture detection. In the image below, it is visible how a certain gesture is detected. U, L, D, R represent photodiodes for gesture detection. If the proximity of an object is detected first, on the D photodiode, then on the L and R and with a similar count (intensity), and in the end on U diode, it would mean that the downward movement has occured.
Gesture detection code can be adjusted in a way that the change on the INT pin occurs while detecting gestures, but also the same can be enabled for detecting an obstacle (object), and in that case, the sensor would inform us about it via INT pin. This allows making a quick and important operation in the code using interrupts.
HOW TO CONNECT IT?
The way Dasduino and our sensor will be connected is described below. If you choose to connect through the headers it has, keep in mind that you need to bring 5 VDC to the positive pin. If, however, you want to avoid the possibility of making a mistake during connection (you’ve chosen easyC), you will simply connect the sensor board with a cable to the Dasduino. The voltage of the easyC system is 3.3 VDC.
ARDUINO CODE
In order to make programming and using this module easier, we will use a prepared library. The library can be found here. If you do not know how to install the library, read our tutorial.
Here, we will show how to use the gesture sensor.
#include "Wire.h" //Adding Wire library
#include "SparkFun_APDS9960.h" //Adding sensor library
#define APDS9960_INT 2 //Defining Croduino's pin to which the INT pin of the sensor will be connected (has to be an Interrupt pin)
SparkFun_APDS9960 apds = SparkFun_APDS9960();
//A constructor for our library in order to have access to functions that control the sensor
int
isr_flag = 0;
//Variable which serves as a flag so to know that a gesture is detected
void
setup
() {
//Setting Croduino's pin as an input. To that pin, we connect the INT pin of the sensor
pinMode
(APDS9960_INT,
INPUT
);
Serial.begin(9600);
//Initialization of serial communication and adjusting the communication speed (9600 baud)
Serial.println();
//Printing the message to know that the connection between the computer and Croduino is ok.
Serial.println(F(
"**********Test APDS-99600 Senzora – Senzor geste**********"
));
//Setting the interruption routine. If it happens that on the Croduino's pin number 2
//a fall from logical one to logical zero is detected, the main program is interrupted and it "jumps" to the function "detekcijaGeste()"
attachInterrupt
(0, detekcijaGeste, FALLING);
//Initialization of the sensor library and checking if the sensor is connected to Croduino.
if
( apds.init() ) {
Serial.println(F(
"Initialization of the APDS-9960 sensor library is successful!"
));
}
else
{
Serial.println(F(
"Initialization of the library is unsuccessful. Sensor is not found. Check the connections!"
));
}
//If the initialization was successful, start the sensor
if
( apds.enableGestureSensor(
true
) ) {
Serial.println(F(
"Gesture sensor is started and activated. Make a gesture. :)"
));
}
else
{
Serial.println(F(
"Not possible to start the gesture sensor... :("
));
}
}
void
loop
() {
if
( isr_flag == 1 ) {
//If a gesture is detected, the isr_flag will be set to 1.
detachInterrupt
(0);
//Stop detecting gestures, in order to be able to read the current gesture.
gesta();
//Read the current gesture
isr_flag = 0;
//Return the flag to 0, which means that a gesture has not happened yet.
attachInterrupt
(0, detekcijaGeste, FALLING);
//Set the gesture detection again, now that Croduino has read which gesture it was about
}
}
void
detekcijaGeste() {
//If the INT pin has fallen from logical one to logical zero, it means that the sensor has read a gesture, so read it in the main program.
isr_flag = 1;
}
void
gesta() {
//Here we pick up the gesture from the sensor and print what gesture it was about.
if
( apds.isGestureAvailable() ) {
Serial.print(
"Detektirana gesta: "
);
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(
"Can not be determined."
);
}
}
}
Furthermore in the code, we will see how to use the sensor for reading levels of red, green and blue color (RGB).