How to control an Arduino with a wireless PS2 controller

  • The PS2 controller is an economical and ergonomic option for controlling robots.
  • You need a logic level converter to connect the PS2 controller to Arduino.
  • The PS2X library makes it easy to integrate the PS2 controller with Arduino.
  • The controller has 14 buttons and two analog sticks for precise control.

books about arduino

The interaction between Arduino and a PlayStation 2 controller is incredibly interesting for robotics and automation projects. The PS2 wireless controller is a versatile and very inexpensive controller, capable of offering a variety of analog and digital inputs. Although it may seem like a complex task at first, the integration of these two systems is quite affordable thanks to different libraries and tools that have already been developed to facilitate the process.

A wireless PS2 controller is an ergonomic and inexpensive option for controlling robots, 4×4 vehicles, or any type of project where smooth and convenient remote control is required. Using this controller with Arduino is a solution that many hobbyists choose, as it is not only inexpensive, but provides a highly precise control experience thanks to its two analog sticks and a set of 14 buttons.

Advantages of using a PS2 controller with Arduino

One of the main advantages of the PS2 controller is that it's wireless , allowing you to give your projects mobility without the limitations of cables. Furthermore, it's very comfortable to use thanks to its ergonomic design, optimized for long gaming sessions, making it perfect for controlling robots or other devices for extended periods without affecting accuracy or responsiveness.

Another important aspect is the cost. A clone PS2 controller can be purchased for less than 10€. For this price, you get a controller with a large number of buttons and analog inputs, making it a tremendously attractive option for remote control projects.

To integrate this controller with Arduino, in addition to the controller itself and the receiver, it is common to use a logic level converter that allows adjusting the 3,3V signals from the PS2 controller to the 5V that Arduino works with in most of its versions.

Necessary materials

  • Wireless PS2 ControllerA clone of this remote costs between €9 and €10.
  • wireless receiver for the remote control, usually included in the package.
  • Logic level converterThis is necessary since Arduino operates at 5V and the PS2 controller works with 3,3V signals.
  • Arduino. You can use any model, such as the Arduino Uno, Mega or Mini.
  • Protoboard and cables to make the connections between the receiver and the Arduino.

Connections and assembly

The wireless receiver that comes with the PS2 controller is the key component for communication between the two. This component connects to the Arduino via a series of pins, which vary depending on the receiver, but generally include power pins (5V and GND), data pins, clock pins , and other auxiliary pins such as the attention or vibration pin.

An important recommendation is not to rely on the colour code of the cables, as this can vary depending on the manufacturer. It is best to use a multimeter to check the function of each one of them, which will avoid possible errors in the connection.

Once you have correctly identified the pins, you can use a breadboard to solder or connect the different signals to the Arduino, or if you prefer to assemble everything compactly, you can also solder the wires directly to the receiver, always making sure that the connections are correct.

It is very important to use a logic level converter because, as mentioned, the PS2 controller operates at 3,3V, while most Arduino boards operate at 5V. This converter will allow both systems to "talk" to each other without causing damage.

Configuration and library for the PS2 controller

To control a PS2 controller with Arduino, there's a fundamental library called PS2X_lib , created by Bill Porter, which greatly simplifies using this type of controller. This software allows you to manage all the signals the controller sends through its wireless receiver.

Installing this library is very simple and you can find it on GitHub . Once downloaded and installed in your Arduino development environment, you can start working with it.

The next step is to configure the pins in the Arduino code. You must ensure you correctly define the clock, command, data , and attention pins that correspond to the inputs and outputs of your Arduino . This is vital, as incorrect pin assignment can cause the controller to not respond correctly.

The following code snippet shows how to configure these pins:

#include 
PS2X ps2x; // crear la clase para el mando PS2
int error; // variable para errores
#define PS2_CLK 34
#define PS2_CMD 24
#define PS2_ATT 32
#define PS2_DAT 22
// Configuramos los pines para el mando
error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_ATT, PS2_DAT, true, true);

Once the pins are configured, you can begin reading the signals sent by the controller through the code. The library provides specific methods for checking if a button has been pressed, as well as for obtaining the analog values ​​of the controller's joysticks.

Button reading

The PS2 controller has up to 14 buttons , distributed between the front and back. To read the front and back buttons, you can use the following code:

if (ps2x.Button(PSB_PAD_UP)) {
   Serial.println("Arriba");
} else if (ps2x.Button(PSB_PAD_DOWN)) {
  Serial.println("Abajo");
} else if (ps2x.Button(PSB_PAD_LEFT)) {
  Serial.println("Izquierda");
} else if (ps2x.Button(PSB_PAD_RIGHT)) {
  Serial.println("Derecha");
}

In addition to the directional buttons, you can read the activation of action buttons such as Circle, X, Square, and Triangle , in a similar way.

Reading Joysticks

The analog sticks on the PS2 controller provide continuous values ​​that you can use to move motors or control the direction and speed of a robot, for example. The values ​​range from 0 to 255, with 127 being the center value when the sticks are at rest.

int LX = ps2x.Analog(PSS_LX);
int LY = ps2x.Analog(PSS_LY);
int RX = ps2x.Analog(PSS_RX);
int RY = ps2x.Analog(PSS_RY);
Serial.print("Stick Values: ");
Serial.print(LX);Serial.print(",");
Serial.print(LY);Serial.print(",");
Serial.print(RX);Serial.print(",");
Serial.println(RY);

With these values ​​you can control the speed of the motors depending on how much pressure is applied to the control levers.

Practical examples

Controlling a 4x4 rover robot with a PS2 controller is a classic example of the applications of this type of controller. This setup becomes especially easy with an Arduino Mega , as it offers more pins and more memory, allowing you to manage multiple motors and sensors simultaneously.

The following code shows a basic example of how to control a rover using the directional buttons on the controller:

void loop() {
 ps2x.read_gamepad();
 if (ps2x.Button(PSB_PAD_UP)) {
  avance();
 } else if (ps2x.Button(PSB_PAD_DOWN)) {
  retroceso();
 } else if (ps2x.Button(PSB_PAD_LEFT)) {
  giroIzquierda();
 } else if (ps2x.Button(PSB_PAD_RIGHT)) {
  giroDerecha();
} else {
  paro();
 }
}

This is a simple example of how the different buttons on the controller can control the movement of the vehicle, but the use of joysticks would allow for smoother and more precise movements, controlling both the direction and speed of the robot.

With proper integration and configuration, this controller becomes a powerful tool for controlling almost any Arduino-based electronic project.

Combining a PlayStation 2 controller with Arduino opens up a wide range of possibilities for controlling robot projects, automated systems, or even simpler systems like robotic arms. The large number of buttons and the precision of the joysticks make this controller an excellent choice for those looking to add remote control to their projects with an ergonomic and familiar interface.


Add as preferred source in Google