memoryleak
Published © MIT

Use Gestures to Control YouTube Playback with Arduino

Wave your hand to fast forward, pause or go to next video. All you need are an Arduino-compatible microcontroller board and an IR sensor.

BeginnerFull instructions provided1 hour572

Things used in this project

Hardware components

Seeeduino V4.2
Seeeduino V4.2
×1
Seeed Grove - Human Presence Sensor
×1

Software apps and online services

Arduino IDE
Arduino IDE
VS Code
Microsoft VS Code

Story

Read more

Schematics

Hardware connection

Connect the seeeduino board, IR sensor, and your computer

Code

Arduino signal recognition code

Arduino
Copy and paste it into your Arduino IDE to capture hand motions.
#include <Wire.h>
#include <Math.h>
#include "Grove_Human_Presence_Sensor.h"

AK9753 movementSensor;

float ir2, ir4;       //variables to store your sensor data
float diff24;         //variable to store the difference between the left and right sensor
float tempDiff = 0.0; //variable to store the current diff24. 
                      //This variable is used to test whether the diff24 is going up or going down.
float temp;
float mov;            //variable to store the difference of 2 diff24

void setup()  //set up movement sensor
{
    Serial.begin(9600);

    Wire.begin();

    //Turn on sensor
    if (movementSensor.initialize() == false)
    {
        Serial.println("Device not found. Check wiring.");
        while (1)
            ;
    }
}

void loop()
{

    if (movementSensor.dataReady())
    {
        ir2 = movementSensor.getIR2(); 
        ir4 = movementSensor.getIR4();       //get sensor data
        temp = movementSensor.getTMP();

        movementSensor.startNextSample();

        diff24 = ir2 - ir4;
        float mov = diff24 - tempDiff; 
        if (abs(mov) > 60)    // Determine whether there is a movement
        {
            if (mov > 60)    //Determine the movement direction
            {
                Serial.println("right");
            }
            if (mov < -60)
            {
                Serial.println("left");
            }
            delay(500);      //We don't want to trigger multiple signals during one hand waving.So once the detection was triggered we will delay the sensor reading data for 0.5 second.
            if (movementSensor.dataReady())   //As the sensor is delayed, so the diff24 value is still the one we calculated 0.5 seconds before.So we use the following code to refresh the diff24 data.
            {
                ir2 = movementSensor.getIR2();
                ir4 = movementSensor.getIR4();
                temp = movementSensor.getTMP();

                movementSensor.startNextSample();
                diff24 = ir2 - ir4;
            }
            tempDiff = diff24;
        }
        delay(10);
    }
}

Python code to control your keyboard

Python
Control your keyboard based on the signal captured from Arduino
import serial
import time
from pynput.keyboard import Key, Controller

keyboard = Controller()

arduino = serial.Serial('/dev/cu.usbmodem14101', 9600)
time.sleep = 1  #pause 1 seconds for python to connect with arduino

while 1:
    incoming = str(
        arduino.readline())  #read the serial data and print it as line
    print(incoming)

    if 'right' in incoming:
        for i in range(4):
            keyboard.press(Key.right)    #the press function will keep pressing the key for a short while
            keyboard.release(Key.right)  #So release the key once its pressed

    if 'left' in incoming:
        for i in range(4):
            keyboard.press(Key.left)
            keyboard.release(Key.left)
    incoming = " "  #refresh the incoming data

Credits

memoryleak

memoryleak

5 projects • 5 followers
A soft and hardware engineer motivated by interest and fun

Comments