The Adafruit Analog 2-Axis Joystick is a versatile and compact input device that provides a simple interface for user input. It operates using two potentiometers—one for the X-axis and one for the Y-axis—which allow it to measure position or movement along these axes. This joystick is commonly utilized in applications such as gaming consoles, robotics, and various control systems where manual input is required for navigation or operation.
Pin Number | Name | Description |
---|---|---|
1 | GND | Ground connection |
2 | +V | Power supply (3.3V to 5V) |
3 | VRx | Voltage output for X-axis |
4 | VRy | Voltage output for Y-axis |
5 | SW | Switch (activated when joystick is pressed down) |
// Define the analog pins for the joystick
const int xAxisPin = A0;
const int yAxisPin = A1;
const int buttonPin = 2;
void setup() {
// Initialize the button pin as an input with an internal pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
// Begin serial communication at a baud rate of 9600
Serial.begin(9600);
}
void loop() {
// Read the position of the joystick
int xPosition = analogRead(xAxisPin);
int yPosition = analogRead(yAxisPin);
// Read the state of the joystick button
int buttonState = digitalRead(buttonPin);
// Print the X and Y positions to the Serial Monitor
Serial.print("X: ");
Serial.print(xPosition);
Serial.print(" | Y: ");
Serial.print(yPosition);
Serial.print(" | Button: ");
Serial.println(buttonState);
// Add a small delay to prevent flooding the serial output
delay(100);
}
Q: Can I use this joystick with a 3.3V system? A: Yes, the Adafruit Analog 2-Axis Joystick can operate with a 3.3V power supply.
Q: How do I know if the joystick is centered? A: The joystick is centered when the analog output values for both the X and Y axes are approximately half of the maximum analog reading value.
Q: What is the function of the SW pin? A: The SW pin is connected to a switch that is activated when the joystick is pressed down. It can be used as a digital input to detect this action.