Gesture Controlled Relay Switch using 3-axis Gyroscope
Gesture Recognition is the process by which gesture made by the user is conveyed as information to control devices. This project proposes a gesture-controlled relay switch that can be controlled using different gestures to control the home appliances TV, fan, right ..etc. This is a simple and easy way of controlling devices using an MPU6050 gyro sensor .The Arduino Uno is the brain behind all the computation , taking in all the gyroscope date and translates it into a signal to turn a relay on/off which in turn will turn on/off any appliance.
Requirements :
- Computer with an internet connection
- Download and install Arduino IDE
Components
1. MPU6050 Gyro sensor
2. Arduino UNO
3. Breadboard
4. 2 channel relay Module
5. Jumper wires
Hardware
Getting
Started with
Arduino UNO
Arduino Uno
Although there are many types of boards from arduino . Here we are going to be using the Arduino UNO as it is the most commonly used one. The arudino UNO has :
- The operating voltage is 5V
- The recommended input voltage will range from 7v to 12V
- Digital input/output pins are 14
- Analog i/p pins are 6
- DC Current for each input/output pin is 40 mA
- Flash Memory is 32 KB
- SRAM is 2 KB
- EEPROM is 1 KB
- CLK Speed is 16MHz
Another wonderful feature of the arduino is the option of using a add-on boards to the arduino which comes as a module and they are known as “Shields”
Pin out diagram
Arduino UNO
MPU6050 Gyro Sensor
The MPU-6050 is a sensor based on Micro Electrical Mechanical System (MEMS) technology. In this sensor module, the 3-axis accelerometer and the 3-axis gyroscope embedded in a single chip using I2C protocol to communicate, which helps to measure the acceleration, voltage, orientation, displacement, and many parameters of a system. An onboard Digital Motion Processor in this module is used to process the 6 axis fusion algorithm to provide a complete 9 axis motion fusion output by accessing the external magnetometer. MPU-6050 is a low cost and highly accurate sensor with an I2C bus.
This sensor has 8pins, the pins are follows :
VCC,GND,SCL,SDA,XDA,SCL,AD0,INT
2 channel Relay Module
The 2 channel relay module consists of two relays electrically isolated from the controlling inputs, used to control high voltage, high current loads such as motor, solenoid valves, lamps, and AC load. This module provides a standard interface controlled by the microcontroller. This operates directly with a 3.3 V or 5V logic signal from the microcontroller. It also comes with a LED to indicate the status of the relay.
CIRCUIT DIAGRAM
CONNECTING MPU6060 Sensor To ARDUINO
STEP1:
Wire up the data lines and power lines of the Ultrasonic sensor.
- MPU6050 Sensor VCC -> Arduino 5V
- MPU6050 Sensor GND -> Arduino GND
- MPU6050 Sensor SCL -> Arduino Analog Pin A5
- MPU6050 Sensor SDA -> Arduino Analog Pin A4
STEP2:
Wire up the Relay Module
- Relay VCC -> Arduino Vcc
- Reay GND -> Arduino GND
- Relay Signal1 -> Arduino Digital Pin 5
- Relay Signal2 -> Arduino Digital Pin 6
PROGRAMMING
Getting started with Arduino
Before starting any project with the arduino , you need to first let the arduino connect and communicate with your computer .This is done by using the software which is used for programming the arduino called the Arduino IDE
Installing The Arduino Software Package On Windows
Head over to the arduino.cc website or superkits.com to download arduino IDE and continue with the project
After installing the arduino IDE , we then need to use the printer usb cable which is provided with the arduino . We plug in one of the ends to the arduino and the other to the Desktop
If you used the Installer, as soon as you connect your board the Windows – from XP up to 10 – will install drivers automatically
If the board is not properly recognized when the zip package is downloaded and expanded, please follow the procedure below.
- START menu> CONTROL PANEL MENU
- From the control panel, check for System and Security
- Select system
- Select Device Manager from the System window
- Select open port named “Arduino UNO (COMxx)”, under Ports (COM & LPT). If there is no COM & LPT section, check “Other Devices” for “Unknown Device”.
- Choose the “Update Driver Software” option by right-clicking on the “Arduino UNO (COmxx)” port
- Navigate to the “Browse my computer for Driver software” option.
- Choose the driver file named “arduino. inf”, located in the “Drivers” folder of the Arduino Software download (not the “FTDI USB Drivers” sub-directory). If you are using an old version of the IDE (1.0.3 or older), Choose the Uno driver file named “Arduino UNO.inf”, If an old version of the IDE (1.0.3 or older) is used
- Thus the windows driver installation is completed
Select your board type : Arduino UNO and select the port
Choose Tools | Serial Port menu. This is likely to be COM3 or higher (COM1 and COM2 are usually reserved for hardware serial ports). To find out, you can disconnect your board and re-open the menu; the entry that disappears should be the Arduino board. Reconnect the board and select that serial port.
Press CNTRL + A & press DELETE to clear the Arduino IDE page
Upload the program
Copy the Sketch Code given below And try to understand
After that click on the “Upload” button. Then we can see the RX and TX LED on the board flashing. The message “Done uploading.” will appear if the uploading is a success
Install I2Cdev and Arduino-MPU6050-master as ZIP file in Arduino IDE
Download I2Cdev Library by clicking the button: Download I2Cdev Library
Download Arduino-MPU6050-master Library by clicking the button: Download Arduino-MPU6050-master
- In the Arduino IDE, choose Sketch, then goes to Include library and select Add. ZIP Library. Select the option to “Add. ZIP Library ’‘and then select the zip file you downloaded, from the top-down list.
- Then go back to Sketch > Include Library menu. From the library at the bottom of the drop-down menu, choose the library. In the Arduino sketches directory, the zip file will have been expanded in the libraries folder.
Arduino Code
Before we start, restart the Arduino IDE after installing the library.
#include <I2Cdev.h>
#include <MPU6050.h>
MPU6050 mpu;
// Timers
unsigned long timer = 0;
float timeStep = 0.01;
// Pitch, Roll and Yaw values
float pitch = 0;
float roll = 0;
const int RELAY_PIN_1 = 5;
const int RELAY_PIN_2 = 6;
void setup()
{
pinMode(RELAY_PIN_1, OUTPUT);
pinMode(RELAY_PIN_2, OUTPUT);
digitalWrite(RELAY_PIN_1, HIGH);
digitalWrite(RELAY_PIN_2, HIGH);
Serial.begin(115200);
// Initialize MPU6050
while (!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
{
Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
delay(500);
}
// Calibrate gyroscope. The calibration must be at rest.
// If you don't want calibrate, comment this line.
// mpu.calibrateGyro();
// Set threshold sensivty. Default 3.
// If you don't want use threshold, comment this line or set 0.
mpu.setThreshold(3);
}
void loop()
{
timer = millis();
// Read normalized values
Vector norm = mpu.readNormalizeGyro();
// Calculate Pitch and Roll
pitch = pitch + norm.YAxis * timeStep;
roll = roll + norm.XAxis * timeStep;
int flag_1 = 0;
int flag_2 = 0;
int flag_3 = 0;
int flag_4 = 0;
if (pitch > 40 ) {
digitalWrite(RELAY_PIN_1, LOW);
}
if(pitch<=-20){
digitalWrite(RELAY_PIN_1,HIGH);
}
if (roll > 40 ) {
digitalWrite(RELAY_PIN_2, LOW);
}
if(roll<=-20){
digitalWrite(RELAY_PIN_2,HIGH);
}
/*
if (pitch 45)
{
if (flag_3 == 0)
{
flag_3 = 1;
digitalWrite(RELAY_PIN_1, HIGH);
}
else {
flag_3 = 0;
digitalWrite(RELAY_PIN_1, LOW);
}
}
if (roll < -45)
{
if (flag_4 == 0)
{
flag_4 = 1;
digitalWrite(RELAY_PIN_1, HIGH);
}
else {
flag_4 = 0;
digitalWrite(RELAY_PIN_1, LOW);
}
}
*/
//print the pitch and roll
Serial.print(" Pitch = ");
Serial.print(pitch);
Serial.print(" Roll = ");
Serial.println(roll);
// Wait to full timeStep period
delay((timeStep * 1000) - (millis() - timer));
}
Then, click the Upload button in the Arduino IDE and wait a few seconds until you see the message “Done uploading.” in the bottom left corner.
Output
After connecting , it should look like this
- Gyroscope should be in a suitable position from where it’s going to take the calibration of the coordinates as 0,0,0 (x,y, z-axis), before it is turned ON, then move your hand to get the output on the relay
You should see the Pitch and Roll coordinates