[Arduino] Analog Thumb Joystick

This is going to be a easy and fun one, analog joystick for Arduino. I got this one for around $6 from Adafruit. Awesome and easy to use. Essentially, this is what they use in all those consoles, PS1, PS2, PS3, PS4… Some stuffs you can make with this: robotic servo control, RC motor speed controller…

Introduction
Basically, the Joystick work like a potentiometer, an adjustable voltage divider to adjust the output voltage. By going to one side to another, the voltage change from 0V to 5V, or whatever your input voltage is. Same apply to pressing down on the joystick. Since this device work by adjusting the voltage, we will need to use the Analog Input pins from the Arduino to read the data.

Wiring
The wiring is very simple, just hook up Vcc to the 5V supply from Arduino, GND to ground. Then hook up the rest to the Analog Input pins. Here’s what mine loop like:

Vcc   -> 5V
Xout  -> A0
Yout  -> A1
Sel    -> A2
GND -> GND

Coding
Well, again, analog device, just use the analogRead function in the Arduino, play around with the joystick and see what values it give you. From the values you get for x, y, button, you can control your projects. For example, map the y to pwm signal to control your RC motor speed with something like:

pwm = map(x,512,1023,1000,2000)

Anyway, here’s the source code to get your output values.

// Yu Hin Hau
// Adafrit Analog Thumb Joystick
// 4/16/2014

void setup() {
  Serial.begin(9600);
}

void loop() {

  // Read X and Y Value from Analog
  int x = analogRead(0);
  int y = analogRead(1);
  int button;

  // Check Button Value
  if (analogRead(2) >= 1000)
    button = 1;
  else
    button = 0;

  // Output Data
  Serial.println("X: "+String(x)+"\tY: "+String(y)+"\tButton: "+String(button));

  // Delay
  delay(10);
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s