First Sensor – KY-023 Joystick


This little joystick is similar to the ones on many modern game controllers. It slides smoothly in X and Y and clicks a button when pressed. The X and Y axes are connected to potentiometers and form a voltage divider between 0v and 5v with the center rest point theoretically being at the 2.5v point. Reading the joysticks is as simple as polling two analog inputs. The switch connects to ground when depressed, so use a digital input with the Arduino’s built in pullup resistor.

Below is the code that I ended up with for reading this sensor. It’s a very basic program that sets up the appropriate pins and then loops forever reading the values from all three pins and displays them via the serial port. I’ll definitely revisit this joystick in some of the future projects as a simple input device that allows for two different analog values and a digital input for triggering.

Pros:
  • Simple interface – power, ground, 2x analog, 1x digital
  • Feels smooth and clicks nicely
Cons:
  • The values return hit their limits (0 and 1023) well before the joystick physically does
  • While simple to interface, it does use three pins besides power and ground

#define X_PIN A0
#define Y_PIN A1
#define B_PIN 3

void setup() {
  pinMode(X_PIN, INPUT);        // X-axis
  pinMode(Y_PIN, INPUT);        // Y-axis
  pinMode(B_PIN, INPUT_PULLUP); // Button
  
  Serial.begin(9600);
}

void loop() {
  // Read all the values
  int X_Val = analogRead(X_PIN);
  int Y_Val = analogRead(Y_PIN);
  int B_Val = digitalRead(B_PIN);

  // Display all the values on one line
  Serial.print("X: ");
  Serial.print(X_Val);
  Serial.print(" Y: ");
  Serial.print(Y_Val);
  Serial.print(" B: ");
  Serial.println(B_Val);

  // Wait a little bit so the output doesn't scroll too fast to read
  delay(100);
}

Comments

Popular posts from this blog

New Project - Every Sensor in the 45-in-1 Sensor Kit