Servo motor control with Joystick and Arduino

Servo motor control with Joystick and Arduino

In this tutorial, we are going to learn how to make a Servo motor control with joystick and Arduino. In this project, we are going to control the angular position of the servo motor depending on the input given to the Joystick. For this project, the required components are:-

->Servo Motor

->Joystick Module

->Arduino Uno R3

->Bread Board

->Male-Male, Male-Female connecting wires

Servo motor

A Servo motor is a type of motor that is used for precise angular and linear positions. It can rotate up to 180 degrees. The Servo motor is a closed-loop system where it uses feedback to control the motion of the motor. It is used in various electronic devices like DVDs, toy cars, robotics, etc, and many more. There is a total of 3 wires on the servo motor namely Vcc, GND, and Control pin.

Pin configuration of Servo motor

WIRE OF MOTOR

COLOR OF EACH WIRE

Vcc pin(5V)

Red

GND pin

Brown

Control or PWM pin

Yellow or Orange

Joystick module

Joystick Module is a very simple module that consists of two variable resistors (or potentiometers) and a push button. Variable resistors provide analog outputs whereas push button generates output in digital form either high state or low state. One potentiometer is for X-axis and the other potentiometer is for Y-axis. THE JOYSTICK module consists of 5 pins. The description of each pin is given below.

Pin description of Joystick module

Pin no.

Pin

Description

1

GND

Connect to the ground terminal of the Arduino.

2

+5V

Connect to the 5v pin of the Arduino

 

3

 

VRx

It gives output with respect to the joystick’s X-axis (left/right) position.

 

4

 

VRy

It gives output with respect to the joystick’s Y-axis (up/down) position.

 

 

5

 

 

SW

It gives the output obtained from the push button. If the button is pressed the output will be low (0) and when it is released the output will be high(1).

Circuit diagram for Servo motor control with Joystick and Arduino

Servo control using joystick

Follow the connection as explained below:-

Connection of Servo with Arduino

Step 1:- Connect the Red wire(Vcc) of the servo to the 5v pin of the Arduino.

Step 2:- Connect the Brown wire(GND) of the servo to the GND pin of the Arduino.

Step 3:- Connect the Yellow/Orange wire(control signal) of the servo to the digital pin 9 of the Arduino.   //This pin no 9 is for the X-axis position of the joystick

Step 4:- Connect the Yellow/Orange wire(control signal) of the servo to the digital pin 10 of the Arduino.  //This pin no 10 is for the Y-axis position of the joystick

 

Connection of Joystick module with Arduino

Step 1:- Connect the joystick’s GND pin to Arduino’s GND pin.

Step 2:- Connect the +5v pin of the joystick to the 5v pin of Arduino.

Step 3:- Connect the VRX pin of the joystick to the analog pin A0 of the Arduino.

Step 4:- Connect the VRY pin of the joystick to the analog pin A1 of the Arduino.

Step 5:- Connect the SW pin of the joystick to the digital pin 2 of the Arduino.

 

***IMPORTANT NOTE:- In the above tutorial on Servo motor control with joystick and Arduino, we are using only 1servo for demonstration. The joystick has 2 position axis x & Y. In the given code below we have considered both the axis for the servo control. You can control the servo from either of the axis for a better understanding of the working principle of the joystick.

Code for the Project


Download code

//Welcome to my new tutorial on Servo motor control with Joystick and Arduino by FREAK ENGINEER

 

#include <Servo.h>   //including the library of the Servo Motor

 

Servo Xservo;        Servo Yservo;

 

int Xpin=A0;        // initialising A0 as the X-axis movement of the joystickint Ypin=A1;        // initialising A1 as the Y-axis movement of the joystickint Spin=2;         // initialising digital pin 2 as the joystick’s push buttonint XSpin=9;        // initialising digital pin 9 as the servo control signal pinint YSpin=10;       // initialising digital pin 10 as the servo control signal pin           // declaring variables for storing read valuesint WVx;int WVy;int Xval;int Yval;int Sval;  

 

int dt=200;         // intialising delay variable

 

void setup(){  // put your setup code here, to run once:Serial.begin(9600);

 

pinMode(Xpin,INPUT);pinMode(Ypin,INPUT);pinMode(Spin,INPUT);pinMode(XSpin,OUTPUT);pinMode(YSpin,OUTPUT);

 

Xservo.attach(XSpin);         //enabling pin 9 for XservoYservo.attach(YSpin);         //enabling pin 10 for Yservo

 

digitalWrite(Spin,HIGH);      // intialising the Spin HIGH(1) of the joystick push button}

 

void loop(){  Xval=analogRead(Xpin);       // Reading the value from A0 WVx=(180./1023.)*Xval;       // Calculating the value for the X axis movement of the joystick  Yval=analogRead(Ypin);       // Reading the value from A1 WVy=(180./1023.)*Yval;       // Calculating the value for the Y axis movement of the joystick  Sval=digitalRead(Spin);      // Reading the value from digital pin 2

 

 Xservo.write(WVx);           // Moving the servo using the X-movement of the joystick Yservo.write(WVy);           // Moving the servo using the Y-movement of the joystick  delay(dt);  Serial.print(“X Value = “); Serial.print(Xval);                     // displaying the X-axis value on the serial monitor Serial.print(” Y Value = “); Serial.print(Yval);                     // displaying the X-axis value on the serial monitor Serial.print(” Switch State is “); Serial.println(Sval);                   // displaying the push button value on the serial monitor }

Author

K Sai Kamal Kumar


Visit YouTube

Share This Post

Leave a Comment