Professional Documents
Culture Documents
Experiment-1: Circuitdiagram
Experiment-1: Circuitdiagram
Experiment-1: Circuitdiagram
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.
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: