Dual-axis Joystick Module
The dual-axis joystick module uses the metal button potentiometer from PS2 game controllers. This module features two analog output interfaces for the X and Y axes, which represent the positional offsets on each axis. The outputs are analog signals. The module also has one digital output that indicates whether the button on the Z axis is pressed, which functions as a digital switch. With this joystick module, users can easily control objects in 2D space. It is suitable for programming controllers, connecting to sensor expansion boards, and creating innovative remote interactive projects.
Module Source
Specifications
Hardware Connection
- The X-axis analog output is connected to the A0 analog input pin of the development board.
- The Y-axis analog output is connected to the A1 analog input pin of the development board.
- The button output is connected to the digital input pin 2 of the development board.
- The power wire (+VCC) is connected to 5V.
- The ground wire (GND) is connected to GND.
Usage Method
In the code, it implements reading the analog values of the X and Y axes, as well as checking whether the button is pressed.
c
/******************************************************************************
* Test Hardware: LCSC ColorEasyDuino Development Board
* Version Number: V1.0
* Modified By: www.lckfb.com
* Modification Date: April 17, 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.
******************************************************************************/
// 定义连接摇杆模块的引脚
int joyX = A0; // X轴连接到模拟引脚A0
int joyY = A1; // Y轴连接到模拟引脚A1
int buttonPin = 2; // 按键连接到数字引脚2
void setup() {
Serial.begin(9600); // 启动串行通信
pinMode(buttonPin, INPUT_PULLUP); // 设置按键引脚为输入,并启用内部上拉电阻
}
void loop() {
int xValue = analogRead(joyX); // 读取X轴的模拟值
int yValue = analogRead(joyY); // 读取Y轴的模拟值
int buttonState = digitalRead(buttonPin); // 读取按键状态
// 打印出值
Serial.print("X axis: ");
Serial.print(xValue);
Serial.print("\tY axis: ");
Serial.print(yValue);
Serial.print("\tButton: ");
Serial.println(buttonState);
delay(100); // 简单的延迟,使得串行输出可读
}
1
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
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