The dual-axis joystick (KY-023) is a module which consists of two potentiometers and one button, allowing the user to control various devices (chassis, robots), for example, to remotely control a two-axis rotary camcorder that can be rotated in two planes.
The dual-axis joystick module consists of two 10 kΩ potentiometers, which determine the position of the X and Y axes. The resistance is changed by a lever. The average leg of each potentiometer is connected to the VRX and VRY contacts, and the second and third legs are connected to the power supply and ground. In addition, a button is installed, readings are taken from the SW contact, a seat for the pull-up resistor (R1) is also provided.
To read data from the VRX pins (X-axis) and VRY (Y-axis), you must use the analog port of the Arduino. (value from 0 to 5V or from 0 to 1023), and for reading data from the button we use a digital port. So, as one pin of the button is connected to the ground and when the joystick is pressed down, the circuit is closed, but false positives (switch bounce) is possible. To obtain stable readings, the output RW must be pulled to the + 5V supply, via pull-up resistor R1 or use the built-in pull-up resistor.
Parts List
Connection
For reading the values from the potentiometers of the joystick, a pair of analog pins on the Arduino (A0 and A1) and a digital pin (D2) for reading the values from the button will be used.
Code
[codesyntax lang=”cpp”]
int value = 0; void setup() { pinMode(2, INPUT); Serial.begin(9600); } void loop() { value = analogRead(A0); // read X axis value Serial.print("X:"); Serial.print(value, DEC); value = analogRead(A1); // read Y axis value Serial.print(" | Y:"); Serial.print(value, DEC); value = digitalRead(2); // read Button state Serial.print(" | Button:"); Serial.println(value, DEC); delay(100); }
[/codesyntax]
Output
Open the serial monitor and move the joystick
X:522 | Y:523 | Button:0
X:522 | Y:522 | Button:1
X:522 | Y:522 | Button:1
X:522 | Y:523 | Button:1
X:0 | Y:60 | Button:1
X:0 | Y:48 | Button:1
X:0 | Y:88 | Button:1
X:0 | Y:99 | Button:1
X:522 | Y:523 | Button:1
X:522 | Y:522 | Button:1
X:522 | Y:522 | Button:0
X:522 | Y:522 | Button:1