Raspberry Pi 3 Game Controller
by Biaver_Instructables in Circuits > Raspberry Pi
47 Views, 0 Favorites, 0 Comments
Raspberry Pi 3 Game Controller

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

Alright! First we have the supplies:
- Raspberry Pi 3 (running Raspberry Pi OS)
- T-Cobbler Plus (GPIO breakout header)
- 4 pushbuttons
- 5 LEDs
- 1 Decade Counter
- 5 330Ω Resistors
- Color coded jumper wires
- Python Script (GPIO to Keyboard)
- Godot Game (ARM32)
Build the Circuit

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:
- Up: GPIO 16
- Down: GPIO 6
- Left: GPIO 5
- Right: GPIO 19
Decade Counter:
- Pin 16: 3.3v
- Pin 8: GND
- Pin 14: GPIO 25
- Pin 13: GND
- Pin 3: LED 1
- Pin 2: LED 2
- Pin 4: LED 3
- Pin 7: LED 4
- Pin 10: LED 5
- Pin 1: Pin 15 (RESET)
Raspberry Pi and Game Setup

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

Once you have the breadboard and code setup, run it!
- Run the controller.py script
- Run the Raspberry Pi 3 Game.arm32
Do not run it in full screen or the game will lag hard.