RGB and Gesture Sensor – APDS-9960 Arduino Interfacing

ABOUT THE PROJECT:

complete

Our sensor, the Avago APDS-9960, offers ambient light and color (as clear, red, green, and blue) measuring, proximity detection, and gesture sensing.

We need an Arduino that can connect to a network. It can be either an Arduino with an Ethernet Shield or as we are using, an Arduino Ethernet.

Level shifting: if you are using a 5V Arduino, you’ll need to use a level shifter to convert the 5V Signal to 3.3V that the sensor uses.

The APDS-9960 breakout board provides six pins to provide power to the sensor and I2C bus. We’ll only use five of those pins.

Arduino pin Gesture sensor pin
D2 INT
3.3V VCC
GND GND
A4 SDA
A5 SCL

When using a level converter, pass the data signals SDA,SCL and INT through pins A1, A2 and A3 of the level converter. This converts 3.3V on the sensor side to 5V that the Arduino needs and vice versa. Connect the HV pin of the level converter to the 5V pin of the Arduino and the LV pin to the 3.3V pin of the Arduino

SparkFun RGB and Gesture Sensor - APDS-9960
ABOUT THE SENSOR

Description: This is the SparkFun RGB and Gesture Sensor, a small breakout board with a built in APDS-9960 sensor that offers ambient light and color measuring, proximity detection, and touchless gesture sensing. With this RGB and Gesture Sensor, you will be able to control a computer, microcontroller, robot, and more with a simple swipe of your hand! This is, in fact, the same sensor that the Samsung Galaxy S5 uses and is probably one of the best gesture sensors on the market for the price.

The APDS-9960 is a serious little piece of hardware with built-in UV and IR blocking filters, four separate diodes sensitive to different directions, and an I2C compatible interface. For your convenience, we have broken out the following pins: VL (optional power to IR LED), GND (Ground), VCC (power to an APDS-9960 sensor), SDA (I2C data), SCL (I2C clock), and INT (interrupt). Each APDS-9960 also has a detection range of 4 to 8 inches (10 to 20 cm).

Features:

  • Operational Voltage: 3.3V
  • Ambient Light & RGB Color Sensing
  • Proximity Sensing
  • Gesture Detection
  • Operating Range: 4-8in (10-20cm)
  • I2C Interface (I2C Address: 0x39)

SparkFun RGB and Gesture Sensor - APDS-9960

Documents:

imageedit_1_7493397999

file-pdf-icon

DATASHEET OF

RGB and Gesture Sensor – APDS-9960 DataSheet

(clickable image)

PIN CONFIGURATION

SparkFun RGB and Gesture Sensor - APDS-9960

SOFTWARE REQUIRED

ARDUINO IDE (for windows)

SCHEMATICS

schematics-and-layouts-icon

 

RGB and Gesture Sensor – APDS-9960

(CLICKABLE IMAGE)

 

 

 

ARDUINO SOURCE CODE

