Introduction: Control Arduino Using Joystick

Picture of Control Arduino Using Joystick

after about a month of have my arduino I got bored so I was surfing the web and found this cool way of controlling arduino

if you do any thing illegal i am not held responsable

Step 1: Things Needed

Picture of Things Needed

1.arduino (duh)
2. computer (if u are reading this you have one unless it is not yours _)
3.usb ports(most computers have these)
4.time(took me 5 hours to figure it out & to cofigure to my servo)

Step 2: Software You Need

Picture of Software You Need

arduino programer
pyserial(plug in for python)
pygame(plug in for python)
joystick drivers

Step 3: Programs

Picture of Programs
i have added the programs you need


  • JoystickSerialServo
  • --------------
  • Servo control with a PC and Joystick

/** Adjust these values for your servo and setup, if necessary **/
int servoPin = 3; // control pin for servo motor
int minPulse = 600; // minimum servo position (to find use google)
int maxPulse = 2400; // maximum servo position (to find use google)
int refreshTime = 20; // time (ms) between pulses (50Hz)

/** The Arduino will calculate these values for you **/
int centerServo; // center servo position
int pulseWidth; // servo pulse width
int servoPosition; // commanded servo position, 0-180 degrees
int pulseRange; // max pulse - min pulse
long lastPulse = 0; // recorded time (ms) of the last pulse

void setup() {
pinMode(servoPin, OUTPUT); // Set servo pin as an output pin
pulseRange = maxPulse - minPulse;
centerServo = maxPulse - ((pulseRange)/2);
pulseWidth = centerServo; // Give the servo a starting point (or it floats)

void loop() {
// wait for serial input
if (Serial.available() > 0) {
// read the incoming byte:
servoPosition =;

// compute pulseWidth from servoPosition
pulseWidth = minPulse + (servoPosition * (pulseRange/180));

// stop servo pulse at min and max
if (pulseWidth > maxPulse) { pulseWidth = maxPulse; }
if (pulseWidth < minPulse) { pulseWidth = minPulse; }

// debug

// pulse the servo every 20 ms (refreshTime) with current pulseWidth
// this will hold the servo's position if unchanged, or move it if changed
if (millis() - lastPulse >= refreshTime) {
digitalWrite(servoPin, HIGH); // start the pulse
delayMicroseconds(pulseWidth); // pulse width
digitalWrite(servoPin, LOW); // stop the pulse
lastPulse = millis(); // save the time of the last pulse


#!/usr/bin/env python
# created 19 December 2007
# copyleft 2007 Brian D. Wendt
# code adapted from:
# NOTE: This script requires the following Python modules:
# pyserial -
# pygame -
# Win32 users may also need:
# pywin32 -

import serial
import pygame

# allow multiple joysticks
joy = []

# Arduino USB port address (try "COM5" on Win32)
usbport = "COM8"

# define usb serial connection to Arduino
ser = serial.Serial(usbport, 9600)

# handle joystick event
def handleJoyEvent(e):
if e.type == pygame.JOYAXISMOTION:
axis = "unknown"
if (e.dict['axis'] == 1):
axis = "X"

if (e.dict['axis'] == 0):
axis = "Y"

if (e.dict['axis'] == 2):
axis = "Throttle"

if (e.dict['axis'] == 3):
axis = "Z"

if (axis != "unknown"):
str = "Axis: %s; Value: %f" % (axis, e.dict['value'])
# uncomment to debug
#output(str, e.dict['joy'])

# Arduino joystick-servo hack
if (axis == "X"):
pos = e.dict['value']
# convert joystick position to servo increment, 0-180
move = round(pos * 90, 0)
if (move < 0):
servo = int(90 - abs(move))
servo = int(move + 90)
# convert position to ASCII character
servoPosition = chr(servo)
# and send to Arduino over serial connection
# uncomment to debug
#print servo, servoPosition

elif e.type == pygame.JOYBUTTONDOWN:
str = "Button: %d" % (e.dict['button'])
# uncomment to debug
#output(str, e.dict['joy'])
# Button 0 (trigger) to quit
if (e.dict['button'] == 0):
print "Bye!\n"

# print the joystick position
def output(line, stick):
print "Joystick: %d; %s" % (stick, line)

# wait for joystick input
def joystickControl():
while True:
e = pygame.event.wait()
if (e.type == pygame.JOYAXISMOTION or e.type == pygame.JOYBUTTONDOWN):

# main method
def main():
# initialize pygame
if not pygame.joystick.get_count():
print "\nPlease connect a joystick and run again.\n"
print "\n%d joystick(s) detected." % pygame.joystick.get_count()
for i in range(pygame.joystick.get_count()):
myjoy = pygame.joystick.Joystick(i)
print "Joystick %d: " % (i) + joy[i].get_name()
print "Depress trigger (button 0) to quit.\n"

# run joystick listener loop

# allow use as a module or standalone script
if name == "main":

Step 4: Test It _

i did not write the python script
i found it on

HAVE fun

4 servo version

Step 5: Video

this is not my video its the video from the website


Great Wight Ninja (author)2012-07-16

This would be a simple edit, but you might want to add a joystick as another "duh" thing you need.

your right. totally skipped my mind

axeman911 (author)2012-01-04

how do u upload the programs to the arduino if your using python?

the arduino ide. python just sends commands over serial com

lane29 (author)2010-12-13

nice background

bigbodysmallbrain (author)lane292010-12-25

LOL. thanks

geeklord (author)2009-05-17

That's funny, I just finished up a project almost exactly like this. I attached a joystick to an Arduino to a Servo motor! The only difference is that I dirrectly connected to the potentiometers in the joystick, I didn't use any programs or serial communication. Yours is great! I should post an 'ible for mine.

eliastorre (author)geeklord2010-01-11

Hi there!

Hey please post an instructable for that!!! That would be interesting. And to the author, very cool instructable.

geeklord (author)eliastorre2010-01-24

Yea, that was a while ago... It's reeeaaallly simple, though. The potentiometers were very easy to access  once you got the plastic case off. Then I just used a couple analogRead's and the Servo library.


cool. but i use multiple joysticks for reason i dont quite konw


ReCreate (author)2009-07-02

How could this get illegal?

if you use it to throw ninja star to kill someone

Its like if you show someone to use a computer, and then they download warez, And then they sue you when the RIAA hits them, And then you say i am not held Liable blah blah blah...Nevermind >_