1.3k
This example shows how to connect a joystick to your Arduino.
Here is a picture of the joystick I am using, again this was part of the 37 in 1 sensor kit that I purchased recently
Here is the pinout and where I connected it
Pin Number | Label | Signal | Arduino Connection | ||||
1 | GND | Ground | |||||
2 | +5V | 5V | |||||
3 | VRx | Voltage proportional to X position | Connected to analogue pin 0 | ||||
4 | VRy | Voltage proportional to Y position | Connected to analogue pin 1 | ||||
5 | SW | Joystick pushbutton | Connected to Digital pin 2 |
Code
const int SWITCH = 2;
const int X_PIN = 0;
const int Y_PIN = 1;
void setup()
{
pinMode(SWITCH, INPUT);
digitalWrite(SWITCH, HIGH);
Serial.begin(115200); //watch the baud rate in the serial monitor
}
void loop()
{
Serial.print(digitalRead(SWITCH));
Serial.print(” – “);
Serial.print(analogRead(X_PIN));
Serial.print(” by “);
Serial.println(analogRead(Y_PIN));
delay(100);
}
Links