Joystick Game Controller Using Arduino UNO

by vinvout-io in Circuits > Arduino

210 Views, 2 Favorites, 0 Comments

Joystick Game Controller Using Arduino UNO

Screenshot_2023-04-20_12-17-51.png
Untitled design.png
inst subway1.png

Yes, that's right, we are going to build a joystick game controller for Subway Surfers using an Arduino UNO.

Note: it only works on a computer/Laptop, not a phone.

I was actually trying to make a mouse using this, but I got frustrated because I wasn't getting it right. So to cool me up, I searched online games and started playing subway surfers for fun and this was the moment I thought why can't I make a joystick to play this?

Supplies

  1. Arduino UNO
  2. Joystick

First of all, how do we play Subway Surfers on a computer we use WASD right? We can use the same logic here.

If you know a bit about Arduino, you know that you cannot give inputs to your computer from an Arduino UNO, like keyboard input. It is actually only available for some other boards like Esplora, Due, and MKR family boards, etc... Okay, so I used Python with Pyfirmata and thanks to BoppreH I used his keyboard library for doing this project. For this project, we need to install pyfirmata and keyboard (just open a command prompt and type "pip install pyfirmata keyboard").

This is really simple to make but it took me a while to figure out the threshold values for the joystick.

You cannot play it on a phone unless I figure out the port to which the Arduino connects to your phone.

inst subway3.png
inst subway4.png

When You move the joystick upwards(forward) we have a "W". Similarly "A" for left, "D" for right, and "S" to move down (or ROLL in subway surfers). Am I missing something?

Yes, the important piece of this game, skateboard. To activate the skate you need to press the "SPACE" I configured the button of the joystick as "space". But here comes another problem - if you want to connect a switch to the Arduino we have to configure it in the input pull up State or the pull-down state you can actually solder a resistor on the PCB of the joystick itself but I have soldered it and it was having some problems like it was always giving me float so I searched about it and I found this

Connections

inst subway 2.png

The connections are really simple we just connect the two pins the Vrx and Vry to A1 and A2 and the switch is connected to digital pin8. The GND and 5v are connected to the Arduino's GND and 5V. If you look at the above picture, you can see I have soldered a resistor between the five-volt and the switch pin of the joystick.

Code Explanation

Joystick For Subway Surfers! #diy #game #gamecontrollers
inst subway5.png

It is better to watch this video for the explanation of code. Skip to - 2:13 of the video

Before doing anything first we have to upload a sketch (Standard Firmata) to our Arduino to work with pyfirmata from the Arduino examples.


Note - another thing that I soldered the resistor to the joystick is because the pyfirmata does not support input pull-up


example for pressing a key and releasing it:

keyboard.press("w")

keyboard.release("w")

By running the code you will get the output from the joystick. You may need to adjust the threshold values in order to work it like butter.

I think there are there are some minor Flaws In my joystick.

One thing to remember is that Pyfirmata is not going to give you the values exactly like, that you would get from Arduino IDE.

Note to Linux Users

inst subway6.png
inst subway7.png

First I have only tried it on a Linux machine. So if you are using Linux you may need to run it as a root. Maybe you have to create a virtual environment for that. Now let's run it from the terminal so here the command is - sudo Python3 and whatever the name of your file.

After running it from the terminal you can watch the letters being printed as you change the direction

inst subway8.png
inst subway9.png

it's ready you just need to open the Subway Surfers from your browser and just play it. To make this joystick controller more comfortable I just cut out some cardboard pieces in such a way that it will be easier and more comfortable to hold the controller

Joystick For Subway Surfers! #diy #game #gamecontrollers

It may not work perfectly as we are not getting or giving correct threshold values for all the possible directions.

This is just a fun project.

Hope you liked it.

Thank you...


Code and Connections

circuitgamejoystickinstructable.png

Connect the Arduino to the computer and upload the code.

Connect the joystick