You are on page 1of 6

Electronics

PHOTOGRAPHS: HAMISH TROLOVE


Taking
Charge
HOW TO USE A
PLAYSTATION 2
CONTROLLER WITH
YOUR ARDUINO
PROJECT. By Hamish Trolove

I
was designing a little box with some inputs I was hoping to use as well as are very sophisticated, well-designed
joysticks and a few buttons to control a heap of other buttons and features and robust pieces of equipment that
a project of mine one day when it far beyond what I needed. And they are ideal for using as an interface for
occurred to me that it looked familiar. were cheaper than the small handful of mechatronics projects. Even though the
I suddenly asked myself why I was components I was going to need to buy PlayStation 2 game console is now a
trying to build what was essentially to make my own. rather outdated piece of equipment, the
a game controller from scratch when There has been millions of dollars PlayStation 2 controller clones are still
there were perfectly good game poured into the development of the made and can be purchased through
controllers around that included the PlayStation controllers and so they Trade Me extraordinarily cheaply.

88 The Shed February/March 2016


Not only that, but they are also easy
to connect to an Arduino. In addition
to the two high-quality joysticks, all
the buttons are pressure sensitive
which adds even more functionality to
the device.
This article demonstrates how
you would connect a PlayStation 2
controller to an Arduino and how easily
it can be used to control various devices
thanks to the PS2Xlib Arduino Library.

Applications for PS2 if you are into a bit of hacking is to www.billporter.info/2010/06/05/


controllers extract the sockets from a PlayStation playstation-2-controller-arduino-
Where you could use a PlayStation 2 controller to USB adapter (about library-v1-0/ and the library source
controller aside from on a PlayStation $5-$10 or so through Trade Me). I code can be obtained from Github
console: have found the tabs on the back of the https://github.com/madsci1016/
• Controlling a wheeled or tracked sockets are quite fragile and it is worth Arduino-PS2X. Just click on the
vehicle's motion. embedding them in hot glue once you “Download ZIP” button on the right
• Controlling a robotic arm. have soldered some wires to it. side of the page. Once you have the zip
• Interacting with a computer. The illustration below shows the file downloaded, start your Arduino
• Controlling a pan and tilt PlayStation 2 plug and the pin labels. Interface and navigate through the
camera mount. To connect to the Arduino we only menu Sketch>Import Library>Add
• DIY radio control system by using a need to connect the data, command, Library. This will open a dialogue where
bluetooth module or radio module ground, 3.3V, attention and clock pins. you can navigate to your downloaded
to transmit the commands from The rumble motor pin only needs to be PS2X Library zip file.
the PlayStation 2 controller to connected to a 7-9V supply if rumble
another Arduino which is feedback is desired.
controlling the vehicle.
7V Rumble motor

• Then there is my own pet project


PlayStation wiring
which uses the PlayStation 2
controller as the pilot interface for
Acknowledge
Command

an ROV (aka underwater drone).


Attention

Connecting the
Data

GND

Clock
3.3V

controller
There are two options for connecting
the controller to the Arduino. The
method for masochists is to cut off the The example
connector and connect the wires into sketch included with
a row of terminal headers or directly the library is a great
solder them to some other plug. demonstration of the
The wire colours in the PlayStation capabilities of the library and
diagram may help you identify which makes use of all of the features
wire is which (the 7V-9V rumble motor of the PlayStation 2 controller. The
supply wire is often grey in colour). A demonstration that is outlined in this
better option is to retain the plug and The software side article is not quite as sophisticated but
find a suitable socket. In stark contrast Thanks to one Bill Porter an easy-to- covers the essentials needed to use
to the ease of purchasing a controller, use library is available which will allow a PlayStation 2 controller to actuate
finding sockets is much harder. It users to use a PlayStation 2 controller motors, servos and switch things on
is possible to purchase sockets and (or even a Guitar Hero controller) with and off.
breakout boards from the Robotshop an Arduino. You can find it through To use the library in an Arduino
(http://w w w.robotshop.com/en/ Bill Porter's website: “The Mind of Bill Sketch, it needs to be called using the
ps2-connector.html) in America. Porter”—http://www.billporter.info/. following commands:
Unfortunately there are no local More specifically here is the link to his #include <PS2X_lib.h>
suppliers. The cheaper alternative page with links to his library—http:// PS2X ps2x;

The Shed February/March 2016 89


