Project in progress
Head Mouse With MPU6050 and Arduino Micro

Head Mouse With MPU6050 and Arduino Micro © GPL3+

This project consists on a mouse controlled with the movement of the head, using a gyroscope and an Arduino Micro.

  • 18,925 views
  • 8 comments
  • 39 respects

Components and supplies

Necessary tools and machines

09507 01
Soldering iron (generic)
Mini drill (generic)

Apps and online services

About this project

Hi everyone, here is my new project, a mouse controlled with the movement of the head! I decided to make this project once I received my accelerometer/gyroscope MPU6050 from IC Station, and I wanted to do something useful with this.

Then I found out that a "Head Mouse" already exists, made by Martin Millmore with another accelerometer/gyroscope.

Well here is my version, made with an Arduino Micro and a MPU6050.

Thanks to IC Station http://www.icstation.com/ for the accelerometer/gyroscope they sent me, allowing me to create this project!

Step 1: Materials and tools needed

Materials needed:

  • Arduino Micro
  • accelerometer/gyroscope MPU6050
  • a Compeed box (I found this comfortable as Martin Millmore said in his guide)
  • an elastic band
  • micro USB cable to USB type B (like those used for Android smartphones)
  • cables
  • a PCB board
  • glue or better, Sugru

Tools needed:

  • a computer with the Arduino IDE installed
  • soldering station
  • mini drill

Step 2: Building

  • Solder on the pcb board the pins like in the image: pin 2 of Arduino to pin SDA, pin 3 to SCL, pin 5V to PWR, pin GND to GND.
  • Make a hole with the Dremel on the Compeed box so the micro USB cable can reach the Arduino Micro, and two straight holes for the elastic band.
  • Glue the board to the box and add the elastic band.

Step 3: Code

Connect the board to your computer and load first the "Test" code to the Arduino to test if everything work fine. You'll probably need to change some values (everything is written in the code's comments) since I had to add a constant, because values from the gyroscope are not "zero" when I'm not moving.

Use the Serial Monitor to check if you need to fix the "zero" of the gyroscope. Once you found the right values you can load the program deleting the "Serial.print " part, that is equivalent to the standard code.

If you want the click function after a delay of time since the pointer has been in a 10x10 pixel zone load the code "with_click " to Arduino Micro.

Here there are the codes:

To use them you'll need the MPU6050 and I2C libraries:

In this site I found the libraries and the code from which I developed the programs of my project. Link to Martin Millmore's project with the LSM9DS0 instead of the MPU6050.

Code

Standard code (only movement)Arduino
Code used only to move the pointer
/* Code to control the mouse pointer
 * through the movement of the head
 * Change values at vx and vy (+300 and -100 in my case)
 * using the TEST code to make your project work.
 * 
 * Gabry295
 */

#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;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  mpu.initialize();
  if (!mpu.testConnection()) {
    while (1);
    }
}

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

  vx = (gx+300)/200;  // "+300" 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-100)/200; // same here about "-100"
  
  Mouse.move(vx, vy);
  
  delay(20);
}
Test codeArduino
Code to check values from the gyroscope in order to calibrate the movement of the pointer
/* Code to view on the Serial Monitor the values of the Gyroscope
 * and the output for the mouse. This is equal to the standard code
 * but with instructions to check the values on the Serial monitor.
 * Change the delay to read the values at reduced speed.
 * 
 * Gabry295
 */

#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;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  mpu.initialize();
  if (!mpu.testConnection()) { while (1); }
}

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

  vx = (gx+300)/150;  // "+300" 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-100)/150; // same here about "-100"

  Serial.print("gx = ");
  Serial.print(gx);
  Serial.print(" | gz = ");
  Serial.print(gz);
  
  Serial.print("        | X = ");
  Serial.print(vx);
  Serial.print(" | Y = ");
  Serial.println(vy);
  
  Mouse.move(vx, vy);
  
  delay(20);
}
Movement and auto-clickArduino
Code that includes both movement of the pointer and clik after 2 seconds the pointer stand on a restricted area
/* Code to control the mouse pointer with the movement of the head
 * and assisted click.
 * Change the values at vx and vy (+300 and -100 in my case) 
 * setting them with the TEST code.
 * 
 * Gabry295
*/

#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, vx_prec, vy_prec;
int count=0;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  mpu.initialize();
  if (!mpu.testConnection()) {
    while (1);
    }
}

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

  vx = (gx+300)/200;  // "+300" 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-100)/200; // same here about "-100"
  
  Mouse.move(vx, vy);
  
  
  if ( (vx_prec-5)<=vx && vx<=vx_prec+5 && (vy_prec-5)<=vy && vy<=vy_prec+5) { // checking the pointer doesn't move too much from its actual position: (in this case a 10 pixel square)
    count++;                                                                  
    if(count == 100){ // the click will happen after 2 seconds the pointer has stopped in the 10px square: 20ms of delay 100 times it's 2000ms = 2s
      if (!Mouse.isPressed(MOUSE_LEFT)) {
        Mouse.press(MOUSE_LEFT);
        count = 0;
      }
    }
    else {
      if (Mouse.isPressed(MOUSE_LEFT)) {
        Mouse.release(MOUSE_LEFT);
      }
    }
  }
  else {
    vx_prec = vx; // updating values to check the position of the pointer and allow the click
    vy_prec = vy;
    count = 0;
    }
  
  delay(20);
}

Comments

Similar projects you might like

Human Head to Robot Head

Project tutorial by jegatheesan

  • 3,382 views
  • 5 comments
  • 8 respects

Head Tracking for Wireless 3D First Person Vision

Project showcase by twhi2525

  • 8,552 views
  • 7 comments
  • 54 respects

Keyboard and Mouse Controller

Project showcase by pdio

  • 2,622 views
  • 0 comments
  • 3 respects

Micro Soldering Station

Project tutorial by Paulo Bruckmann

  • 14,378 views
  • 13 comments
  • 44 respects

Digital 3D Printer Filament Counter Using PS/2 Mouse

Project in progress by ArtSuzhou

  • 8,445 views
  • 11 comments
  • 32 respects
Add projectSign up / Login