Control Arduino Using Joystick





Introduction: 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

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

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

Step 3: 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



    • Pocket-Sized Contest

      Pocket-Sized Contest
    • Pro Tips Challenge

      Pro Tips Challenge
    • Paper Contest 2018

      Paper Contest 2018

    We have a be nice policy.
    Please be positive and constructive.




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

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

    the arduino ide. python just sends commands over serial com

    nice background

    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.

    Hi there!

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

    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.