Project tutorial
Arduino Motion Controlled Electronics!

Arduino Motion Controlled Electronics! © GPL3+

A simple device that allows users to wave their hand and control Any device they want.

  • 4,733 views
  • 0 comments
  • 15 respects

Components and supplies

Sparkfun APDS-9960
×1
11026 02
Jumper wires (generic)
×1
A000066 iso both
Arduino UNO & Genuino UNO
You CAN'T use the Mega 2560!
×1
Relay (generic)
×1
Mfr 25fbf52 4k75 sml
Resistor 4.75k ohm
×1

Apps and online services

About this project

To make this project, first connect the APDS-9960 according to the schematic. Next, get a relay module and connect it to the Arduino. Note: You can't use the Arduino Mega 2560 for this project. Link the I2C lines to the resistors and Arduino. You will now need to copy and paste in the code to the Arduino IDE. You will need to install the library. Head on over to the IDE and select: sketch -> include libraries -> manage libraries -> then search "apds". Download and install it. Connect your appliance to the relay module, if unsure check out my other project where I discuss how: https://www.hackster.io/gatoninja236/arduino-humidifier-control-2e7805 Plug in the appliance lines first and then connect the Arduino to a power source. You now have a working motion-controlled appliance!

Code

Arduino CodeC/C++
Copy-paste. Based from the Sparkfun APDS-9960 example code.
/****************************************************************
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!
 
 Arduino Pin  APDS-9960 Board  Function
 
 3.3V         VCC              Power
 GND          GND              Ground
 A4           SDA              I2C Data
 A5           SCL              I2C Clock
 2            INT              Interrupt

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.
****************************************************************/

#include <Wire.h>
#include <SparkFun_APDS9960.h>

// Pins
#define APDS9960_INT    2 // Needs to be an interrupt pin

// Constants

// Global Variables
SparkFun_APDS9960 apds = SparkFun_APDS9960();
int isr_flag = 0;
int relayPin = 3;

void setup() {

  // Set interrupt pin as input
  pinMode(APDS9960_INT, INPUT);
  pinMode(relayPin, 3);
  digitalWrite(relayPin, HIGH);

  // Initialize Serial port
  Serial.begin(9600);
  Serial.println();
  Serial.println(F("--------------------------------"));
  Serial.println(F("SparkFun APDS-9960 - GestureTest"));
  Serial.println(F("--------------------------------"));
  
  // 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 loop() {
  if( isr_flag == 1 ) {
    detachInterrupt(0);
    handleGesture();
    isr_flag = 0;
    attachInterrupt(0, interruptRoutine, FALLING);
  }
}

void interruptRoutine() {
  isr_flag = 1;
}

void handleGesture() {
    if ( apds.isGestureAvailable() ) {
    switch ( apds.readGesture() ) {
      case DIR_UP:
        Serial.println("UP");
        break;
      case DIR_DOWN:
        Serial.println("DOWN");
        break;
      case DIR_LEFT:
        Serial.println("LEFT");
        digitalWrite(relayPin, HIGH);
        break;
      case DIR_RIGHT:
        Serial.println("RIGHT");
        digitalWrite(relayPin, LOW);
        break;
      case DIR_NEAR:
        Serial.println("NEAR");
        break;
      case DIR_FAR:
        Serial.println("FAR");
        break;
      default:
        Serial.println("NONE");
    }
  }
}

Schematics

Schematic
Attach as follows:
Schematic bb

Comments

Similar projects you might like

Motion Controlled Color Changer!

Project tutorial by Arduino “having11” Guy

  • 9,441 views
  • 17 comments
  • 14 respects

Motion Activated Night Light

Project tutorial by Haziq Afandi

  • 3,113 views
  • 0 comments
  • 4 respects

Gesture / Motion Controlled Automatic Appliances

Project tutorial by

  • 1,740 views
  • 0 comments
  • 12 respects

Remote Controlled Bulb With Arduino

Project tutorial by muhammed shameel k.v

  • 2,859 views
  • 0 comments
  • 12 respects

Arduino Simple "Camera Slider" Electronics

Project tutorial by Ian Cumming

  • 5,758 views
  • 3 comments
  • 15 respects

Complete Digital Clock Including Alarm and Motion Sensor

Project tutorial by LAGSILVA

  • 12,678 views
  • 11 comments
  • 50 respects
Add projectSign up / Login