Menu Close

Servo Motor controlled using Joystick – Arduino

Robo India presents tutorial on, how to use Joystick to control servo motors using Arduino. 

1. Introduction 

A joystick is an input device which consists of a lever, which can move in several directions in X and Y axes. It is an analog joystick – more accurate and sensitive than just ‘directional’ joysticks – with a ‘press in to select’ button.

Since it’s analog, you’ll need two analog reading pins on your microcontroller to determine X and Y. These X and Y values are used to interpret the position of the lever.

The values are from 0 to 1023. Arduino has built in ADC converter which convert the voltage 0V – 5V to 0 to 1023 values. When the lever is left untouched the lever stays at mid position of both X and Y axis and shows half value of 1023.

2. Hardware required

S.No.ItemQuantity
1.Arduino 1
2.Breadboard 1
3.Joystick Module 1
4.Servo Motors 2
5.Male to Female Jumper Wires 4
6.Male to Male Jumper Wires 8

3. Connection

4. Programming

You may download this Arduino Sketch from here.

#include <Servo.h>           //include servo library

Servo servo1;                // creating servo object
Servo servo2;
int joystick_x = A0;              // joystick x direction pin                                          
int joystick_y = A1;              // joystick y direction pin                                         
int pos_x;                   // declaring variable to stroe horizontal value
int pos_y;                   // declaring variable to stroe vertical value
int servo1_pos=90;          // storing servo position
int servo2_pos=90;

void setup ( ) 
{
Serial.begin (9600) ;
servo1.attach (3) ;          // servo 1 attached pin
servo2.attach (4) ;          // servo 1 attached pin
servo1.write (servo1_pos);           
servo2.write (servo2_pos);
pinMode (joystick_x, INPUT) ;                     
pinMode (joystick_y, INPUT) ;                      
}

void loop ( ) 
{
pos_x = analogRead (joystick_x) ;  
pos_y = analogRead (joystick_y) ;                      

if (pos_x < 300)            //if the horizontal value from joystick is less than 300
{
  if (servo1_pos < 10)      //first servo moves right
  { 
  } 
  else
  { 
    servo1_pos = servo1_pos - 20; 
    servo1.write ( servo1_pos ) ; 
    delay (100); 
  } 
} 
if (pos_x > 700)
{
  if (servo1_pos > 180)
  {  
  }  
  else
  {
  servo1_pos = servo1_pos + 20;
  servo1.write ( servo1_pos ) ;
  delay (100) ;
  }
}

if (pos_y < 300)      //if the vertical value from joystick is less than 300
{
  if (servo2_pos < 10)  //second servo moves right
  { 
  } 
  else
  { 
    servo2_pos = servo2_pos - 20; 
    servo2.write ( servo2_pos ); 
    delay (100); 
  } 
} 
if (pos_y > 700)
{
  if (servo2_pos > 180)
  {  
  }        
  else
  {
  servo2_pos = servo2_pos + 20;
  servo2.write(servo2_pos) ;
  delay (100) ;
  }
}
}

5.Output

To control servo motors, when we move the joystick horizontally, the first servo motor will move right and left depend on lever position. Similary, when we move the joystick vertically, another servo motor will move right and left depend on lever position.

If you have any query please write us at support@roboindia.com

Thanks and Regards
Content Development Team 
Robo India
https://roboindia.com

Leave a Reply