Apds9960 gesture sensor on nodeMCU

Hi guys,
I’m trying to ise an apds9960 by sprkfun with an nodeMCU but I’m not sure how to connect it properly, the pins are diferent on the node, so where do I change to make it work?

Not a Blynk specific topic… However, it is an I2C based device, so look up your NodeMCU pinouts and wire it accordingly.

https://www.google.com/#q=nodemcu+pinout

https://www.google.com/#q=apds-9960+nodemcu

https://learn.sparkfun.com/tutorials/apds-9960-rgb-and-gesture-sensor-hookup-guide

I made the gesture ensor work!! but for some reason my node is constantly disconecting frm server? someone knows what might be?

Post your code (properly formatted), otherwise how will we be able to tell what the problem may be.

   
    /****************************************************************
    GestureTest.ino
    APDS-9960 RGB and Gesture Sensor
    Shawn Hymel @ SparkFun Electronics
    May 30, 2014
    https://github.com/sparkfun/APDS-9960_RGB_and_Gesture_Sensor

    Tests the gesture sensing abilities of the APDS-9960. Configures
    APDS-9960 over I2C and waits for gesture events. Calculates the
    direction of the swipe (up, down, left, right) and displays it
    on a serial console. 

    To perform a NEAR gesture, hold your hand
    far above the sensor and move it close to the sensor (within 2
    inches). Hold your hand there for at least 1 second and move it
    away.

    To perform a FAR gesture, hold your hand within 2 inches of the
    sensor for at least 1 second and then move it above (out of
    range) of the sensor.

    Hardware Connections:

    IMPORTANT: The APDS-9960 can only accept 3.3V!
     
    Wemos Pin  APDS-9960 Board  Function
     
     3.3V         VCC              Power
     GND          GND              Ground
     D3           SDA              I2C Data
     D1           SCL              I2C Clock
     D6           INT              Interrupt
     D7           -                LED

    Resources:
    Include Wire.h and SparkFun_APDS-9960.h

    Development environment specifics:
    Written in Arduino 1.0.5
    Tested with SparkFun Arduino Pro Mini 3.3V

    This code is beerware; if you see me (or any other SparkFun 
    employee) at the local, and you've found our code helpful, please
    buy us a round!

    Distributed as-is; no warranty is given.

    Modified for ESP8266 by Jon Ulmer Nov 2016
    ****************************************************************/

    #include <Wire.h>
    #include <SparkFun_APDS9960.h>
    #define BLYNK_PRINT Serial
    #include <IRremoteESP8266.h>
    #include <ESP8266WiFi.h>
    #include <BlynkSimpleEsp8266.h>

    // Pins on wemos D1 mini
    #define APDS9960_INT    D6  //AKA GPIO12 -- Interupt pin
    #define APDS9960_SDA    D3  //AKA GPIO0
    #define APDS9960_SCL    D1  //AKA GPIO5
    // Constants

    // Global Variables
    SparkFun_APDS9960 apds = SparkFun_APDS9960();
    volatile bool isr_flag = 0;

    // Your WiFi credentials.
    // Set password to "" for open networks.
    char ssid[] = "Jeo";
    char pass[] = "239239";

    // You should get Auth Token in the Blynk App.
    // Go to the Project Settings (nut icon).
    char auth[] = "66dfbbdca562ff8";

    // Bridge widget on virtual pin 1
    WidgetBridge bridge1(V1);

    // Timer for blynking
    BlynkTimer timer;

    // Place the AuthToken of the second hardware here
    BLYNK_CONNECTED() {
    bridge1.setAuthToken("4668802bd32066b92dd3");
    }

    void setup() {

      //Start I2C with pins defined above
      Wire.begin(APDS9960_SDA,APDS9960_SCL);

      // Set interrupt pin as input
      pinMode(APDS9960_INT, INPUT);

      Blynk.begin(auth, ssid, pass);

      // Initialize Serial port
      Serial.begin(115200);
      
      // Initialize interrupt service routine
      attachInterrupt(APDS9960_INT, 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 loop() {
      if( isr_flag == 1 ) {
        detachInterrupt(APDS9960_INT);
        handleGesture();
        isr_flag = 0;
        attachInterrupt(APDS9960_INT, interruptRoutine, FALLING);

      Blynk.run();
      timer.run();
      }
    }

    void interruptRoutine() {
      isr_flag = 1;
    }

    void handleGesture() {
        if ( apds.isGestureAvailable() ) {
        switch ( apds.readGesture() ) {
          case DIR_UP:
            bridge1.digitalWrite(4, HIGH); 
            break;
          case DIR_DOWN:
            bridge1.digitalWrite(4, LOW);
            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:
          bridge1.digitalWrite(4, LOW);
          bridge1.digitalWrite(5, LOW);
          bridge1.virtualWrite(3, 1);
          delay(50);
          bridge1.virtualWrite(3, 0);
            break;
          default:
            Serial.println("NONE");
        }
      }
    }

I fixed the code formatting as per requirements for proper display

Without digging into your code, I would guess that the interrupt routine you have is still too-much-too-fast.

I would move it into a timer loop of it’s own, even something checking every 250ms is better than blocking or flooding the void loop()

how can I do it?

I suspect it is that other code within your loop(). Do Note, I do not have any experience with the sensor you are using, so this may or may not work. I have also heard that interrupts can be troublesome on ESP’s, although I do not really use them so this may be incorrect as well. RESEARCH.

Question, why the isr_flag? Could you not just put this code in the interrupt routine, and remove it from loop()? Like below:

void loop() {
      Blynk.run();
      timer.run();
      }
    

    void interruptRoutine() {
       detachInterrupt(APDS9960_INT);
        handleGesture();
        attachInterrupt(APDS9960_INT, interruptRoutine, FALLING);
    }

Then it would still run when the interrupt is triggered, but keep it from slowing down the BLYNK process.

You could maybe even take it a step further and put the code from the handleGesture() in the interrupt routine as well.


 void interruptRoutine() {
       detachInterrupt(APDS9960_INT);
        if ( apds.isGestureAvailable() ) {
        switch ( apds.readGesture() ) {
          case DIR_UP:
            bridge1.digitalWrite(4, HIGH); 
            break;
          case DIR_DOWN:
            bridge1.digitalWrite(4, LOW);
            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:
          bridge1.digitalWrite(4, LOW);
          bridge1.digitalWrite(5, LOW);
          bridge1.virtualWrite(3, 1);
          //delay(50); //delays don't work in ISR
          bridge1.virtualWrite(3, 0);
            break;
          default:
            Serial.println("NONE");
        }
      }
attachInterrupt(APDS9960_INT, interruptRoutine, FALLING);
}

Although, ISR’s should be as short and as fast as possible. It’s worth a try though.

I saw that you have BlynkTimer timer; setup already, and timer.run() in the void loop()… so figured you already knew how to use it.

Try moveing your interrupt check routine into a void MyLoop() of it’s own and add a timer.setInterval(250L, MyLoop);

http://docs.blynk.cc/#blynk-firmware-blynktimer

As I said, I honestly haven’t dug into your code, so there may be much more to your issue as @Toro_Blanco suspects.

http://gammon.com.au/interrupts