edwardthe
Published © GPL3+

Mouse using Arduino & MPU 6050 Accelerometer Sensor

Today we're gonna make a very simple project & which is a Mouse. Yes, but with an Arduino.

BeginnerFull instructions provided1 hour3,777
Mouse using Arduino & MPU 6050 Accelerometer Sensor

Things used in this project

Story

Read more

Schematics

arduino mouse project circuit diagram using mpu6050

follow the circuit shown in the picture to get the project done proeprly

Code

Arduino Mouse project code using MPU6050

Arduino
This code will work only if you're using shown parts
#include <Wire.h>
#include <I2Cdev.h>
#include <MPU6050.h>
#include <Mouse.h>

MPU6050 mpu;
int16_t ax, ay, az, gx, gy, gz;
int vx, vy;

const int button1 = 4;
const int button2 = 5;
int responseDelay = 10;
void setup() {
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  Serial.begin(9600);
  Wire.begin();
  Mouse.begin();
  mpu.initialize();
  if (!mpu.testConnection()) {
    while (1);
  }
}

void loop() {
  mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

  vx = (gx - 400) / 200; // "-400" because the x axis of gyroscope give values about -350 while it's not moving. Change this value if you get something different using the TEST code, chacking if there are values far from zero.
  vy = -(gz - 200) / 200; // same here about "-200"
  int buttonState1 = digitalRead(button1);
  int buttonState2 = digitalRead(button2);

  if (buttonState1 == HIGH) {
    Mouse.press(MOUSE_LEFT);
    delay(100);
    Mouse.release(MOUSE_LEFT);
    delay(200);
  }
  else if (buttonState2 == HIGH) {
    Mouse.press(MOUSE_RIGHT);
    delay(100);
    Mouse.release(MOUSE_RIGHT);
    delay(200);
  }
  Mouse.move(vx, vy);

  delay(20);
}

Credits

edwardthe

edwardthe

3 projects • 1 follower
Thanks to Ashiq's Theory.

Comments