Showing posts with label PS2 controller. Show all posts
Showing posts with label PS2 controller. Show all posts

Sunday, 4 December 2011

Interfacing PS2 Controller with Arduino

    In the previous post PS2 Controller Outline, the pin configuration, specifications and working of different buttons of PS2 controller are explained. In this post we interface the controller with an Arduino development board and test a sample program. We seperate this into two sections; the hardware and software sections. The hardware section is pretty much straight forward which involves connecting controller wires to arduino pins. In the software section, we use the PS2X class in "PS2 controller Arduino Library v1.4" provided by billporter.

Hardware Section:

    Out of 9 wires from the PS2 controller, we here use only 6 pins. They are Vcc(red), gnd(black), clock(blue), command(orange), attention(yellow), data(brown).



PS2 Controller Pins Arduino Pins
Vcc 3V3 pin of on-board FTDI chip
gnd digital ground pin
*clock pin 13
*command pin 11
*attention pin 10
**data pin 12






    * Note1: clk, cmd, att, data can be connected to any of the digital pins and program can be modified accordingly.

  ** Note2: The data pin should be connected as shown in the figure

Software Section: 
    The library is available for download at billporter. In the PS2X class, all the button states are defined as constants as given below:


Some of the important functions defined are given below:

1. config_gamepad(clock,command,attention,data,Pressures?,Rumble?)
                 This function configures the PS2 controller and returns a corresponding result in byte format. The pins to which clk, cmd, att and data are connected should be entered in this function for initial setup. Pressure and Rumble are boolean values which can be enabled or disabled.

2. read_gamepad()
                This function is called to get new values from the controller. This should be called atleast once in a second. It can also be used to set vibration strength of the vibrating motor.

3. Button(Button_Constant)
                This function returns a boolean value depending on whether a button is pressed or not. It takes a button constant as argument.

4. Analog(Button_Constant)
                This function takes Button_Constant as argument and returns a byte which represents the analog value of the button (ie) the position in case of stick and the pressure with which the button is pressed in case of pressure button.

5. ButtonPressed(Button_Constant)
                It returns a boolean value based on whether a button is just pressed or not.

6. ButtonReleased(Button_Constant)
               It returns a boolean value based on whether a button is just released or not.


7. NewButtonState(Button_constant)
                             It returns a boolean value based on change in button state.


Sample Code:
    Having discussed the software and hardware sections, lets now examine the example code given in the library itself. Click here to view the sketch "PS2X_Example".


ALGORITHM:
  • Include PS2X_lib.h
  • Create an instance for PS2X class

SETUP:

  • Set baud rate
  • Configure the game pad
  • Identify the controller type 
  • Display results in Serial Monitor

LOOP:

  • Read game pad ( read_gamepad( ) )
  • Check all the button states 
  • If any button is pressed, display it in serial monitor
  • If L1 or R1 is pressed, display the analog stick values in serial monitor
  • Call a delay of 50 milliseconds.
Screenshot of serial monitor:



Wednesday, 21 September 2011

PS2 Controller - Simple Outline





The PlayStation brand is a series of video game consoles created and developed by Sony Computer Entertainment. In this post we concentrate on how the (playstation 2) PS2 controller functions. The PS2 controller consists of 4 main select buttons ‘Triangle’, ‘Circle’, ‘Cross’ and square, start, select, a D-pad, shoulder buttons ( L1, L2, R1, R2 ) and 2 analog sticks with Dual shock feature. All the buttons are pressure-sensitive except start and select.


How is pressure-sensitivity achieved?


Whenever a button is pressed, a small disc attached to the bottom of the button is pushed against the conductive strip mounted on the circuit board of the controller. With increase in pressure applied over the button, area of the disc touching the strip is increased and so there is an increase in conductivity leading to increase in flow of current. Thus, the buttons are made pressure-sensitive.


What is dual shock feature? How is it achieved?

         The Dual Shock feature provides a tactile stimulation to certain actions in a game. It makes us feel the vibration during the some critical situations in the game. It is achieved by means of 2 dc vibration motors ( left and right ).


        To the shaft of each motor, an unbalanced weight is attached. When the motor rotates, due to imbalance of weight, it wobbles which is experienced as vibration by the user.


How do analog sticks work?
        Beneath the analog stick, 2 potentiometers (bc103, 10K) are mounted at right angles to each other, one for vertical and the other for horizontal movements. 





          Whenever the joystick is moved in horizontal or vertical direction, there is a corresponding change in the resistance of corresponding POT. Due to this variation in resistance, there is a change in current flowing through it.

Schematic:



Pin Out:






Pin Name Description
1 DATA Data
2 CMD Command
3 +7.6V Dualshock and Dualshock 2 uses this for powering vibro-motors. Voltage on PS2 is 7.6V(standard)/9V
4 GND Ground
5 VCC Vcc
6 ATT ATT select
7 CLK Clock
8 N/C No connection
9 ACK Acknowledge



Pin Details:

  • Data:  This signal is an 8 bit serial transmission synchronous to the falling edge of clock
  • Command: This signal is the counter part of DATA. It is again an 8 bit serial transmission on the falling edge of clock.
  • ATT(Select/DTR/Command): ATT is used to get the attention of the controller.This signal will go low for the duration of a transmission.
  • Clock: (from PSX to Controller) Used to keep units in sync.
  • ACK (from Controller to PSX): This signal should go low for at least one clock period after each 8 bits are sent and ATT is still held low.



More Info on PlayStation:


Coming up next:

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More