Create Your Own VR Game Controller Using Arduino

28K2942

Intro: Create Your Own VR Game Controller Using Arduino

If you are a gamer and a hardware geek at the same time, you obviously know how hard it can be to choose how to spend weekend, playing games or making cool hardware. This project will give you heaven of the both worlds!

We will create an Arduino based game controller which detects motion which works as a great controller for VR games. But it's not limited to VR, it can be used to play any supported PC, XBox or PlayStation game.


Required Hardware:

  • Arduino Uno
  • MPU6050

STEP 1: Programming Arduino

If you are not interested in details how the program works then simply upload the code given in resources.zip to Arduino Uno and jump to Step 2.

_

Initializing Sensor

To initialize the sensor, we first initialize the I2C communication using standard Wire library then configure the MPU. If configuration fails, then the Arduino goes in forever while loop as it’s useless to continue.

void InitializeMpu()
{
  Wire.begin();
  Wire.setClock(400000UL);
  i2cData[0] = 7; // Set the sample rate to 1000Hz - 8kHz/(7+1) = 1000Hz
  i2cData[1] = 0x00; // Disable FSYNC and set 260 Hz Acc filtering, 256 Hz Gyro filtering, 8 KHz sampling
  i2cData[2] = 0x00; // Set Gyro Full Scale Range to ±250deg/s
  i2cData[3] = 0x00; // Set Accelerometer Full Scale Range to ±2g
  while (i2cWrite(0x19, i2cData, 4, false));   // Write to all four registers at once
  while (i2cWrite(0x6B, 0x01, true));   // PLL with X axis gyroscope reference and disable sleep mode
  while (i2cRead(0x75, i2cData, 1));
  if (i2cData[0] != 0x68)
  { // Read "WHO_AM_I" register
    Serial.print(F("Error reading sensor"));
    while (1);
  }
}
Reading the Motion Data

Motion data is read from IMU sensor (MPU6050) in every loop and the respective gyro and accelerometer values are updated. The following function does this job.

void UpdateReadings()
{
  while (i2cRead(0x3B, i2cData, 14));
  accX = ((i2cData[0] << 8) | i2cData[1]);
  accY = ((i2cData[2] << 8) | i2cData[3]);
  accZ = ((i2cData[4] << 8) | i2cData[5]);
  tempRaw = (i2cData[6] << 8) | i2cData[7];
  gyroX = (i2cData[8] << 8) | i2cData[9];
  gyroY = (i2cData[10] << 8) | i2cData[11];
  gyroZ = (i2cData[12] << 8) | i2cData[13];
}
Computing Angles Using Complementary Filter

Angles can be computed using complementary filter with a good accuracy. We will use complementary filter since it’s fine for most of the application. But if you need even better accuracy, you can use Kalman Filter.

void ComputeAngles()
{
  double roll  = atan2(accY, accZ) * RAD_TO_DEG;
  double pitch = atan(-accX / sqrt(accY * accY + accZ * accZ)) * RAD_TO_DEG;

  double gyroXrate = gyroX / 131.0; // Convert to deg/s
  double gyroYrate = gyroY / 131.0; // Convert to deg/s

  compAngleX = 0.98 * (compAngleX + gyroXrate * dt) + 0.02 * roll; // Calculate the angle using a Complimentary filter
  compAngleY = 0.98 * (compAngleY + gyroYrate * dt) + 0.02 * pitch;
}
Set Controller Data Object

We need a dataForController_t object to send data to the computer/host as a joystick. We use the following code to create and fill dataForController_t object.

dataForController_t getControllerData(double _roll, double _pitch) 
{
  //Restrict roll and pitch to +-90
  if (_roll > 90 || _roll < -90)
    _roll = 90 * _roll / abs(_roll);

  if (_pitch > 90 || _pitch < -90)
    _pitch = 90 * _pitch / abs(_pitch);

  //Get new instance with default values of buttons and sticks
  dataForController_t controllerData = getBlankDataForController();

  controllerData.rightStickX =  (int)(_roll * 1.42 + 128);
  controllerData.rightStickY = (int)(_pitch * 1.42 + 128);

  //You can use these and many other buttons also
  //  controllerData.triangleOn = !digitalRead(2);
  //  controllerData.circleOn = !digitalRead(3);
  //  controllerData.squareOn = !digitalRead(4);
  //  controllerData.crossOn = !digitalRead(5);

  return controllerData;
}

