Joystick Module¶
Overview¶
A joystick is an input device consisting of a stick that pivots on a base and reports its angle or direction to the device it is controlling. Joysticks are often used to control video games and robots. A Joystick PS2 is used here.
Required Components¶
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Elite Explorer Kit |
300+ |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
---|---|
- |
|
Wiring¶
Schematic Diagram¶
This module has two analog outputs (corresponding to X,Y biaxial offsets).
In this experiment, we use the Uno board to detect the moving direction of the Joystick knob.
Code¶
Note
You can open the file
20-joystick.ino
under the path ofelite-explorer-kit-main\basic_project\20-joystick
directly.Or copy this code into Arduino IDE.
Now, when you push the rocker, the coordinates of the X and Y axes displayed on the Serial Monitor will change accordingly. Pressing the button will also display the coordinate Z=0.
Code Analysis¶
The code is use the serial monitor to print the value of the VRX and VRY pins of the joystick ps2.
void loop()
{
Serial.print("X: ");
Serial.print(analogRead(xPin), DEC); // print the value of VRX in DEC
Serial.print("|Y: ");
Serial.print(analogRead(yPin), DEC); // print the value of VRX in DEC
Serial.print("|Z: ");
Serial.println(digitalRead(swPin)); // print the value of SW
delay(50);
}