Using Cytron PS2 Shield with Arduino Edubot

Introduction

Have you tried out using Arduino Edubot for line following? The Arduino Edubot worked in autonomous mode where it will keep on following the line. However, what if you are interested to control the Arduino Edubot manually? Fret not, you can easily control Arduino Edubot with the help of Cytron PS2 Shield. This shield allows you to integrate the standard PS2 controller into your Arduino Edubot or even other projects.

Hardware Required


Aluminium Robot Chassis Complete SetCT Uno (Arduino UNO compatible)
2A Motor Driver ShieldCytron PS2 Shield
PS2 Controller3.7V 18650 Li-ion Battery
2×18650 Battery HolderDC Jack(Male) to DG126 Converter

Hardware Introduction

In this project, Cytron PS2 Shield will be used to create an interface between the CT-UNO and the PS2 Controller. Cytron PS2 Shield enables the user to utilize all the buttons from the PS2 controller including the two joystick. By using this shield, decoding the PS2 controller will be as easy as reading UART data. Furthermore, this shield allows the user to use either the hardware serial from Arduino itself (Pin 0 and 1) or software serial (pin 2,3,8,9,10 and 11). 

Did I mention that decoding data from PS2 controller is also an easy job. Here, we provide you with the Cytron PS2 Shield library, which helps you to read and write to the controller easily. You can get the library from here.

In this article, I am going to divide it into two parts. The first part explains on how to use the Cytron PS2 Shield library and decodes all the button from PS2 controller. In the subsequent part, I will be explaining how you can use the PS2 to control you Arduino Edubot.

Part 1: Using Cytron PS2 Shield Library

Before I start explaining about the library, make sure you have downloaded the library. Go to here, then download the zip file of library. Make sure you install the library into you Arduino IDE. While working with the PS2 shield, it is a good practice to refer the User’s Manual to understand more.

Circuit Setup:

For the first part, the setup is quite simple, just attach the Cytron PS2 shield onto the CT-UNO. Next, connect your PS2 controller to the PS2 connector of the shield.

2015-07-06 14.02.46

1. To read the buttons from the PS2 controller

For this simple example, I have connected a LED on pin 13 of Arduino as the indicator.

2015-07-06 14.08.38

To read the digital buttons of PS2 controller, you can use the function:

 readButton(button);

 

The button is the key you are interested to read from the PS2 controller. The list of available buttons are shown in table below:

PS2_SELECTPS2_JOYSTICK_LEFTPS2_JOYSTICK_RIGHTPS2_START
PS2_LEFTPS2_RIGHTPS2_UPPS2_DOWN
PS2_LEFT1PS2_LEFT2PS2_RIGHT1PS2_RIGHT2
PS2_TRIANGLEPS2_CIRCLEPS2_CROSSPS2_SQUARE
joystick
Button Label

Below is the code for this example:

#include <SoftwareSerial.h>
#include <Cytron_PS2Shield.h>

Cytron_PS2Shield ps2(2,3);

#define led 13

void setup()
{
ps2.begin(9600);

pinMode(led,OUTPUT);
digitalWrite(led,LOW);
}

void loop()
{
if(ps2.readButton(PS2_SELECT)==0)
{
digitalWrite(led,HIGH);
}
else
{
digitalWrite(led,LOW);
}
}


In this example, whenever you pressed the SELECT button on the PS2 controller, it will light up the LED on pin 13.

2015-07-06 14.09.24

2015-07-06 14.09.22

2. To read the analog joystick from PS2 controller

For this example, I will show you how to read the analog value from the joystick. For your information, the joystick is simply a 2 axispotentiometer inside. Therefore, the shield will decode it and return analog value ranged between 0 to 255.

To read the analog joystick value, you can use the function similar to previous example, except a little changes on the button variable.

readButton(button)

 

The available joystick button are:

