The HW-504 Joystick Module is a versatile input device commonly used in DIY electronics projects. It allows users to add analog input via a simple interface, typically for controlling cursors, objects in games, or for use in robotics. The module includes a two-axis thumb joystick that can move in the X and Y directions and usually features one or more pushbuttons that can be activated by pressing down on the stick.
Pin | Description |
---|---|
GND | Ground connection |
+5V | Power supply input (3.3V to 5V) |
VRx | Analog output for X-axis |
VRy | Analog output for Y-axis |
SW | Digital output for pushbutton switch |
// Define the pins for the joystick connections
const int xAxisPin = A0; // Analog input pin for X-axis
const int yAxisPin = A1; // Analog input pin for Y-axis
const int buttonPin = 2; // Digital input pin for pushbutton
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 joystick position values
int xPosition = analogRead(xAxisPin);
int yPosition = analogRead(yAxisPin);
// Read the button state (LOW when pressed due to pull-up resistor)
int buttonState = digitalRead(buttonPin);
// Print the joystick position values 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 overwhelming the serial monitor
delay(100);
}
Q: Can I use this joystick with a 3.3V system? A: Yes, the joystick module can operate at 3.3V, but ensure that all connections are consistent with the 3.3V logic.
Q: How do I interpret the analog values from the joystick? A: The analog values will range from 0 to 1023, with around 512 being the center position for both axes.
Q: What is the function of the SW pin? A: The SW pin is connected to the pushbutton switch on the joystick. It will read LOW when the button is pressed and HIGH otherwise.
Q: How can I improve the precision of the joystick readings? A: Implement software filtering or averaging of multiple readings to reduce noise and improve precision.