0% found this document useful (0 votes)
27 views5 pages

Experiment-1: Circuitdiagram

The document describes an experiment to control servos with a joystick module connected to an Arduino. The circuit connections and Arduino code are provided to move two servos based on the horizontal and vertical position of the joystick.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views5 pages

Experiment-1: Circuitdiagram

The document describes an experiment to control servos with a joystick module connected to an Arduino. The circuit connections and Arduino code are provided to move two servos based on the horizontal and vertical position of the joystick.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment-1

Aim:Controlling actuators through Serial Monitor. Creating different led patternsand


controlling them using push button switches. Controlling servo motor with the help
of joystick.

CircuitDiagram

The hardware part of this project is very easy to make. First, connect the joystick
modulewiththeArduino.TheconnectionsforthejoystickmoduleandtheArduino are as
follows:

• ConnecttheVCConthejoystickmodulewiththe5VpinontheArduino
• ConnecttheGNDpinonthejoystickmodulewiththeGNDontheArduino
• ConnecttheVERpinonthejoystickmodulewiththeA0ontheArduino
• ConnecttheHORpinonthejoystickmodulewiththeA1ontheArduino

Afterthat,connecttheservomotorswiththeArduino.Theconnectionsforservo motors
with Arduino are as follows:

• ConnecttheblackwireonboththeservomotorswiththeGNDontheArduino
• Connecttheredwireonboththeservomotorswiththe5VpinontheArduino
• Connecttheyellowwireonthefirstmotorwithpin8ontheArduino
• Connecttheyellowwireonthesecondmotorwithpin9ontheArduino
WorkingProcedure
Whenthejoystickmodulemovesinthehorizontalorintheverticaldirection,itgives
usvaluesfrom0to1023.Sowecanapplyaconditioninthecodethatifthevalueis
lessthan300orgreaterthan700,thentheservoswillmove.

Whenthejoystickismovedinthehorizontaldirection,thefirstservowillmove
towardsrightorleftanduponmovingthejoystickintheverticaldirection,the second servo will
move towards the right or left.
ArduinoCode:
#include
Servoservo1;
Servoservo2;
intx_key=A1;
inty_key=A0;
int x_pos;
inty_pos;
int servo1_pin = 8;
intservo2_pin=9;
int initial_position = 90;
int initial_position1 = 90;

void setup ( ) {
Serial.begin (9600) ;
servo1.attach (servo1_pin ) ;
servo2.attach (servo2_pin ) ;
servo1.write (initial_position);
servo2.write(initial_position1);
pinMode (x_key, INPUT) ;
pinMode (y_key, INPUT) ;
}

voidloop(){
x_pos=analogRead(x_key);
y_pos=analogRead(y_key);

if(x_pos<300){
if(initial_position<10){}else{initial_position=initial_position-20;servo1.write(
initial_position);delay(100);}}if(x_pos>700){
if(initial_position>180)
{
}
else{
initial_position = initial_position + 20;
servo1.write ( initial_position ) ;
delay(100);
}
}

if(y_pos<300){
if(initial_position1<10){}else{initial_position1=initial_position1-
20;servo2.write(initial_position1);delay(100);}}if(y_pos>700){
if(initial_position1>180)
{
}
else{
initial_position1 = initial_position1 + 20;
servo2.write ( initial_position1 ) ;
delay(100);
}
}
}

CodeExplanation

Firstofall,weincludedthelibraryfortheservomotorwhichwillhelpuswith
makingthecodeeasier.Then,weinitializedtwovariables,oneforeachofthetwo
servomotorswhichwillhelpusinusingthelibraryfunctions.

#include
Servoservo1;
Servoservo2;

Then,weinitializedthepinswherewehaveconnectedtheverticalandhorizontal
pinsonthejoystickmoduleandalsothesignalpinsontheservos.

intx_key= A1;
inty_key= A0;
intx_pos;
inty_pos;
intservo1_pin=8;
intservo2_pin=9;
intinitial_position=90;
intinitial_position1=90;

Then we tell the Arduino where we have connected the servo pins and also moved the
servomotorsattheinitialposition,whichis90degrees.Afterthat,wedeclaredboth
theverticalandhorizontalpinsonjoystickmoduleastheinputpins.

servo1.attach (servo1_pin ) ;
servo2.attach (servo2_pin ) ;
servo1.write (initial_position);
servo2.write(initial_position1);
pinMode (x_key, INPUT) ;
pinMode (y_key, INPUT) ;
Intheloopfunction,wereadthevaluesforthehorizontalandtheverticalposition
fromthejoystickmoduleandsavedtheseinthevariables.Thenweapplieda
conditionthatifthevalueforthehorizontalpositionislessthan300,thenthefirst servo will
move towards the right.

x_pos = analogRead (x_key) ;


y_pos = analogRead (y_key) ;
if (x_pos<300){
if(initial_position<10)
{
}
else{
initial_position = initial_position - 20;
servo1.write ( initial_position ) ;
delay(100);
}
}

If the value for the horizontal position is greater than 700, then the servo will move
towardstheleft.Similarlyfortheverticalpositionofthejoystickmodule,ifthevalue is less than
300, then the second servo will move towards the left, and if the value is
greaterthan700,thenthesecondservowillmovetowardstheright.

if(x_pos>700){
if(initial_position>180)
{
}
else{
initial_position = initial_position + 20;
servo1.write ( initial_position ) ;
delay(100);
}
}
Output:

You might also like