Raspberry Pi 3 Game Controller

by Biaver_Instructables in Circuits > Raspberry Pi

47 Views, 0 Favorites, 0 Comments

Raspberry Pi 3 Game Controller

474181355_583296214509678_3792711270987062118_n.jpg

This is a homemade controller using a breadboard and some python script, no fancy circuitry here!

Supplies

474181355_583296214509678_3792711270987062118_n.jpg

Alright! First we have the supplies:

  1. Raspberry Pi 3 (running Raspberry Pi OS)
  2. T-Cobbler Plus (GPIO breakout header)
  3. 4 pushbuttons
  4. 5 LEDs
  5. 1 Decade Counter
  6. 5 330Ω Resistors
  7. Color coded jumper wires
  8. Python Script (GPIO to Keyboard)
  9. Godot Game (ARM32)

Build the Circuit

473828976_478670601696791_7348318267791869042_n.jpg

Build the circuit as shown in the photo.

Connect the T-Cobbler to the breadboard and then connect the ribbon cable to the GPIO Pins on the Pi with the T-Cobbler pointing away from the Pi.

Pushbutton:

  1. Up: GPIO 16
  2. Down: GPIO 6
  3. Left: GPIO 5
  4. Right: GPIO 19

Decade Counter:

  1. Pin 16: 3.3v
  2. Pin 8: GND
  3. Pin 14: GPIO 25
  4. Pin 13: GND
  5. Pin 3: LED 1
  6. Pin 2: LED 2
  7. Pin 4: LED 3
  8. Pin 7: LED 4
  9. Pin 10: LED 5
  10. Pin 1: Pin 15 (RESET)

Raspberry Pi and Game Setup

Screenshot 2025-01-23 153315.png

Power on your Raspberry Pi and place this file into the desktop folder. Download it from the Google Drive

https://drive.google.com/file/d/1ayN0FVdCz9yBr819A1t-sXqjbeep66S_/view?usp=drive_link

Custom Python Script

You'll need this script running on the Pi for the controller to work. It translates pushbutton inputs into keyboard presses using an external library. Download the .py file.


import RPi.GPIO as GPIO

from pynput.keyboard import Controller, Key

import time


GPIO.setmode(GPIO.BCM)

keyboard = Controller()


# Setup

clock = 25

inputPins = [16, 6, 5, 19]

buttonKeys = [Key.up, Key.down, Key.left, Key.right]

delay = 0.03

GPIO.setup(PIN, GPIO.OUT)

for pins in inputPins:

GPIO.setup(pins, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)


def keyboard():

for pins in inputPins:

if GPIO.input(inputPins[pins]) == HIGH:

keyboard.press(button_keys[pins])

else:

keyboard.release(button_keys[pins])


def clock_speed():

GPIO.output(clock, GPIO.HIGH)

time.sleep(delay)

GPIO.output(clock, GPIO.LOW)

time.sleep(delay)


try:

while True:

if delay < 0:

delay += 0.001

elif delay > 0.03 or GPIO.input(16) == HIGH or GPIO.input(6) == HIGH:

delay -= 0.001

else:

delay += 0.001

keyboard()

clock_speed()

except KeyboardInterrupt:

GPIO.cleanup()

Downloads

Run

Screenshot 2025-01-23 162550.png

Once you have the breadboard and code setup, run it!

  1. Run the controller.py script
  2. Run the Raspberry Pi 3 Game.arm32

Do not run it in full screen or the game will lag hard.