arduino-icon-2

  •     #include <SPI.h>
        #include <Wire.h>
        #include <Ethernet.h>
        #include <SparkFun_APDS9960.h>
    
        // Enabe debug tracing to Serial port.
        #define DEBUG true
        #define STATICIP false
    
        // Pins
        #define APDS9960_INT    2 // Needs to be an interrupt pin
    
        // Here we define a maximum framelength to 64 bytes. Default is 256.
        #define MAX_FRAME_LENGTH 64
    
        #include <WebSocket.h>
    
        byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x91, 0x8C };
    
        #if STATICIP
        byte ip[] = {10,0,1,100};
        #endif
    
        // Create a Websocket server
        WebSocketServer wsServer;
    
        void onConnect(WebSocket &socket) {
        Serial.println("onConnect called");
        }
    
        // You must have at least one function with the following signature.
        // It will be called by the server when a data frame is received.
        void onData(WebSocket &socket, char* dataString, byte frameLength) {
    
        #ifdef DEBUG
        Serial.print("Got data: ");
        Serial.write((unsigned char*)dataString, frameLength);
        Serial.println();
        #endif
    
        // Just echo back data for fun.
        socket.send(dataString, strlen(dataString));
        }
    
        void onDisconnect(WebSocket &socket) {
        Serial.println("onDisconnect called");
        }
    
    
        // Global Variables
        SparkFun_APDS9960 apds = SparkFun_APDS9960();
        int isr_flag = 0;
    
    
    
        void setup() {
        #ifdef DEBUG  
        Serial.begin(9600);
        #endif
    
        #if STATICIP
        Ethernet.begin(mac, ip);
        #else
        if (Ethernet.begin(mac) == 0) {
        #if DEBUG
        Serial.println("Unable to set server IP address using DHCP");
        #endif
        for(;;)
        ;
        }
        #if DEBUG
        // report the dhcp IP address:
        Serial.println(Ethernet.localIP());
        #endif
        #endif
    
        wsServer.registerConnectCallback(&onConnect);
        wsServer.registerDataCallback(&onData);
        wsServer.registerDisconnectCallback(&onDisconnect);
        wsServer.begin();
    
        delay(100); // Give Ethernet time to get ready
    
    
        // Set interrupt pin as input
        pinMode(APDS9960_INT, INPUT);
    
        // Initialize interrupt service routine
        attachInterrupt(0, interruptRoutine, FALLING);
    
        // Initialize APDS-9960 (configure I2C and initial values)
        if ( apds.init() ) {
        Serial.println(F("APDS-9960 initialization complete"));
        } else {
        Serial.println(F("Something went wrong during APDS-9960 init!"));
        }
    
        // Start running the APDS-9960 gesture sensor engine
        if ( apds.enableGestureSensor(true) ) {
        Serial.println(F("Gesture sensor is now running"));
        } else {
        Serial.println(F("Something went wrong during gesture sensor init!"));
        }
    
        }
    
        void interruptRoutine() {
        isr_flag = 1;
        }
    
        String handleGesture() {
        String gesture;
        gesture="NONE  ";
    
        if ( apds.isGestureAvailable() ) {
        switch ( apds.readGesture() ) {
        case DIR_UP:
        Serial.println("UP   ");
        gesture="UP";
        break;
        case DIR_DOWN:
        Serial.println("DOWN");
        gesture="DOWN ";
        break;
        case DIR_LEFT:
        Serial.println("LEFT");
        gesture="LEFT ";
        break;
        case DIR_RIGHT:
        Serial.println("RIGHT");
        gesture="RIGHT";
        break;
        case DIR_NEAR:
        Serial.println("NEAR");
        gesture="NEAR  ";
        break;
        case DIR_FAR:
        Serial.println("FAR");
        gesture="FAR  ";
        break;
        default:
        Serial.println("NONE");
        gesture="NONE ";
        }
        }
    
        return gesture;
    
        }
    
        String rv;
        char gest[6];
    
        void loop() {
        // Should be called for each loop.
        wsServer.listen();
    
        // Do other stuff here, but don't hang or cause long delays.
          delay(100);
    
          if( isr_flag == 1 ) {
            detachInterrupt(0);
            rv=handleGesture();
            isr_flag = 0;
            attachInterrupt(0, interruptRoutine, FALLING);
    
          if (rv!="NONE ") {
    
            if (wsServer.connectionCount() > 0) {
              rv.toCharArray(gest, 6);
              wsServer.send(gest, 6);
            }
    
          } 
    
          }
    
        }

Transfer the sketch to your Arduino and open the serial console and note the printed IP address :

192.168.1.194
    APDS-9960 initialization complete

WebSocket client TECEXion

Now, we are going to implement the TECEXion that connects to the Arduino via WebSocket that gets notified when a gesture is recognized. Create a new tecexion project with Grace:

grace new

Use the following parameters when asked by grace:

name: Gestures
type: tecexion
skeleton: default

Now, change into the new project directory:

cd Gestures

Edit the file src/index.html to add a text area element, replacing the Hello World that was there already.

        <textarea id="msg" data-type="dizmo-textarea"></textarea>

Now, edit the file src/application.js and add the following code inside the tecexready event handler function:

window.document.addEventListener('tecexready', function() {
    ws = new WebSocket("ws://<insert ip address from above>:80/");

    ws.onmessage = function(evt) {
    window.console.log(evt.data);
    document.getElementById("msg").innerHTML=evt.data;
    };
    ws.onerror = function(evt) {
    window.console.log(evt.data);
    document.getElementById("msg").innerHTML="ERROR: "+evt.$
    };
    ws.onclose = function() {
    window.console.log("onclose called");
    });
    ws.onopen = function() {
    window.console.log("onopen called");
    ws.send("Hello, Arduino");
    };
    });

Build and zip the tecexion with Grace

python manage.py zip

Drag the resulting file from the build folder into dizmoSpace.

Now, hold your finger a few centimeters above the sensor and move it left or right … you should see the sensed gesture in the tecexion.

gestures

We are excited to see what you can do with this basic set of gesture support for your tecexions 😛 . Link  us your ideas and implementations on Linkedin or mail us mailt0:tecex , using the hashtag #dizmonks!

HAPPY INTERFACING:)

SHUBHAM GUPTA-TECEX

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s