In the setup part of the sketch the Now we can read which control has •  p s2x.A nalog(PSA B _CROSS)
command to let the Arduino know been used. The labels used in the PS2X is the command for measuring the
how the controller is connected to it, library are very logical. The diagram pressure applied to the “X” button,
looks like: below shows each of the names for the if pressure sensitivity has been
ps2x.config _gamepad(5,4,3,2, buttons and sticks. enabled. As you can see the “PSAB”
false, false); The buttons with the coloured shapes form of the name has been used for
where the numbers are the Arduino's can also be referred to by the names the button.
digital pins where the PlayStation 2 PSB_TRIANGLE, PSB_CIRCLE, PSB_ •  ps2x.Analog(PSS_RY) is the
controller pins are connected as below; CROSS, and PSB_SQUARE. command to obtain readings
gamepad (clock, command, To use the analogue pressure off the right analog stick in the
attention, data, pressure sensitivity on the keys the names vertical direction.
sensitivity enabled, rumble are the same except for substituting
enabled) “PSAB” for “PSB”. So to allow a pressure Bringing it together
If you wanted to use the button reading from the Green Triangle button To illustrate how this works, here is an
pressure sensitivity feature, in the the name would be PSAB_GREEN or example project. It makes use of a DC
command line above you would set this PSAB_TRIANGLE. motor, an electronic speed controller
to “true” and if you wanted the rumble The buttons can be pressed, pressed (in this case an RC car ESC), a series
motor available to provide feedback and held, or pressed with a varying of servos and an LED array designed to
then you would set this to “true” too. For pressure, so there are a number of run on 7-12V. If this were to be applied
use of the rumble feature please look at methods that can be applied to the to a real project, it could be a car with
the example sketch that comes with the buttons and joystick. The methods are: steering and two extra servos to control
PS2X library. Button Pressed, Button, and Analog. a pan and tilt camera mount. A single
Once the controller is set up, the Here are some examples of how these LED would not need the transistor, but
Arduino loops through the sketch are used: I have included a 12V LED array and
continuously. Once per loop through, •  p s2x.Button Presse d(PSB _ transistor to illustrate that other high
the Arduino needs to communicate RED) is for a simple press of the red voltage and high current devices can
with the controller to gather all circle button. be triggered by such a system.
input data. This is done with the •  p s2x.Button(PSB_PAD_DOWN) There is nothing demanding about
following command: is for the down button on the pad the sketch and it can run on any
ps2x.read_gamepad(); being pressed and held. Arduino or Arduino clone.

PSB_L2 PSB_R2
PSB_L1 Lower Button PSB_R1 Lower Button
Upper Button Upper Button

PSB_PAD_RIGHT PSB_START
4 1 2 3

PSB_PAD_UP PSB_SELECT PSB_PINK PSB_GREEN

PSB_PAD_LEFT PSB_RED
PSB_PAD_DOWN ANALOG
PSB_BLUE

PSS_LX PSS_RX

PSS_LY PSS_RY
PSB_L3 PSB_R3
Press on Left Stick Press on Right Stick

90 The Shed February/March 2016


Electronics

Circuit and component diagram

Electronic Speed
Controller (ESC)

470μF

Battery
Servo 2
Servo 1 7.2V NiCd or 11.1V LiPo
Steering Servo to Suit ESC

5.6V output from ESC

Transistor
BD681
E C B

1k Ω

7-12V LED Array

5V from Arduino
1k Ω 1k Ω
3.3V from Arduino
Data
Command
GND
3.3V DC
Attention
Clock

4 1 2 3

ANALOG

Above is a diagram of the


circuit and components. The ESC
supplies 5.6V to the servos through
its control connector. The ESC I
have used has a switch on it that needs
to be turned on before the motor
will run and before any electricity is
LED available to the servos. Other than
Light
On/Off that, the only components are the
4 1 2 3

three resistors for the connection to the


PlayStation 2 controller and the LED's
Servo 1
left ANALOG
Motor
Forward transistor. The capacitor is probably
Servo 2 Servo 2 Steering Steering
not necessary because the battery
Left Right Servo Left Servo Right
should be able to deliver a relatively
Servo 1 Motor
Right Backward smooth supply.
The controls we want to use are:
a switch for the LED array and the
two analogue joysticks to actuate
the servos and ESC. Because the ESC
“speaks” Servo, we just treat it as a
servo in the code.

The Shed February/March 2016 91


The sketch

