Somatosensory game controller

From Microduino Wiki
Jump to: navigation, search
Language: English  • 中文


You can control games by the somatosensory analog keyboard--W, A, S and D.


Detect game controller's motion change with three-axis acceleration sensor MPU6050 and then use Microduino-CoreUSB analog function to simulate keys of W, A, S and D.


Module Number Function
Microduino-CoreUSB 1 Core board
Microduino-10DOF 1 Vector sensor module

Hardware Buildup

  • Setup 1:Connect CoreUSB to the computer, open program examples, select the right board and serial port, then download the program.


  • Setup 2:Stack CoreUSB and 10DOF.
  • Setup 3:Connect CoreUSB to the computer which will identify a keyboard and display " HID Keyboard Device " in the device manager.
Microduino-sensorhub rule.JPG

Software Debugging

Code Description

  • Control variable initialization:
MPU6050 accelgyro;   //Three-axis acceleration sensor 
int ax,ay,az;       //Three-axis acceleration sensor variable
float Ax,Ay,Az;

void setup(){
  Keyboard.begin();      //Keyboard simulation begins.
  Serial.println("Initializing I2C devices...");
  accelgyro.initialize();     //Accelerator initializes. 
    // verify connection 
  Serial.println("Testing device connections...");
  Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
  • Judge the controller's motion status. Moving towards left, right, front and back corresponds to the key A, D, W and S respectively.
   void loop() {
    accelgyro.getAcceleration(&ax, &ay, &az); 
    Ax = ax/16384.00;         //Calculate X-axis's acceleration 
    Ay = ay/16384.00;         // Calculate Y-axis's acceleration
    Az = az/16384.00;         // Calculate Z-axis's acceleration
    if(Ay >= 0.6 )            //Turn the controller leftwards 
    {'A');    //Keyboard 'A'     
    else if(Ay <= -0.6)       //Turn the controller rightwards 
    {'D');   //Keyboard 'D' 
    else if(Ax >= 0.6)       //Turn forward 
    {'S');   //Keyboard 'S' 
    else if(Ax <= -0.6)     //Turn backward 

    {'W'); //Keyboard 'W'
        Keyboard.releaseAll();  //Release the key 


Open NotePad on the computer and connect to Coreusb. By turning the controller leftwards, you'll see the character "A" is printed on the NotePad while by turning the controller back to the original position and you'll see the NotePad stops printing. That also works for the other keys.