PS2 Wireless Controller Module
The PS2 controller is compatible with Sony's PlayStation 2 gaming console. Sony's PSX series gaming consoles have been very popular worldwide. At some point, people started hacking the communication protocol of the PS2 controller, making it possible to connect the controller to other devices for remote control use, such as controlling the robots we're familiar with. The standout feature of this controller is its high cost-performance, with a wide range of buttons, making it convenient for expansion into other applications.
Module Source
Purchase Link:
https://item.taobao.com/item.htm?spm=a1z10.3-c-s.w4002-19589090137.10.257136b4kQus88&id=559076487258
Baidu Netdisk Download Link:
https://pan.baidu.com/s/15WcgkOqKLfXpQpZ4qv3rJA
Password: 3qdg
Specifications
Controller Operating Voltage: Powered by two AA (7) batteries
Receiver Operating Voltage: 3~5V Remote Control Distance: Approximately 15 meters
Control Method: Serial Communication Pin Count: 6 Pins
Instructions for Use
The PS2 controller consists of two parts: the controller and the receiver. The controller is primarily responsible for sending button press information. The receiver is connected to the microcontroller (also called the host, which can be directly used on the PS2 gaming console). It receives the information sent by the controller and transmits it to the microcontroller. The microcontroller can also send commands to the controller via the receiver to configure the controller's sending mode.Special Note: Due to different batches or manufacturers, the appearance of the controller and receiver may vary (the receiver always has an indicator light, but one type of receiver has a power light while the other does not). However, the pin definition of the receiver is the same, the decoding method is the same, and the usage is identical.
Pin Description
Pin | Description |
---|---|
DI/DAT | Signal direction: from controller to host. This signal is 8-bit serial data, synchronized with the clock's falling edge. The signal is read during the clock's high-to-low transition. |
DO/CMD | Signal direction: from host to controller. This signal is opposite to DI. It is 8-bit serial data, synchronized with the clock's falling edge. |
GND | Ground (Power ground). |
VDD | Power supply for the receiver, voltage range 3~5V. |
CS/SEL | Used to trigger the controller signal. It is low during communication. |
CLK | Clock signal, emitted by the host to keep the data synchronized. |
Control Instructions
The clock frequency is 250KHz (4us). If data reception is unstable, the frequency can be increased appropriately. During communication, after a data transmission is complete, the CS line will change from low to high. It is not that the CS line changes after every byte of communication; the CS line stays low throughout the communication. Data transmission and reception are done simultaneously on the clock’s falling edge.
When the microcontroller wants to read the controller data or send a command to the controller, it will pull the CS line low and send a command "0x01". The controller will reply with its ID: "0x41" (green light mode) or "0x73" (red light mode). While the controller sends its ID, the microcontroller will transmit "0x42" to request data. Then, the controller sends "0x5A" to inform the microcontroller that "data is coming."
A communication cycle consists of 9 bytes (8 bits), and these data are transmitted bit by bit in sequence.
idle: The data line is idle, meaning no data is being transmitted.
Example: When a button is pressed, the corresponding bit is "0", and all other bits are "1". For example, when the "SELECT" button is pressed, Data[] = 11111110.
In red light mode: The left and right joystick sends analog values, ranging from 0x00 to 0xFF, and the button values for L3 and R3 are active.
In green light mode: The analog values for the left and right joysticks are not active. When pushed to their extremes, the corresponding values for UP, RIGHT, DOWN, LEFT, △, 〇, X, and □ are sent. However, the L3 and R3 buttons are inactive.
Hardware Connection
PS2 Receiver Development Board
Data (Data) D12
Command (Cmd) D11
Attention (Att) D10
Clock (Clk) D13
Power (VCC) 3.3V
Ground (GND) GND
2
3
4
5
6
7
Usage Method
Prepare the Files
Place the following two files in the project folder at the same level as the .ino file, so that the .ino file can call them.
Download Files
📌 Download Center (Click the link)
📌 In the Download Center -> Module Porting Guide [Non Porting Code], find the Chapter Compression Package.
Enter the code:
/******************************************************************************
* Test Hardware: LCSC ColorEasyDuino Development Board
* Version Number: V1.0
* Modified By: www.lckfb.com
* Modification Date: April 11, 2024
* Function Overview:
*****************************************************************************
* Open-source development board hardware and software information and related projects hardware and software information on official website
* Development board official website: www.lckfb.com
* Technical support resident forum, any technical problems are welcome at any time to exchange learning
* LCSC Forum: club.szlcsc.com
* Follow our Bilibili account: [立创开发板], stay toned to our latest news!
* We focus on cultivating Chinese engineers rather than profiting from board sales.
******************************************************************************/
#include "PS2X_lib.h" // 包含PS2X库
// 定义PS2控制器对象的引脚连接
PS2X ps2x;
const int PS2_DAT = 12; // 数据引脚
const int PS2_CMD = 11; // 命令引脚
const int PS2_SEL = 10; // 选择引脚
const int PS2_CLK = 13; // 时钟引脚
void setup() {
Serial.begin(9600);
// 初始化手柄,并设置模式
if (ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT) == 0) {
Serial.println("No PS2 controller found, check wiring");
while(1); // 循环结束,无法找到手柄
}
Serial.println("PS2 controller found!");
}
void loop() {
// 读取手柄状态
ps2x.read_gamepad();
// 检查是否按下了三角按钮
if (ps2x.Button(PSB_TRIANGLE)) {
Serial.println("Triangle pressed");
}
// 检查是否按下了圆形按钮
if (ps2x.Button(PSB_CIRCLE)) {
Serial.println("Circle pressed");
}
// 检查是否按下了圆形按钮
if (ps2x.Button(PSB_CIRCLE)) {
Serial.println("Circle pressed");
}
// 也可以检测其他按钮...
// // 检测右摇杆位置
// int leftStickX = ps2x.Analog(PSS_LX);
// int leftStickY = ps2x.Analog(PSS_LY);
// Serial.print("Left Stick X: ");
// Serial.println(leftStickX);
// Serial.print("Left Stick Y: ");
// Serial.println(leftStickY);
// // 检测右摇杆位置
// int rightStickX = ps2x.Analog(PSS_RX);
// int rightStickY = ps2x.Analog(PSS_RX);
// Serial.print("Right Stick X: ");
// Serial.println(rightStickX);
// Serial.print("Right Stick Y: ");
// Serial.println(rightStickY);
delay(50); // 延时以减少读取频率
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
The button detection settings can be selected as follows:
Usage Testing
Note: the program should be started only after the controller is connected; otherwise, modify the part of the code that handles the detection module's infinite loop.