Advertisement
arduino joystick connections

Arduino Joystick Experiment – Tutorial #10

I have started this joystick controller experiment inspired by the need of a simple DIY joystick which could deliver greater flexibility in the possible designs. Hardware component basis is very minimal for this experiment so most of DIY electronics hobbyists even with limited experience could enjoy it and build a useful joystick by themselves.

The main brain is the Arduino UNO microcontroller. Few other discrete components are everything what is needed to get it working. This article presents a procedure for reading the analog ports of the Arduino UNO R3 board, on which an analog Joystick was connected. The analog data are related to the position of command and can be monitored through the Arduino setup.

The 2-Axis analog Joystick used here provides a simple and convenient way to add X-Y control to a project. A (10K) potentiometer attached to each axis provides proportional feedback of the up/down and left/right positions. The joystick is spring-loaded, so that it always returns to its centered position when you release it. In addition, a tactile switch is included in the joystick, which turns to on position (closed state) when the joystick button is depressed.

2-Axis analog Joystick

2-Axis analog Joystick

The connection of Arduino controller to joystick sensors and button is very simple as shown in the layout diagram. As stated, The joystick can be moved in two dimensions typically represent the X and Y. The X position is read from analog pin A0 and the Y position is read from analog pin A1. In the Arduino sketch, the analogRead () function returns a number in 0 to 1023 range (512 at the centre/idle position of the joystick).

arduino joystick connections

layout diagram

The sketch we require to observe the joystick operation make a polling to two of the analog input pins A0 & A1. Here the sketch make the first LED (onboard-D13) blink with the values read from the joystck sensors as a direct visual feedback of how we handle the joystick. In idle state, this LED blinks at a default rate, and changes with up/down (UD) & left/right (LR) operations of the joystick. The second LED (connected at D11) lights up only when the joystick button is pushed downwards, from its centre (idle) position.

/* 
* Arduino & Analog Joystick
* Demo/Test Sketch
* Reads two analog inputs A0,A1
* And a button switch input at D12
* Joy Stck = 2-axis analog jostick made of two 10K potmeters
* Tested at TechNode Protolabz/July 2014
*/
int flashLED = 13;
int UD = 0; 
int LR = 1; 
int input1 = 0; 
int input2 = 0;
int button=12;
int pushLED=11;
int buttonState=0;
void setup() {
   pinMode(flashLED, OUTPUT);
   pinMode(button,INPUT);
   pinMode(pushLED, OUTPUT);
   digitalWrite(button,HIGH); 
}
int handleValue(int data) {
   return (data * 9 / 1024) + 48;
}
void loop() {
   input1 = analogRead(UD); 
   delay(100); 
   input2 = analogRead(LR); 
   digitalWrite(flashLED, HIGH); 
   delay(input1);
   digitalWrite(flashLED, LOW);
   delay(input2);
   buttonState = digitalRead(button);
   if (buttonState == LOW) { 
      digitalWrite(pushLED, HIGH); 
     } 
   else {
      digitalWrite(pushLED, LOW); 
   }
}
prototype

Testing of the Prototype

I am sure there are plenty of ways to improve on this, but the effect is not bad and the sketch is very simple!

2 Comments

Join the conversation!

Error! Please fill all fields.
  • T.K.HAREENDRAN

    digitalWrite(button,HIGH)// used to enable the internal pull-up resistor!

    Attn: Mr Aslam

  • Muhammad Ahsan Aslam

    can you please explain how a pin can make a output if it is set to input mode.

    Here is Code Section from above.
    void setup() {
    pinMode(flashLED, OUTPUT);
    pinMode(button,INPUT);// defining input pin, but using as output.
    pinMode(pushLED, OUTPUT);
    digitalWrite(button,HIGH);//I have question here.
    }

Looking for the latest from TI?