Projects / Control a robot arm from a computer game controller

Control a robot arm from a computer game controller

Intermediate2.5 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. UNO

    Centre the servos

    Send all joints to 90 degrees first.

  2. UNO

    Assemble the arm

    With the servos centred.

  3. UNO

    Wire the servos and power

    4 AA holder, shared ground, capacitor.

  4. UNO

    Upload arm.ino

    Then test with the Serial Monitor: type B45 and press Enter.

  5. UNO

    Close the Serial Monitor

    The port must be free.

  6. UNO

    Run arm_control.py

    Check the port name.

  7. UNO

    Find your axis numbers

    Print them and adjust.

  8. UNO

    Tune the limits

    So the arm never hits its frame.

In short

How do you build a control a robot arm from a computer game controller?

A Python script reads a USB game controller and sends joint commands over serial to an Arduino driving a four-servo arm. You need Arduino Uno R3, Robot arm kit 4-DOF (acrylic, SG90), Battery holder 4 × AA with switch, AA alkaline battery × 4, Electrolytic capacitor 1000 µF and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 150 minutes.

Level
Intermediate
Time
About 150 min
Parts
6

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Servo base signalUno D3Rotates the arm
Servo shoulder signalUno D5Lifts the arm
Servo elbow signalUno D6Bends the arm
Servo gripper signalUno D9Opens and closes the claw
All servo red wires4 AA holder plus (about 6 V)Servo power, never from the Uno 5V pin
All servo brown wires4 AA holder minusServo ground
4 AA holder minusUno GNDCommon ground, essential
1000 uF capacitorAcross the servo power rails, stripe to minusAbsorbs current spikes

Code

The PC reads a USB game controller with pygame and sends a short text line such as B90 S45 E100 G30 to the Arduino twenty times a second. The Arduino picks the numbers out of the line, limits each one to a safe range, and moves each servo one degree every fifteen milliseconds toward its target, so the arm moves smoothly and never jerks even if you flick a stick. Servos draw large currents when they start, so power them from four AA cells in the holder (about 6 V) and never from the Uno's 5 V pin, which can reset or be damaged. Connect the holder's minus to the Uno's GND so the control signals share a reference, and put the 1000 microfarad capacitor across the servo supply. Axis and button numbers vary between controllers, so add a print line to see which axis moves when you push each stick, then adjust the numbers in the script. Assemble the arm kit with the servos centred at 90 degrees first (command all joints to 90, then fit the arms) and keep fingers clear of the gripper. Keep the joint limits conservative so the arm cannot push against its own frame. Setup: Arduino IDE, board "Arduino Uno". On the PC install Python 3 with pygame and pyserial. Close the Arduino Serial Monitor before you run the script, because only one program can use the port.

Runs on a virtual Arduino Uno in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// ===== File 1: arm.ino for the Arduino Uno =====
#include <Servo.h>

Servo s[4];
const uint8_t PINS[4] = {3, 5, 6, 9};
const char KEYS[4] = {'B', 'S', 'E', 'G'};          // base, shoulder, elbow, gripper
const int LO[4] = {0, 20, 20, 10};                   // safe limits for every joint
const int HI[4] = {180, 160, 160, 90};
int target[4] = {90, 90, 90, 45};
int cur[4]    = {90, 90, 90, 45};
String line;
unsigned long lastStep = 0;

void parseLine(String l) {                           // example line: B90 S45 E100 G30
  for (int i = 0; i < 4; i++) {
    int p = l.indexOf(KEYS[i]);
    if (p >= 0) target[i] = constrain(l.substring(p + 1).toInt(), LO[i], HI[i]);
  }
}

void setup() {
  Serial.begin(115200);
  for (int i = 0; i < 4; i++) { s[i].attach(PINS[i]); s[i].write(cur[i]); }
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();
    if (c == '\n') { parseLine(line); line = ""; }
    else if (line.length() < 40) line += c;
  }
  if (millis() - lastStep > 15) {                    // move each joint one degree per 15 ms at most
    lastStep = millis();
    for (int i = 0; i < 4; i++) {
      if (cur[i] < target[i]) cur[i]++;
      else if (cur[i] > target[i]) cur[i]--;
      s[i].write(cur[i]);
    }
  }
}

/*
# ===== File 2: arm_control.py for the PC (pip install pygame pyserial) =====
import time
import serial
import pygame

PORT = "/dev/ttyACM0"          # on Windows use something like "COM3"
HOME = {"B": 90, "S": 90, "E": 90, "G": 45}
LIM = {"B": (0, 180), "S": (20, 160), "E": (20, 160), "G": (10, 90)}
ang = dict(HOME)

def dead(v):
    return 0 if abs(v) < 0.15 else v      # ignore stick drift near the centre

pygame.init()
pygame.joystick.init()
pad = pygame.joystick.Joystick(0)
pad.init()
ser = serial.Serial(PORT, 115200, timeout=0.1)
time.sleep(2)                              # the Uno restarts when the port opens
clock = pygame.time.Clock()

while True:
    pygame.event.pump()
    ang["B"] += dead(pad.get_axis(0)) * 2          # axis numbers differ between controllers, print them to check
    ang["S"] += -dead(pad.get_axis(1)) * 2
    ang["E"] += -dead(pad.get_axis(3)) * 2
    ang["G"] += (pad.get_button(5) - pad.get_button(4)) * 2    # one shoulder button closes, the other opens
    if pad.get_button(0):                           # the first button sends the arm home
        ang = dict(HOME)
    for k in ang:
        ang[k] = max(LIM[k][0], min(LIM[k][1], ang[k]))
    cmd = "B%d S%d E%d G%d\n" % (ang["B"], ang["S"], ang["E"], ang["G"])
    ser.write(cmd.encode())
    clock.tick(20)                                  # 20 commands a second
*/

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Type B45 in the Serial MonitorBase turns smoothly to 45 degrees
Push the left stick sidewaysBase rotates
Press the home buttonArm returns to centre
Push a stick fully for a few secondsJoint stops at its limit
Unplug the controllerScript ends and the arm holds its last position

If it does not work

Servos jitter or the Uno resets

Servo power from the Uno; use the AA holder, shared GND, and the capacitor.

Port busy error

Close the Serial Monitor.

No controller found

Plug it in before starting; try pygame.joystick.get_count().

Wrong stick moves a joint

Axis numbers differ; print them and change the code.

Arm slams to one end

Start with all joints commanded to 90 before fitting the arms.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the control a robot arm from a computer game controller?

Arduino Uno R3, Robot arm kit 4-DOF (acrylic, SG90), Battery holder 4 × AA with switch, AA alkaline battery × 4, Electrolytic capacitor 1000 µF and Jumper wires male to male (40 pcs, 20 cm).

How long does the control a robot arm from a computer game controller take to build?

About 150 minutes for a intermediate maker, in 8 steps.

How do I know the control a robot arm from a computer game controller works?

Type B45 in the Serial Monitor: Base turns smoothly to 45 degrees. Push the left stick sideways: Base rotates. Press the home button: Arm returns to centre.

Servos jitter or the Uno resets: what should I check?

Servo power from the Uno; use the AA holder, shared GND, and the capacitor.

Port busy error: what should I check?

Close the Serial Monitor.

No controller found: what should I check?

Plug it in before starting; try pygame.joystick.get_count().

Wrong stick moves a joint: what should I check?

Axis numbers differ; print them and change the code.