• sadhvikadari2k1

SERVO MOTOR CONTROLLED BY JOYSTICK


Ever heard of a joystick? The answer would be obviously yes for people who like playing video games and also who are tech-savvy. What if I mention a thumb joystick, the first thing that comes to your mind eventually is game controllers. Definitely, it is used for playing games but it also acts as a great addition in DIY Electronics, and there are a lot of fun things that one can do with it.


This article covers the working and configuration of servo motor controlled by thumb joystick using Arduino in detail.


List Of Components Used


1. Servo motor

A servo motor is actually a rotary actuator that gives precise control in terms of its angular position, acceleration, and velocity. A servo motor works on the principle of PWM (Pulse width modulation). This implies that depending on the width of the pulse that is applied the rotor will turn in the desire position. For this interfacing, an SG90 servo motor is used. The white piece in the picture is a rotor and is controlled by a bunch of gears inside. If a DC source powers the motor then it is called a DC servo motor and if powered by an AC source then it is called an AC servo motor.

Image showing a SG90-Servo motor
SG90 Micro-Servo Motor Image

Buy SG90- Servo Motor from here.


2. Arduino Board

Arduino Board can read inputs such as light on a sensor, a finger on a button, or a Twitter message and convert it to an output such as activating a motor, turning on an LED, publishing something online. All of this can be done by sending a set of instructions to the microcontroller on the board by the use of the Arduino programming language.

Image of an ARDUINO UNO Board
ARDUINO Board

Buy Arduino UNO from here.


3. Thumb Joystick

A #thumb joystick also called an analog joystick that has two potentiometers in it, one on the x-axis and the other on the y-axis, which separates the horizontal and the vertical movements respectively. So when we move this #joystick in the x-y plane the variable resistances of the potentiometer will vary and the Arduino will read this as an analog input. The rotatory head of the servo motor is controlled by the joystick.

Image of a "thumb joystick" to control the servo motor
Thumb Joystick

Buy Thumb Joystick from here.


Circuit Diagram

The circuit below shows the connection of an #SG90 servo motor with the Arduino board and thumb joystick. There are three wires of the servo motor. The brown wire is the ground wire, the red one goes to the 5 volts, and the orange one is called the #PWM wire which is connected to the digital line of the servo motor and the yellow one of the joystick is connected to the analog pin of the Arduino board.

Note: We are using only 3 pins out of the 5 of the thumb joystick in this project.


Circuit Diagram Of Arduino Connected With SG90 And Thumb Joystick
Circuit Diagram with SG90 and Thumb Joystick

The hardware arrangement of this project is very simple to make. First and foremost, connect the thumb joystick with the Arduino board. The connections for the thumb joystick and the Arduino board are given as:


Joystick Connection:

  1. Joystick VCC (red wire) --> Arduino 5Volts

  2. Joystick GND (brown wire) --> Arduino GND (grounded)

  3. Joystick x_axis pin (yellow wire) --> #Arduino analog input pin A0

Subsequently, connect the servo motor with the Arduino board. The connections for servo motors with Arduino board are given as:


Servo Connection:

  1. Servo VCC (red wire) --> Arduino 5Volts

  2. Servo GND (brown wire) --> Arduino GND (grounded)

  3. Servo signal pin (orange wire) --> Arduino pin 3


Code Explanation

Since we are using only one servo motor, considering only one axis is more than enough.

This is the code that has been injected into the Arduino.

#include <Servo.h>

First and foremost, we incorporate the servo library which will facilitate us with making the code much easier. So in the first line, the servo library is included.

Servo servo;

Then, we initialize a variable, for the servo motor which will help us in using the library functions.

int x_axis;

Furthermore, we initialize a variable, which takes an input value from the thumb joystick.

int servo_val;

Also, we initialize the signal pin on the servo, which will take the angle to which the servo motor has to be turned to.

pinMode(A0,INPUT);            

Under the setup function, we then set our analog input pin A0 as the input,

servo.attach(3);

And then we are communicating to the Arduino that the #servo motor is connected to pin number three. In the loop function, we read the values for the horizontal position from the joystick and save this in the variable declared.

x_axis=analogRead(A0);

Under the loop, we are using the declared variable x_axis to read the value from pin A0.

servo_val=map(x_axis,0,1023,0,180);

Now we are changing the x_axis value which is between 0 and 1023 to a value that is between 0 and 180 for the motion of the servo motor.

servo.write(servo_val);

We are going to communicate with the Arduino that we want the servo #motor to turn to this particular angle.


Complete Code

//interfacing Arduino with Joystick

#include <Servo.h>

Servo servo;

int x_axis;   //x axis reading from joystick will go into analog pin A0
int servo_val;

void setup()
{
  pinMode(A0,INPUT);                   //pin A0 shall be used as output
  servo.attach(3);
}

void loop()

{
  x_axis=analogRead(A0);                //read joystick input on pin A0
  servo_val=map(x_axis,0,1023,0,180);
  servo.write(servo_val);     //write the calculated value to the servo  
}

Download the source code from here

CODE
.txt
TXT • 243B

Note: Please change the file extension to .ino(Arduino IDE) file format before you upload the code to Arduino Board.



Working

Circuit image of whole setup when the switch is in OFF state
Circuit image at switch off state

When the thumb joystick moves along the axis, it gives us values from 0 to 1023, but as servo motor can take only values from 0 to 180 and it is the maximum angle that it can rotate to.


So we are simply converting the x_axis value which is in between 0 and 1023 to 0 and 180. Then by moving the joystick we see the servo motor moves accordingly. When the joystick is moved along the axis, the servo will move towards the right or left.


By changing the mapping value which is now from 0 to 180 to 0 to 90, we check out the results. Then we observe that the servo motor sets itself to a mean position of 45 degrees and moves accordingly from there as we move the joystick around.

Circuit image of whole setup when the switch is in ON state
Circuit image at switch on state

Video By - Aishwarya Balajee

Video explanation of the whole working process for a better understanding.

The article depicts how the Arduino Joystick Interface can be implemented and one can understand the use of the Arduino Joystick in an application where we will control a Servo Motor. The joystick can be used in various applications like controlling DC Motors and Servo Motors, controlling LEDs, Remote-controlled Cars, Robotic Control, etc.

See also:

  1. Interfacing servo motor with Arduino

  2. Interfacing Water/Rain Sensor with Arduino

  3. Interfacing servo motor with RPi