PS2_JOYSTICK_LEFT_X_AXISPS2_JOYSTICK_LEFT_Y_AXISPS2_JOYSTICK_RIGHT_X_AXIS
PS2_JOYSTICK_RIGHT_Y_AXISPS2_JOYSTICK_RIGHT_UPPS2_JOYSTICK_RIGHT_DOWN
PS2_JOYSTICK_RIGHT_LEFTPS2_JOYSTICK_RIGHT_RIGHTPS2_JOYSTICK_LEFT_UP
PS2_JOYSTICK_LEFT_DOWNPS2_JOYSTICK_LEFT_LEFTPS2_JOYSTICK_LEFT_RIGHT

The analog output format of joystick is illustrated by the figure below:

joystick_output_format

Below is the code for this example:

#include <SoftwareSerial.h>
#include <Cytron_PS2Shield.h>

Cytron_PS2Shield ps2(2,3);

void setup()
{
ps2.begin(9600);
Serial.begin(9600);
}

void loop()
{
Serial.print("X Axis val: ");
Serial.println(ps2.readButton(PS2_JOYSTICK_LEFT_X_AXIS));
delay(50);
}

The results can be viewed using the Arduino’s Serial Monitor. When the left joystick is push towards the x axis, the output value will change accordingly.

Capture

3. To control the vibrator motor from PS2 controller

In the last example, I will show you how you can control the vibrator motor in the PS2 controller using your Arduino. 

* I need to highlight, not all PS2 controller comes with vibrator motor, so don be surprise if this code cannot works.

To control vibrator motor, you can use this function:

vibrate(motor,value);

Below is the code for this example:

#include <SoftwareSerial.h>
#include <Cytron_PS2Shield.h>

Cytron_PS2Shield ps2(2,3);

void setup()
{
ps2.begin(9600);
}

void loop()
{
/* Function: vibrate(motor, value);
motor = 0; value = 0 to 255 //bigger vibrator
motor = 1; value = 0 or 1 //smaller vibrator
*/

ps2.vibrate(1,1);
delay(500);
ps2.vibrate(1,0);
delay(500);
}

Part 2: Integrating Cytron PS2 Shield with Arduino Edubot

For this part, we will combine both the PS2 Shield and the Arduino Edubot. The PS2 controller will be used to control the movement of the Edubot. 

Project Setup:

1. First, attach the CT-UNO to the aluminium chassis.

2015-07-06 15.04.43

2. Next, add the 2A Motor Driver Shield onto the CT-Uno.

2015-07-06 15.05.47

3. Connect the motor connection to the motor shield. Motor 1 for right wheel, motor 2 for left wheel.

2015-07-06 15.05.50

4. Make sure the selection jumper are connected for the signed magnitude option.

2015-07-06 15.05.54

5. After that, stack the PS2 shield on top of the motor shield.

2015-07-06 15.06.22

6. Make sure the selection jumper are connected for 9.6k Baud rate, D2 for Rx and D3 for Tx.

2015-07-06 15.06.27

7. Connect the PS2 controller to the PS2 shield.

2015-07-06 15.06.38

8. Finally, insert the DC jack to power up the robot.

2015-07-06 15.06.48

This is how the overall robot looks like.

2015-07-06 15.10.32

After setting up the hardware, let us move on to the software part of the Arduino Edubot. Here I have prepared the example sketch, PS2_Edubot_Joystick which you can used for this project. This sketch will read the buttons (TRIANGLE, SQUARE, CIRCLE, CROSS) and the right joystick to control the movement. The demo of this project is shown in the video below.

If you have any questions regarding this project, feel free to discuss in our Technical Forum. Good luck building your PS2 controlled Arduino Edubot.

Buy

, , , , ,

Related Post

Getting Started with Colour Sensor Module (SN-COLOUR-MOD)

Getting Started with Tilt Sensor (SN-TILT)

Getting Started with Vibration Sensor Module (Normally Off) (SN-VIBRATE-MOD)

Getting Started with IR Distance Click

2 thoughts on “Using Cytron PS2 Shield with Arduino Edubot

  1. Hi there,

    I did everything as shown in the example code to test out the analog joystick reading but all it does is print out “X Axis Val: 255” and it just keeps on looping that printout. Any ideas what when wrong? Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *