Game Cotroller Using Arduino UNO
by vinvout-io in Circuits > Arduino
336 Views, 4 Favorites, 0 Comments
Game Cotroller Using Arduino UNO
I saw some videos about game controllers and I wanted to make one. But I didn't know that I couldn't make them with Arduino UNO until I programmed it. The libraries used (keyboard.h) in those projects allow the 32u4 and SAMD based boards (Leonardo, Esplora, Zero, Due, and MKR Family) to appear as a native Mouse and/or Keyboard to a connected computer, so an Uno(ATmega328p) won't work directly. Hmm, but I know another way to do it that's what I am sharing here.
Supplies
- push button x 4
- Resistors 220 ohm x 4
- Arduino UNO x 1
- Breadboard
- Cardboard
- Glue gun
Making It Possible
When I didn't know c++ I used to program Arduino using python. While using python the Arduino always transmits and receives data. So I can utilize that to make our USB Game controller. For this project, we also need to install pyfirmata and keyboard(just open a command prompt and type "pip install pyfirmata keyboard"). Before that, we also need to upload a sketch called standard frimata from Arduino ide to establish serial communication between the PC and the Arduino.
Tinker Cad Circuit Diagram
just wire it up.
Connect the pushbuttons and resistors as shown in the schematic
Understanding How It Works
When we play car games we usually press the keys 'W', 'A', 'S', 'D' so here also when we press the push buttons we just need to give those characters to the computer.
ENCLOSURE
First Connect the circuit as shown in the schematic then place everything on a breadboard but do not connect it to the Arduino now. Use cardboard to cover the breadboard and add a little flavor to it(some stickers) and now place the Arduino to the back of the cardboard and connect all the wires to Arduino. And don't forget to put some holes in the cardboard for the pushbuttons. I also hot glued some pieces of pens to the pushbuttons so it will give some height and be much more comfortable.
Final Part
Just connect it to your computer and run the given python code then take a game and start playing voila!!!
Do not forget to install pyfirmata and keyboard
Game - SuperTuxKart
If you need further more explanation about the code and other kinds of stuff then try watching this video
Final Output
CODE
''' Game controller python for serial communication what to do when pressing a pushbutton send w/a/s/d to the computer''' """https://github.com/boppreh/keyboard""" import pyfirmata import keyboard import time board = pyfirmata.Arduino('/dev/ttyUSB0') #Find the port from arduino ide it = pyfirmata.util.Iterator(board) it.start() w_pin=board.get_pin('d:6:i') a_pin=board.get_pin('d:11:i') s_pin=board.get_pin('d:13:i') d_pin=board.get_pin('d:8:i') while True: w=w_pin.read() if w==1: keyboard.press("w") elif w==0: keyboard.release("w") a=a_pin.read() if a==1: keyboard.press("a") elif a==0: keyboard.release("a") s=s_pin.read() if s==1: keyboard.press("s") elif s==0: keyboard.release("s") d=d_pin.read() if d is True: keyboard.press("d") elif d is False: keyboard.release("d")