In loop() we will call this function as,

dataForController_t controllerData = getControllerData(compAngleX, compAngleY);
Send Controller Data to Computer

To send this data to joystick simple call the setControllerData() function as follows,

setControllerData(controllerData);

STEP 2: Connect the MPU Sensor

Connect the MPU sensor with Arduino Uno as,

Arduino MPU6050

3.3V ————- Vcc

Gnd ————- Gnd

A5 ————— SCL

A4 ————— SDA

After you have programmed your Arduino Uno with the given code, connect the MPU. You can uncomment the following line to check on serial monitor/plotter if everything is working fine, moving the sensor will change the values accordingly. Be sure to comment it again after testing.

//#define DEBUG

STEP 3: Turn Arduino Into Joystick

To turn Arduino into joystick first you need to turn start Arduino into DFU mode. To do this, just connect the pins mentioned in the image.

On Windows the Arduino (usb device) will disconnect with a sound (be-dun). And connect again (buh-din!). Now Arduino is on DFU mode, just open the TurnIntoAJoystick.bat file (see resources section) and it will turn your Arduino into a joystick.Atmel FLIP must be installed for this to work. In windows 10 go to Settings > Devices > Connected Devices and it will show up as a Joystick.

STEP 4: Testing the Joystick


You can test the joystick using TestJoystick.exe (see resources section). Just connect the Arduino Joystick into your computer and run TestJoystick.exe. It will show a floating plane that rotates as you move the joysytick. You can add more buttons and features and use it the way you like. For more information check out the UnoJoy page.

If it works fine then go ahead and try it with your favorite games!

26 Comments

Thanks for sharing the project, but I can't connect the controller with Steam VR, since it doesn't give me any option to pair it or anything. How can I do it?

Can you also use it for Steam VR?

How can i connect it to Steam VR?

yes its great, its better with 2 if you can get it right.


Yes it works well, just assign the values in steam control settings.

Hello and thank you very much- it was hard to do at first but i got it in the end.

I want to ask, How can i get the temperature readings off the mpu6050 aswell and render it, Do you think it will need a seperate widget?

Always learning

G

Source code already contains tempRaw variable. This is the formula to convert it to degrees,

Temperature in degrees C = (TEMP_OUT Register Value as a signed quantity)/340 + 36.53

Thank you, I did find it, I started making a tkinter widget and got stuck with the serial protocol as I didn't have the device with me.

Will try again in the morning, I'm using this device to control a motion driving simulator. Thank you again for making this possible,

How can I slow the MPU6050 down?
It is too sensitive for the motion driving simulator I made, also with Arduino ( could double as electri car)
I also used it on quake and it's just too sensitive.
Please help.
Thank you
Hi friend, I would like to upload your project for arduino leo, but in the github there is no library for leo, how can I fix it?
I give up, the instructions to install leojoy are so approximate, that many have also asked for help on the arduino forum and have never received an answer...

thanks again for the help, I'll try another library :)
Thank you so!
in your opinion, is it possible to set the throttle using 1 of the 3 axis?
Hi, how do we write the codes
I Couldn't find it
hi,i had to ask you that how can i use mpu 6050's z angle as right joystick's x-axis.
great project btw, keep up the good work
This makes my life. I have big disabilities and I'm using it on my Xbox adaptative controller. Thank you. Will like to buy some 🍻🍻🍺🍺
Glad to know it is helping people. Let me know if you need any help in extending the features :)
Thank you a lot for your work. I have a disability of 96%. I use it with the adaptative controller for Xbox, put the device in my head. It would be awesome to make it smaller, sometimes it falls because is heavy and the wire also it does force. Thanks you a lot again.🙌🏻🙌🏻🙌🏻🙌🏻🙌🏻👏🏻👏🏻👏🏻
More Comments