/* Servo ServoN1; //Create servo object


PS2Controlv0.ino representing Servo 1
21 September 2015 Servo ServoN2; //Create servo object
Hamish Trolove - www.techmonkeybusiness.com representing Servo 2
Servo ESCcontrol; //Create servo object
This sketch illustrates the use of a PlayStation 2 representing ESC
Controller to actuate a series of servos and an Electronic
Speed Controller (ESC) as you might do with a vehicle. An const int LEDpin = 13; //green LED is on
LED light is used to illustrate the use of the PlayStation 2 Digital pin 13
Buttons.
volatile boolean LEDHdlts; //LED headlights
Pin assignments are: on/off toggle

3.3V output to PS2 red pin int PlyStnRStickUpDn = 0; //Value read off
5V output to 1K ohm pull up resistors for the PS2 Right Stick up/down.
PS2. int PlyStnRStickLtRt = 0; //Value read off
Pin D02 to PS2 brown pin (data) the PS2 Right Stick left/right
Pin D03 to PS2 yellow pin (attention) int PlyStnLStickUpDn = 0; //Value read off
Pin D04 to PS2 orange pin (command) the PS2 Left Stick up/down
Pin D05 to PS2 blue pin (clock) int PlyStnLStickLtRt = 0; // Value read off
the PS2 Left Stick left/right
Pin D06 to ESC Signal Pin
Pin D07 to Steering Servo Signal pin int ESCSetting = 90; //Setting for the ESC
Pin D08 to Servo 1 Signal pin (degrees).
Pin D09 to Servo 2 Signal pin int StrServoSetting = 90; //Setting for the
Steering Servo
Pin D13 to LED Transistor Base int ServoN1Setting = 90; //Setting for the
Servo 1
The ESC servo connection supplies 5.6V to int ServoN2Setting = 90; //Setting for the
the servos. Servo 2
void setup()
The coding pulls on the PS2X library developed by {
Bill Porter. ps2x.config_gamepad(5,4,3,2, false, false);
See www.billporter.info for the latest from Bill Porter //setup pins and settings: GamePad (clock,
and to download the library. command, attention, data, Pressures, Rumble)
//We have disabled the pressure sensitivity
The controls used for this sketch are: and rumble in this instance.
Right Stick - X-axis = Steering Servo left/ pinMode(LEDpin, OUTPUT); //Sets the LEDpin
right, Y-axis = ESC forward/backward to output
Left Stick - X-axis = Servo 2 left/right,
Y-axis = Servo 1 left/right LEDHdlts = false; //Sets the Headlights
Triangle = Toggle the LED to off
SteeringServo.attach(7);// attaches the
*/ Steering Servo to pin 7
ServoN1.attach(8);// attaches the Servo 1
#include <Servo.h> //For driving the ESCs to pin 8
and Servos ServoN2.attach(9);// attaches the Servo 2
#include <PS2X_lib.h> // Bill Porter's PS2 to pin 9
Library ESCcontrol.attach(6,150,2250);// attaches
the ESC to pin 6
PS2X ps2x; //The PS2 Controller Class //The ESC attachment command above also
Servo SteeringServo; //Create servo object includes the signal settings
representing SteeringServo //for the maximum and minimum that the ESC

92 The Shed February/March 2016


Electronics

will recognise. This left Stick Up and Down


//varies for different ESCs. PlyStnLStickLtRt = ps2x.Analog(PSS_LX); //
Left Stick Left and Right
//Set all ESCs and Servos to a neutral 90
degree position //Readings from PS2 Controller Sticks are
//this avoids the ESC trying to calibrate. from 0 to 255
ESCcontrol.write(90); //with the neutral being 128. The zero
SteeringServo.write(90); positions are to
ServoN1.write(90); //the left for X-axis movements and up for
ServoN2.write(90); Y-axis movements.
delay(5000); //Five second delay to allow
ESC and controller to //Variables to carry the settings for the
// fully initialise. ESCs and Servos
} //The values from the PS2 Sticks are mapped
to 0 to 180 degrees
void loop()
{ ESCSetting = map(PlyStnRStickUp
ps2x.read_gamepad(); //This needs to be Dn,-256,256,0,179);
called at least once a second StrServoSetting = map(PlyStnRStickLt
// to get data from the Rt,-256,256,0,179);
controller. ServoN1Setting = map(PlyStnLStickUp
Dn,-256,256,0,179);
if(ps2x.ButtonPressed(PSB_GREEN)) // ServoN2Setting = map(PlyStnLStickLt
Triangle pressed Rt,-256,256,0,179);
{
LEDHdlts = !LEDHdlts; //Toggle the LED //Write it to the Servos or ESCs
light flag
} ESCcontrol.write(ESCSetting);
SteeringServo.write(StrServoSetting);
//Analogue Stick readings ServoN1.write(ServoN1Setting);
PlyStnRStickUpDn = ps2x.Analog(PSS_RY); // ServoN2.write(ServoN2Setting);
Right Stick Up and Down digitalWrite(LEDpin,LEDHdlts); //Light the
PlyStnRStickLtRt = ps2x.Analog(PSS_RX); // LED based on headlights status flag
Right Stick Left and Right delay(15);
PlyStnLStickUpDn = ps2x.Analog(PSS_LY); // }

All things going well, when you neutral position. The ESCcontrol. then you should not have any problems.
connect this up and connect in the write(90); line in the Setup routine
battery, you should be able to push the sets the throttle to neutral to allow Final remark
triangle button to light the LED and the ESC to initialise. If you are using With an input device as sophisticated
move the joysticks to move the servos a Radio Control Aircraft ESC you will as the PlayStation 2 controller there
and run the motor. need to initialise it at 0 degrees. are a wealth of different mechatronics
If it does not automatically go into Depending on the quality of the projects that they can interface with.
Analog Mode (as indicated by the read Arduino you are using you may The PS2X library makes it very easy
light on the controller), press the Analog need to have the USB connected to to access the full range of functions
button. This will enable the joysticks on the computer to power the Arduino. available from the PlayStation 2
the controller and set things going. Ordinarily the Arduino should be able controller. Why reinvent the wheel
The circuit and sketch are designed to run on the 7.2v being supplied from when there are already ultra-high
with a car ESC in mind. This means a the NiCad battery. If you have an 11.1V performance mags available for cheaper
servo command of 90 degrees is the LiPo battery and ESC designed to suit it prices than a couple of components.

The Shed February/March 2016 93

You might also like