Project Description:

Creating a motion based controller that can communicate data back and forth from the arduino board to the Unity 5 game design engine. In this case, we are using a triple axis accelerometer (Adxl345) to control the X and Y position of game objects inside the Unity engine. Both the accelerometer and the arduino are assembled into a rectangle box to form a comfortable, user friendly controller.



Adhesive Glue

Arduino Board
Basic Soldering skills

Laser Cutter (Bandsaw would also work, but not as precise)

Plexiglass (Or any other materials you wish to assemble the arduino and accelerometer in)
USB 1.1 cable


Step 1: Mechanical Assembly

First, sotter your pins in the accelemenator. In this example, all the pins are soldered, but the only necessary pins are the: VIN port, Grnd Port, SDA port, and SCL port. Place the accelerometer on the breadboard so that all the pins are in different rows, but in the same column. Next, connect the four wires from the arduino to the accelerator. One wire should be going from the 5V port on the arduino, to the VIN port on the accelerometer. Both ground ports on the arduino and the accelerometer should also be connected. The SDA and SCL ports on the arduino should also be connected (and matched) to the SDA and SCL ports on the accelerometer. Check the back of your arduino to find out which of its ports are the SDA and SCL. Don't worry about connecting a power supply, all of the power will be feed to the arduino through the USB 1.1 cable.

Step 2: Design Your Game

This is probably the most important part of the game, and it is highly recommended to be comfortable with unity for this project. You can really make any type of game that you want, we decided to make a ball and labyrinth style game. When making the game make sure to make all the objects that need to be controlled children of a larger game object.

Step 3: Coding

This is probably the most difficult part of the project. The first thing you are going to want to do is download the code library for the ADXL345 accelerometer in Arduino IDE. It can be found on the adafruit website. Once uploaded to the arduino, it will spit out a bunch of data into the serial monitor. Really, what would be ideal here is to grab the XYZ positional data and send it straight to the unity project, but sending floats isn't as simple as it seems. When sending the data, unity will always read it in as a string, which proves difficult to work with. To work around this, what we can do is a series of if statements for the arduino. for example:

if( event.acceleration.y >3){



We are going to want to do theses types of comparisons for eight different directions: Up, Down, Left, Right, Up-Right, Up-Left, Down-Right, and Down-Left.

By sending these strings of directions, we can actually use them! In Unity, on our player controller (which should be attached to the object or group of objects you wish to control) We are going to want to use an if statement that will make use of the string being read in:

if(serial.ReadLine() == "left"){


transform.Rotate((-Time.deltaTime * 50), 0 , 0 );


And again, we are going to want to do this for all eight directions.

Now, the toughest part is to actually allow the data to be sent to unity. In the same script, we are going to want to allow the Serial library: using System.IO.Ports

Then, we are going to want to create a new serial port instance:

SerialPort serial = new SerialPort("COM5", 9600);

**Note to check which COM your arduino is using, you can do this in Arduino IDE, but the most common are COM3 and COM5.**

Inside our start function we want to now open the serial port:



serial.ReadTimeout = 10;


After all this, try testing out the data transfer, and you should see that it works!

Step 4: Cutting

Now that you have your game coded and your arduino communicating with Unity 5, the project is essentially finished. However, you want to to assemble it into a user friendly, attractive controller for all your friends to use! To create this controller you can use any material you'd like, just make sure that it doesn't throw off your coordinates in your accelemenator, you may have to go back and adjust your code accordingly. Also, be sure to leave your USB 1.1 port open to connect your controller to your computer.

In this example, we used plexiglass. First, you will have to measure out the dimensions of each piece to fit your needs. Again, be sure to take an account for USB 1.1 port opening. Next, using a laser cutter cut out all pieces of for the controller box. You can also cut the plexiglass with a bandsaw, but the laser cutter is more accurate. In addition, we used a bandsaw to cut out a small piece of wood that were the same dimensions as the arduino. While this is not necessary it is recommended for the assembly step.

Step 5: Controller Assembly

(If you did not cut the piece of wood as recommended in the previous step, skip this first instruction)
Take your piece of wood and place the arduino board on top of it. Then screw your board to the wood, making sure it's firmly in place.

Next, glue your wood to the bottom piece of your plexiglass. If you skipped the wood, you could screw the board directly to the plexiglass, though this could damage it or become uncomfortable if it's not thick enough for the screw.

Afterwards, remove the adhesive sticker from the back of your breadboard, then place it up close and snug next to the arduino.

Next, glue your the rest of your box pieces to form the controller, again make sure the USB 1.1 port is still accessible.

Step 6: Plug and Play

You're finished! Hook your sweet controller up to your computer using the USB 1.1 cable, and test out your game!

<p>That's a neat setup :)</p>

About This Instructable




More by lefevere1je:Tylt 
Add instructable to: