Arduino/Unity Car Driving Experience

by MyOwnPersonalJetPack in Circuits > Arduino

1493 Views, 7 Favorites, 0 Comments

Arduino/Unity Car Driving Experience

PXL_20210523_165247237.PORTRAIT(1).jpg

What is this project

I made a car out of cardboard with a steering wheel, gear shaft, and pedals made out of everyday items, measured the movement using arduino potentiometers, and buttons, and translated it to a fully functional physics based car in the Unity engine. There aren't many online tutorials on how to make unity and arduino work togetherm so I decided to document what I learned in the form of a project. (Plus I wanna drive and for some reason my parents say 12 years old is too young)


Idea behind project


Hi, I am a 12-year-old kid who loves electronics, coding, space, and building. I remember, on my 10th birthday, I began sitting in the front seat of our car, and I loved it. Not only did I not get car sick, but it reminded me of a beloved game, Asphalt 9. If you don't know what that is, it is a car racing game. (I hope you can see where this is going).

Jump forward 1 year, and I start getting into the free game building engine, Unity. After building my first character movement project, I fell in love and convinced my parents to buy me a $10 tutorial on c# (the Unity programing language). Since then, I have created a space simulation using real physics equations, a game with soft bodies and toggle-able ragdoll physics, experimenting with Perlin noise and procedural terrain generation, and now, using Arduino to create a real-life car game.

Things to keep in mind while building I would like to say a few things that came in handy when constructing this.

1. If you have any questions or issues while constructing, please contact me at nasasid373@gmail.com. I would love to help! This build was very fun, and I hope you enjoy it too.

2. Keep in mind that the build can be frustrating at times. There are times where errors just keep popping up, and it is very overwhelming. I would suggest taking a break, even if you don't want to. I know that the lack of information available online was scarce and that drove me nuts (I was so desperate I even went to the second page on google). But keep going! The reward is worthless.

3. Except for the electronics, none of the building materials or tools are mandatory. Because I have an awesome and creative family, I had all of the materials at my house lying around, and the electronics I had from old Arduino builds. In the end, the project cost me EXACTLY $0!! I am very happy.

4. I just wanted to make it as arcade-style-car-game as possible, without the cost of getting a $4,000 (I know the price freaked me out too) arcade car game machine. Plus, mine has a working gear shaft, and a customizable game if you know a bit of coding.

5. Good luck!!!

Supplies

What you need:


Parts (these parts are only suggested and can be substituted. For example, I can replace foam board with cardboard, although I suggest cardboard as it is st. Also, you do not have to get any of the items from the links provided):

1. foam board (Foam board - amazon)

2. skewers (Skewers 100 pack - amazon)

3. rubber bands (rubber bands - amazon)

4. Legos and/or Knex (really any building material works - it could be wood or metal if you wish)

If you were to choose Legos and you don't have any, I would suggest:

Lego techic build - this would give you all the pieces you would most likely need.

Knex set - This should be all the Knex you will need

5. zip ties (500 zip ties - amazon)

6. non-stretchy string (Spoll of yarn - amazon)

7. tape (duct tape - amazon)

Electronic parts (must needs):

1. Lots of wires (male-male, male-female, and female-female) (Wires assortment - amazon)

2. 5 breadboards (some small and some large) (4pcs Breadboard - amazon: I would really suggest these)

3. 3 potentiomenters (Potentiometers - amazon)

4. 10k ohm resistor (Resistors - amazon: these are more than enough, although if you are looking to do anymore Arduino projects I suggest getting these)

5. 4 buttons (Buttons - amazon: more than enough but would suggest if you are doing more arduino projects in the future)

6. Arduino Uno (an Arduino Mega would work too) (Arduino Uno - amazon)

Software:

1. Unity (free) Download Unity

2. Arduino software (free) Download Arduino Software

Tools:

1. exact o-knife (Not necessary but helps. Also, be careful when using this tool. I have cut myself with it and it is not fun)

2. Scissors

3. cutting board (non-static so perfect for electronics)

The Steering Wheel Electronics

Capture.PNG
PXL_20210407_175227910.jpg
PXL_20210407_175326775.jpg
PXL_20210407_175341843.jpg

The steering wheel schematic is very simple. All that is required is supplying voltage and ground to the potentiometer (no resister required) and a connection between the middle pin and the analog 2 (A2) input on the Arduino Uno. The analog pin reads the input as a numerical value, so instead of using the digital pins which is either 1 or 0 (true or false), we use the analog pins. The potentiometer must be attached to the A2 pin because it will come into play once we start coding. Don't worry about the end product, and how you want your steering wheel to work/look yet because right now we're just getting all the electronics handled.

Gear Shaft Electronics

Capture.PNG
PXL_20210417_173345417.jpg
PXL_20210417_173357579.jpg
PXL_20210417_173404895.jpg

For this schematic, I a 2 gear system; forward and reverse. Feel free to add as many shafts as you would like, such as parking, but with the simple tools/material I am using 2 gears works just fine.

The Arduino button mechanics for the gear shaft is quite simple. This model is comprised of 4 buttons and 2 breadboards placed right next to each other. The 2 extra buttons on the other breadboard are required for the building process.

Pedal Mechanics

Capture.PNG

The pedal is very self-explanatory. You use 2 potentiometers to measure how hard you brake and accelerate in whatever direction you wish to go in (either forward or reverse). I didn't have enough potentiometers to show my real-life model, but the Tinkercad schematic should be enough to work off. I tried my best to fit it on one breadboard, but it just wouldn't work for the design that I have in mind for the pedal.

The green and yellow pins connect to analog inputs, which reads the number of degrees that have turned.

All the Electronics

Capture.png

In the end, your electronics should be the same as mine, or pretty similar. You might notice that there is another breadboard right below the Arduino in the Tinkercad schematic, which is just like a hub for electronics so it doesn't get too messy. You don't have to include this, but you can if you'd like. I was unable to make a real-life model because I didn't have enough potentiometers to make it, but everything else should be the same. Keep in mind the wire color choices are not necessary. For me, I organized the colors for the sake of it being less complicated. It does not harm your end product if you change the colors. If you have all of this completed, you are ready for the building stage!

Troubleshooting Electronics Using Code

Capture.PNG

In this section, you will make sure that you wired everything up correctly, by making sure that the code gives the right output. I like to do this as sort of a benchmark every so often to make sure that everything is working, and if there is a problem in the future I know what is working. It only takes a basic understanding of Arduino coding, to understand what this code means, and if it is your first time coding in Arduino software then I would suggest looking up a basic tutorial. There are many tutorials online about it. If everything is wired up correctly, the output should give you a number value for each potentiometer and button. If not, then recheck that all the electronics are wired properly. You can often find a solution by pasting your output into google.

The code can be found in the google doc below:

https://docs.google.com/document/d/12DS-lb2IfXig1k...

All you have to do is copy the code and paste it into an Arduino project. The results should look like the picture above.

Design for Steering Wheel

PXL_20210501_224140079.jpg
PXL_20210501_224220436.jpg
PXL_20210501_224324004.jpg
PXL_20210501_224356884.jpg
PXL_20210501_224404399.jpg

I think the steering wheel has a simple design. I created a wheel attached to a gear, which is connected to a rubber band to add restraint as you pull the steering wheel backward, and then used gears to lower the amount of rotation the potentiometer reads so that the wheel movement could fit the turning diameter of the potentiometer. Then I just shaved 2 sides of a Lego shaft to fit in the potentiometer with a sheetrock knife. I encourage a different solution, but for my case, this was the only solution available. I did have to do it with my dad, however, because it was kind of dangerous, so please be cautious of that.

Design for Gear Shaft

PXL_20210501_223723002.jpg
PXL_20210501_223739798.jpg
PXL_20210501_223818985.jpg
PXL_20210501_223924310.jpg

I think my design was a cool looking way to solve my problem, and quite creative as well. You need to create a system that when you move the gear shaft back and forth, it is pushing down on the button, creating a sensor system. Because there is only forward and reverse, I only had to do the circuitry for one side (forward side), and if that is not being pressed, then the car is going in reverse. It is a simpler and more reliable way of accomplishing this problem. I used Legos, but you can use anything (e.g. cardboard, Knex, foam board, 3d printer filament - if you have a 3d printer). For me, this method worked, but if you are having issues, you can also use a potentiometer, and measure the change. Although, that is more complicated, will require slight changes to the code, and this works.

Design for Pedals

PXL_20210512_013636942.NIGHT.jpg
PXL_20210512_013709920.NIGHT.jpg
PXL_20210512_013722060.jpg
PXL_20210512_013731111.jpg
PXL_20210512_013736960.jpg
PXL_20210505_221410168.jpg
PXL_20210505_221437359.jpg
PXL_20210505_221442633.jpg
PXL_20210505_221452979.jpg

This was the most complicated out of the 3 for me. I had to create a sort of "arm", where when pressure is applied to the end, it converts to rotational force. This took me 4 days individually because it just kept breaking, and I was getting irritated. After the "arm", everything else was quite easy. Once I got the electronics and build working together, it was easy. I used rubber bands to add tension when you pulled it back, and a limit by cutting semi-circles into the box.

Troubleshooting

I like to troubleshoot periodically throughout my builds because it helps eliminate potential problems when they almost inevitably occur. In this case, it is to make sure all your electronics are hooked up properly. I made the mistake of doing this step once all of the building aspects are completed, which wasn't smart because when navigating through the wires to evaluate the issue, it took was quite annoying. I had to deconstruct my builds numerous times, which is why I suggest troubleshooting now. Remember, it can be both a software and hardware issue. Often I find myself searching the web "why is my Arduino software not working", but I usually make silly mistakes, like forgetting to hook up the right wires.

PUT IT ALL TOGETHER!!!!

PXL_20210523_013612954.jpg
PXL_20210523_013640763.PORTRAIT.jpg
PXL_20210523_013658146.PORTRAIT.jpg
PXL_20210523_013717225.NIGHT.jpg
PXL_20210523_013815007.jpg
PXL_20210523_013909586.jpg

YAY! We finally made it. Here, I just have a massive box that I found in my basement, and created a car-like arrangement.
For the pedal, I just cut a hole where my feet go and put it right there, leaving a space for the wires in the back.

For the gear shaft, I made 3 iterations before I successfully attached it to the car. I had a small box and cut to lines into the box (I used a ruler because you want to be as accurate as possible), and taped it A LOT. You'll see clearly what I did in the pictures above.

Finally, for the steering wheel, I did the same same thing as before; cutting 2 lines into one

Unity

CarController.PNG
CarPrefab.PNG
PXL_20210520_140355087.jpg
SerialController.PNG

Unity is not a difficult program, especially after getting into it. I am 12, with some understanding of code, and VERY little understanding of c# (c sharp).


For the actual building, there won't be too much coding for you, unless you decide to create your car script yourself. My car controller is based on this video, and I made changes to the initial code so that it properly communicates with Arduino. I would highly suggest starting there if you intend on coding this yourself.

Steps:

1. Download Ardity (the asset used to communicate your Arduino and Unity) here, and the arcade-style cars asset here in the Unity Asset Store

2. Do everything the guy does in this video until 3:00 in the video. By then, you should have: Setup the car Add the arcade-style camera follow the camera script

3. Paste Unity car controller into a new script attached to the car object.

4. Attach the SerialController in Ardity the Ardity folder under Scripts to the car object

5. You have all of Unity setup now. Attach each of the Colliders and transform components to the CarController script, like in the picture above by dragging and dropping. For the SerialController, make sure your baud rate value in unity is the same as the baud rate in the Arduino code, and make sure you have the right Arduino selected. In my case, COM4, but in many cases, you will be using a COM3 which is the computer's definition for Arduino Uno. I am using an Arduino Mega, which is why mine is different.

6. Now just plug in your Arduino and you should be good to go! If Unity isn't reading your Arduino, make you followed all the steps, and all your values are correct.

7. Plug your Arduino in, and press play! You should be good to go.