Accelerometer based Wireless 3D Air Mouse With Arduino (Hindi & English Video).

By Ashwini Kumar Sinha

18051
Advertisement
Change Video Language

We have seen different types of Human Interface Devices (HIDs), for example mouse and keyboard. In older versions of computer mouse, optical sensors were used to detect movement relative to a surface, thus they require smooth surface to function properly. These mice can work with only two coordinates. With advancements in technology, we are now talking about 3D user interfaces ((3DUIs). So, we must start developing a 3D HID device which have the capability to work with all coordinates.

In this project we are going to make 3D wireless HID device that will give you a whole new experience of using computers and playing online games. With this device, you will be able to control computers and smartphones by just moving your hand in air.

This device can be very helpful in making 3D UI based systems. We can use this device in 3D designing as well. For instance, you can draw any design by moving your hand in the air. Further we can develop such device for 3D holographic display and in VR that gives us a 3D UI.

Best application of this device is that it will enable differently abled people to use computers and smartphones. People who don’t have hands to operate computer or phones can now do so with the help of this device. They can wear this device on their head and operate gadgets by moving their head.

Now, let’s start our Interesting project. First, we need to gather the following components –

Components:3d Mouse

  • Arduino Pro Mini 5V 16-Mhz
  • 2 Bluetooth HC 05
  • Mpu 6050 Sensor
  • Arduino Leonardo Micro (At mega 32u)
  • Jumper wires
  • Micro USB Cable
  • 4 Push Switch

Bill of material

You also need a either FTDI Module or Arduino Uno to upload code on pro mini.

Coding

First of all, we need to install an MPU 6050 library. Open library manager of Arduino IDE and search for MPU6050, then install the library – refer (Fig. 1). After this, we will set one Bluetooth as slave and another as master – follow the Bluetooth instruction on the internet to do this.

Fig. 1 Installing Library

As we have installed the library, now we can start the coding part. In the first part of coding, we will include the library for MPU6050 and set the variables for storing values of mpu6050 sensor. After that we can set up sensor and Bluetooth in set up function – for this refer (Fig. 2). I have used Baudrate 9600 in this code.

Fig .2 Arduino Mini code setting library

 

In the second part of coding, we will send the buttons and sensors data to another Arduino over bluetooth as in pic, refer (Fig 3).

Fig. 3 Arduino Mini code getting MPU6050 data

Our Arduino mini code is ready now. So, let’s start coding for Arduino pro micro.

First, we will create variables to store values, then set up Bluetooth to read serial data of sensor – refer (Fig. 4).

Fig . 4 Arduino Micro code setting library

In the third part of our coding process, we will create a loop function and get the serial data from Bluetooth. Here in this code, I have used software serial for Bluetooth connection with baudrate of 9600.

After this, check the range value of serial data and assign its value for mouse movement as in Fig. 5.

Fig. 5 Checking the data to set coordinate values

Next, we will create if () condition to check how the mouse button functions – you can refer (Fig. 6) for this. We can also add extra Keyboard shortcuts and other functions to it as per your desire. Here in this code, I have added button shortcut for copy and paste function. You can create your own custom function according to your use cases.

Fig . 6 Setting mouse click function

As we have completed the coding part, we need to connect the components.

Connections:

Arduino Mini Pin
Components Pin
Arduino Mini SCL MPU 6050 SCl
Arduino Mini SDA MPU 6050 SDA
Arduino Mini VCC MPU 6050 VCC
Arduino Mini GND MPU 6050 GND
Arduino Mini TX
Bluetooth HC 05 RX
Arduino Mini RX
Bluetooth HC 05 TX
Arduino Mini VCC
Bluetooth HC 05 +Ve
Arduino Mini VCC
Push Switch Pins
Arduino Mini (A3,A0,A2,A1 )
Push switch(1, 2,3,4)

 

Refer Fig. 7 for connection and schematic.

Fig .7 Connection with Arduino Pro Mini

Now, we will connect Arduino Pro Micro with Bluetooth – refer (Fig . 8)

Arduino
Bluetooth HC 05
VCC +VE
GND -VE
Pin 9 TX
Pin 8 RX
Fig .8 Arduino Pro Micro connection.
Fig 9

Now to upload the code to Pro Mico select the board and port and and press ctrl+ U(Refer fig 9).Now upload the code to Pro mini using the board option (Refer Fig 10). Follow the instructions online to upload the code on Pro Mini. Search How to upload code on pro mini.

 

Fig 10

Congrats! The wireless HID mouse is ready for use. You can do some awesome tricks with it –  you can attach it on your hand or any toy gun and enjoy the real shooting game experience. Attach the device on the tip of a plastic knife and you can play vegetable cutting game with it.

Note:- In the code, I have mentioned A7 pin in Arduino for a keyboard button function but not used it in connection and schematics because some Arduino mini version don’t have this pin. If you have extra A7, A8 pins you can add more switch to it and set keyboard shortcut function according to your choice.

Downlode code

 

Advertisement


17 COMMENTS

    • Open Arduino IDE then
      go to sketch–>include library–>manage libraries
      search for MPU 6050 in the search box and install all the three shown there.
      Hope this helps you..!!!

    • Note Raw pin of Aruino Pro mini is capable of regulating unregulated power supply upto 9v or 12v so you can use any battery having 5v to 9V powerbut must go through the raw pin not any other VCC pin

  1. I connected everything perfectly as shown, the circuit powers up and the lights are blinking but the cursor doesn’t move any help appreciated.

  2. i uploaded the code as above mentioned but its does not compile and showing a error of mouse Arduino: 1.8.9 (Windows Store 1.8.21.0) (Windows 10), Board: “Arduino/Genuino Uno”

    In file included from C:\Users\DELL\AppData\Local\Temp\Rar$DIa3592.43265\Final_HID\Final_HID.ino:2:0:

    C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\Mouse\src/Mouse.h:29:2: warning: #warning “Using legacy HID core (non pluggable)” [-Wcpp]

    #warning “Using legacy HID core (non pluggable)”

    ^

    In file included from C:\Users\DELL\AppData\Local\Temp\Rar$DIa3592.43265\Final_HID\Final_HID.ino:3:0:

    C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\Keyboard\src/Keyboard.h:29:2: warning: #warning “Using legacy HID core (non pluggable)” [-Wcpp]

    #warning “Using legacy HID core (non pluggable)”

    ^

    C:\Users\DELL\AppData\Local\Temp\Rar$DIa3592.43265\Final_HID\Final_HID.ino: In function ‘void setup()’:

    Final_HID:15:3: error: ‘Mouse’ not found. Does your sketch include the line ‘#include ‘?
    Mouse .begin(48);

    ^

    Final_HID:16:3: error: ‘Keyboard’ not found. Does your sketch include the line ‘#include ‘?
    Keyboard.begin();

    ^

    C:\Users\DELL\AppData\Local\Temp\Rar$DIa3592.43265\Final_HID\Final_HID.ino: At global scope:

    Final_HID:19:10: error: variable or field ‘loop’ declared void

    void loop({

    ^

    Final_HID:21:8: error: expected primary-expression before ‘if’

    if (mySerial.available() > 2) {

    ^

    Final_HID:21:8: error: expected ‘}’ before ‘if’

    Final_HID:21:8: error: expected ‘)’ before ‘if’

    Final_HID:50:5: error: expected unqualified-id before ‘if’

    if (serialA== 80000){

    ^

    Final_HID:60:5: error: expected unqualified-id before ‘if’

    if (serialA== 90000){

    ^

    Final_HID:69:6: error: expected unqualified-id before ‘if’

    if (serialA== 70000){

    ^

    Final_HID:77:6: error: expected unqualified-id before ‘if’

    if (serialA== 60500){

    ^

    Final_HID:86:6: error: expected unqualified-id before ‘if’

    if (serialA== 60000){

    ^

    Final_HID:93:6: error: expected unqualified-id before ‘else’

    }else{

    ^

    Final_HID:100:1: error: expected declaration before ‘}’ token

    }

    ^

    exit status 1
    ‘Mouse’ not found. Does your sketch include the line ‘#include ‘?

    This report would have more information with
    “Show verbose output during compilation”
    option enabled in File -> Preferences.

    kindly give instruction through which i can upload it succesfully

    • Hello, I hope you are doing well, I know your problem answer and that is you can’t compile and upload that code which is for “micro/Leonardo/mini/mega” to “UNO/NANO,etc..” Arduinos. Briefly you can upload this code just on Arduino which has a (for example: Atmega32U4). Go to IDE and change your board.

      ***I have some problem with that first code for mini, when I compile it I saw this error, do you have as same as my problem?

      #include
      #include
      #include

      MPU6050 mpu;
      int16_t ax, ay, az;
      int16_t gx, gy, gz;

      void setup() {

      Serial.begin(9600);
      Wire.begin();
      mpu.initialize();
      }

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

      long x=map(ax,-17000,17000,20000,20008);
      long y=map(ay,-17000,17000,30000,30008);
      Serial.println(x);
      delay(10);
      Serial.println(y);
      delay(10);
      if( analogRead(A0)>= 1000){
      Serial.println(80000);
      }
      if( analogRead(A1)>= 1000){
      Serial.println(90000);
      }
      if( analogRead(A2) >= 1000){
      Serial.println(70000);
      }if( analogRead(A3)>= 1000){
      Serial.println(60000);
      }
      if( analogRead(A4)>= 1000){
      Serial.println(60500);
      }

      }

      error:
      exit status 1
      Error compiling for board Arduino Uno.

      • Tested this code and works fine and compile a well. You might have not installed the MPU6050 library as stated in article. Install and then use this code.

        #include

        MPU6050 mpu;
        int16_t ax, ay, az;
        int16_t gx, gy, gz;

        void setup() {

        Serial.begin(9600);
        Wire.begin();
        mpu.initialize();
        }

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

        long x=map(ax,-17000,17000,20000,20008);
        long y=map(ay,-17000,17000,30000,30008);
        Serial.println(x);
        delay(10);
        Serial.println(y);
        delay(10);
        if( analogRead(A0)>= 1000){
        Serial.println(80000);
        }
        if( analogRead(A1)>= 1000){
        Serial.println(90000);
        }
        if( analogRead(A2) >= 1000){
        Serial.println(70000);
        }if( analogRead(A3)>= 1000){
        Serial.println(60000);
        }
        if( analogRead(A4)>= 1000){
        Serial.println(60500);
        }

        }

        • Hello, thank you so much for your replay. I solved my problem that night, I didn’t know if I install and copy one library two times I will had problem with it.!!! I go to my installed path (library folder) and delete one of them and everything worked well. #New_experience

  3. how you get these values?….where can i get this values? cause i use arduino joystick for the movement and button for they any funtion for the keyboard…i dont know which value should i use for my x and y joystick…and values for my button…note..i use A0 and A1 for joystick and for the button i use 1,2,3,4..,5,6..etc…please help me

    f( analogRead(A0)>= 1000){
    Serial.println(80000);
    }
    if( analogRead(A1)>= 1000){
    Serial.println(90000);
    }
    if( analogRead(A2) >= 1000){
    Serial.println(70000);
    }if( analogRead(A3)>= 1000){
    Serial.println(60000);
    }
    if( analogRead(A4)>= 1000){
    Serial.println(60500);

    • My project and code is for MPU based and with 4 buttons attached for analog button input. If you want to make this with joystick please give me the details for your joystick model and its data sheet because it may vary according to the joy stick you are using.

  4. if( analogRead(A0)>= 1000){
    Serial.println(80000);
    }
    how to know this value?
    if( analogRead(A1)>= 1000){
    Serial.println(90000);
    }
    if( analogRead(A2) >= 1000){
    Serial.println(70000);
    }if( analogRead(A3)>= 1000){
    Serial.println(60000);

    • I use arduino leonardo as main board..i use pin 4 to 13 for the button press and add A2 for one more button press..i use Dual-Axis XY Thumb stick gane controller arduino pic..+ the mpu6050..everything work..only that i want to connect it to the hc-05 bluetooth..please help me

  5. This is my code:

    #include
    #include
    #include
    #include
    #include
    #include

    const uint8_t Pin_XAxis = A0; // Pin for joy horizontal axis
    const uint8_t Pin_YAxis = A1; // Pin for joy vertical axis

    const int JoyCenter = 1023 / 2; // max value for 10-bit ADC / 2
    const int JoyDeadzone = 50; // +/- area around the center to ignore

    KeyboardButton moveForward(‘w’);
    KeyboardButton moveLeft(‘a’);
    KeyboardButton moveBackward(‘s’);
    KeyboardButton moveRight(‘d’);

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

    void setup() {

    pinMode(4, INPUT_PULLUP);
    pinMode(5, INPUT_PULLUP);
    pinMode(6, INPUT_PULLUP);
    pinMode(7, INPUT_PULLUP);
    pinMode(8, INPUT_PULLUP);
    pinMode(9, INPUT_PULLUP);
    pinMode(10, INPUT_PULLUP);
    pinMode(11, INPUT_PULLUP);
    pinMode(12, INPUT_PULLUP);
    pinMode(13, INPUT_PULLUP);
    pinMode(A2, INPUT_PULLUP);

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

    void loop() {

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

    vx = (gx+1)/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-1)/200; // same here about “-100”

    Mouse.move(vx, vy);

    delay(20);

    //if the button is pressed
    if (digitalRead(12) == LOW) {
    //Send an ASCII ‘L’,
    Keyboard.write(76);
    }
    if (digitalRead(13) == LOW) {
    //Send an ASCII ‘B’,
    Keyboard.write(66);
    }
    if (digitalRead(4) == LOW) {
    //Send an ASCII ‘R’,
    Keyboard.write(82);
    }
    if (digitalRead(5) == LOW) {
    //Send an ASCII ‘M’,
    Keyboard.write(77);
    }
    if (digitalRead(6) == LOW) {
    //Send an ASCII ‘E’,
    Keyboard.write(69);
    }
    if (digitalRead(7) == LOW) {
    //Send an ASCII ‘F’,
    Keyboard.write(70);
    }
    if (digitalRead(8) == LOW) {
    //Send an ASCII ‘G’,
    Keyboard.write(71);
    }
    if (digitalRead(9) == LOW) {
    //Send an ASCII ‘H’,
    Keyboard.write(72);
    }
    if (digitalRead(10) == LOW) {
    //Send an ASCII ‘I’,
    Keyboard.write(73);
    }
    if (digitalRead(11) == LOW) {
    //Send an ASCII ‘J’,
    Keyboard.write(74);
    }
    if (digitalRead(A2) == LOW) {
    //Send an ASCII ‘K’,
    Keyboard.write(75);
    }

    int x = analogRead(Pin_XAxis); // Read X axis
    int y = analogRead(Pin_YAxis); // Read Y axis

    moveLeft.set(x JoyCenter + JoyDeadzone);

    moveForward.set(y JoyCenter + JoyDeadzone);
    }

  6. SOS!!!
    Hello again, I hope you are doing well, I stuck in your codes, I wanna edit your codes but I didn’t know how to change it. my purpose is edit your code to has two button (just right click and left click) like a real mouse, I don’t know how to change your transmitter and receiver codes.
    please guide or send me those codes. ([email protected])
    Thank you so much for your supporting.

  7. sorry I forget to say I used Arduino lilypad as transmitter and pro micro for connect to laptop.
    and used lilypad A4 @ A5 pins for MPU6050 and A3 @ A4 pins for my buttons.

SHARE YOUR THOUGHTS & COMMENTS

Please enter your comment!
Please enter your name here