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 hour42
Mouse using Arduino & MPU 6050 Accelerometer Sensor

Things used in this project

Hardware components

SparkFun Triple Axis Accelerometer and Gyro Breakout - MPU-6050
SparkFun Triple Axis Accelerometer and Gyro Breakout - MPU-6050
×1
Arduino Leonardo With Headers
Arduino Leonardo With Headers
×1
Resistor 10k ohm
Resistor 10k ohm
×2
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1

Software apps and online services

Arduino IDE
Arduino IDE

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 • 0 followers
Thanks to Ashiq's Theory.

Comments

Add projectSign up / Login