L.O.A.S.H’s Arduino Projects: Controlling Servo Motors with a Joystick

With this project, you will be able to control two servo motors with a joystick with Arduino! 

For this project you will need to gather:

  • Arduino Board

0-02-06-3e61517352900af7aca75ae80bd8bb003f5bb9e72a9e9557f75162f83bbd25fa_full.jpg

  • Breadboard

0-02-06-cd66c6b6d65f22179fc1a06c362c44db50bc340f3218d339911b1eb0709a55b8_full.jpg

  • Wires | x13

0-02-06-b48c27591fb83fd36c03c33e241a51a1f619e6b1d07f0de5cb66f1b076e17d8b_full

  • Servo Motors | x2

0-02-06-656ab4f6bc88f237b866d892bdb219fefa5e480bf645f88fc2cbfc55f749f569_full

  • Battery 

0-02-06-8ac9e32dd7aa2cab229409c610e5218da78fd9469262db96a33691471e1b1a6b_full

 

To make this (really cool) project, you will need to:

Step 1: Battery

Connect your external battery to your breadboard.

0-02-06-fa93aff5c167975906e799e6193fd5bb7e4fdcd8271bcebacdfc368bc592e900_full  

Step 2: Wiring 

Get a piece of wire and connect one end to GND on your Arduino Board and the other end to the negative channel on your breadboard.

0-02-06-fb2c30416f0bc5b94c8d3c9d743bffc1439a17d6e5c06e1fe5028219feafe597_full

Step 3: Servo Motors

Get your two servo motors and 6 wires. Connect the VCC and the GND of the two servos to the VCC and GND on the breadboards inputs. Then, connect your first Servo Signal Connect to the Arduino Digital PMW 3 on your Arduino Board.

0-02-06-3daf95e97aa092d7453a4010191438347de1b18e134785dded2454d5d49c5d69_full

 In case you aren’t very familiar with the Arduino Digital PMW 3, it looks like the photo below as well as the Digital PMW 5 and the blue wire from the first servo

0-02-06-24edbd7bb8929ca5eab0480444ddfbfd4030d44cc1b9499de030c0acd518d611_full

Your second Servo Signal Connect should be connected to the Arduino Digital PMW 5.

0-02-06-a98f20a183f18ace8fc31c4260055729730df7fcbc957b428a9d8c2ed68210af_full

Step 4: JoyStick

Connect the GND on your Joystick to the GND on the Arduino Board.

0-02-06-750f14cd78d2d4ccb1571cc4bb98c5cce58194a7654087ed21c9b2f5a411bcaa_full

Then, connect the +5V on your joystick to the 5V on the Arduino Board.

Then, connect the VRx on your joystick to the A0 on the Arduino Board.

Then, connect the VRy on your joystick to the A1 on the Arduino Board.

The photo below shows where I connected what and where. Just look at the corresponding colors to which wire and the connection.

0-02-06-5542b4f4d9b290f3f778bc26fd3dc21fc12fd4cef7fe95ce67ab70ada427e734_full

It also looks something like this….

Screen Shot 2017-07-03 at 13.40.11

 

Step 5:  The Code

Open up your Arduino on your computer and copy the code below:

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

//define our servos
Servo servo1;
Servo servo2;

//define joystick pins (Analog)
int joyX = 0;
int joyY = 1;

//variable to read the values from the analog pins
int joyVal;

void setup() 

{ //attaches our servos on pins PWM 3-5
servo1.attach(3);
servo2.attach(5); }

void loop() 

{ //read the value of joysticks (between 0-1023)
joyVal = analogRead(joyX);
joyVal = map (joyVal, 0, 1023, 0, 180);
servo1.write(joyVal);

joyVal = analogRead(joyY);
joyVal = map(joyVal, 0, 1023, 0, 180);
servo2.write(joyVal);
delay(15); }

Screen Shot 2017-07-03 at 13.54.02

Step 6: Verify, Connect and Upload

Once you verify and save the code, you can connect your Arduino board to your computer and upload the code to your board. Tada! 

Step 7: Have fun and Experiment

Yay! We’ve finally finished yet another Arduino Project. You guys are so awesome! Now, after playing around with this, you can try to experiment maybe add more things (an example, “lights, perhaps?”).

Wait, if you are having any problems uploading or your project isn’t functioning properly, you should double check your wiring and maybe check which Port your Arduino (on your computer) connected to in the tools. If there is anything else don’t be shy to comment down below!

Screen Shot 2017-07-03 at 13.57.45

 

Yours truly, 

L.O.A.S.H

 

 

Advertisements