Introduction: Arduino + 2 Servos + Thumbstick (joystick)

In my other 2 Arduino tutorials I have help new users to play tones and making 2 servos move with a mouse.
This time I will help you move the same 2 servos with a thumbstick.
And again I have surf the internet to see If there are tutorials that clear to understand specially for
the beginner Arduino user and, I have not found one.
To move 2 servos with the thumbstick is way easier and painless than using the mouse. And the reason for this is that some times the processing software does not work correct or there is a conflict between the arduino software and the processing software.
For all the connections I will use the bread board.

Dont forget to vote for me on the Arduino challenge!!!
 
Enjoy and have fun!!!! ;)
                                                                                                                                    
                                                                                                                                                                               Biomech75

Step 1: Materials

Hardware: (bought @ Radioshack)

Arduino Board (I am using the UNO REV 3 version)
2 Parallax servos
Parallax Thumbstick (joystick)
Small Bread Board
Solderless Breadboard Jumper Wire
Something to see the 2 servos movement ( I am using a broken Web Cam and some legos not shown, for this tutorial)

Step 2: Connecting the Servos

I will start with the 2 servos. (I am using the bread board to make all connections easy)
You can use the picture for more information.

Servo Up/Down                   Arduino                                       Servo Right/Left                     Arduino

Red Cable------------------------ 5V                                              Red Cable------------------------ 5V
Black Cable---------------------- GND                                         Black Cable---------------------- GND
Yellow or White Cable--------- PWM (4)                                Yellow or White Cable--------- PWM (10)

Step 3: Connecting the Thumbstick (joystick)

The follwing connection will confuse you a little, just follow the picture.
Again using a bread board will make things easier.

1. The thumbstick has one U/R+ and one L/R+ this two connections will provide power to the joystick. Connect them to the Arduino 5V connection.
2.  Also the joystick has two L/R connections and two U/D connections it is important that you connect both of them to the respective arduino pin.
3.  Do the same to both GND connections on the joystick.

Thumbstick                     Arduino

L/R+  ----------------------     5V
U/D+ ----------------------     5V
GND  GND ---------------    GND
U/D U/D ------------------     Analog 4
L/R L/R -------------------    Analog 3


NOTE: Always double check the connections!!!

Step 4: The Code

Not much to explain... just copy and paste the following code to the arduino software.
Always test (compile) the code before uploading it to the Arduino Board.
When you upload the code to the arduino the servos should not move until you use the joystick.

CODE:

#include <Servo.h>

const int servo1 = 3;       // first servo
const int servo2 = 10;       // second servo
const int joyH = 3;        // L/R Parallax Thumbstick
const int joyV = 4;        // U/D Parallax Thumbstick

int servoVal;           // variable to read the value from the analog pin

Servo myservo1;  // create servo object to control a servo
Servo myservo2;  // create servo object to control a servo



void setup() {

  // Servo  
  myservo1.attach(servo1);  // attaches the servo
  myservo2.attach(servo2);  // attaches the servo

  // Inizialize Serial
  Serial.begin(9600);
}


void loop(){

    // Display Joystick values using the serial monitor
    outputJoystick();

    // Read the horizontal joystick value  (value between 0 and 1023)
    servoVal = analogRead(joyH);          
    servoVal = map(servoVal, 0, 1023, 0, 180);     // scale it to use it with the servo (result  between 0 and 180)

    myservo2.write(servoVal);                         // sets the servo position according to the scaled value    

    // Read the horizontal joystick value  (value between 0 and 1023)
    servoVal = analogRead(joyV);           
    servoVal = map(servoVal, 0, 1023, 70, 180);     // scale it to use it with the servo (result between 70 and 180)

    myservo1.write(servoVal);                           // sets the servo position according to the scaled value

    delay(15);                                       // waits for the servo to get there

}


/**
* Display joystick values
*/
void outputJoystick(){

    Serial.print(analogRead(joyH));
    Serial.print ("---"); 
    Serial.print(analogRead(joyV));
    Serial.println ("----------------");
}

Step 5: Torubleshooting

1.  The servos won't move.
A.  Check the connections, remember in this tutorial we use PWM pins for the servos and the Analog pins for the joystick.
2.  When I upload the code to the board the servos, they start vibrating.
A. It means that either U/D+ or L/R+ are not properly connected. Double check your connections. You have to disconnect the usb from the     board to verify connections try to review the connections on step 3.
3. I double check the connections and the servos wont move.
A. Take out the joystick and reconnect it again in the bread board by pressing it down to make sure it is connected. Connections have to be tight from the joystick to the bread board.

Any other help or issues you have feel free to let me know and I will be glad to help all of you. ;)

Comments

author
CodyG37 (author)2017-07-06

hi im trying to acheive the same thing using motors and l298n module/motor driver , this picture explains it , i rly hope someone can help me! its for my exo skeleton project

what i plan to do with exo.png
author
abilly (author)2016-11-25

Works great! just tweaked the code a bit to average 5 inputs limit the noise

author
rmhoutz (author)abilly2017-06-29

Hi abilly I'd love to see your tweaked code it sounds like it would be a big improvement.

author
rmhoutz made it! (author)2017-06-29

Thanks biomech75 for creating this and sharing it. Also big thanks to bodhi2 for creating the code to control the servos and have them stop.when the joystick is returned to center.

20170629_100604.jpg
author
skumar231 (author)2017-05-28

HI there i make control servo with laptop mouse video

do you wan't to see the video click the link

https://www.youtube.com/watch?v=-YwpvwRELzE

author
ServiceT (author)2017-03-31

Great code. worked perfectly. I have modified it to use it on my project.

Thank you.

author
prafull porwal (author)2017-03-23

help me sir

author
prafull porwal (author)2017-03-23

sir when we release joystick camera comes into initial position but i want that when when we release joystick then it remain in his current position.

author
RomeroS3 (author)2016-12-26

having trouble when compiling. tried tweaking code and still does not want to work.

author
trentbrown made it! (author)2016-11-30

Thanks for the great instructable! Waiting for another thumbstick to arrive and then on to the robot arm (made with paint sticks and tape)

2_servo_1Thmb_B.mp4
author
DanielW245 (author)2016-11-26

is there a way to do this without the breadboard? meaning can i solder the connections or do i need to have a breadboard for this to work? im making a mask with moving eyebrows and im a bit of a noob with this stuff. would like to have the servos connected to the mask, both connected to the same up/down axis and have the arduino/battery attached to the back of the mask and have the wires run long enough to have the thumbstick be able to be used in your pocket, and essentially would like it to function with up=lifts the eyebrows, no action=centers eyebrows and down=lowers the eyebrows, any help would be greatly appreciated

author
trentbrown (author)2016-11-23

Great tutorial, thank you!

author
Victor EmmanuelS (author)2016-11-21

This is great!

I have an idea for the push-button to keep the position:

It can be used as to set "position lock" by acting as a switch when to read the analog input from the joystick. A click would allow read the analog input (activate the servo control), a second click would save the current analog values and keep it as such (disconnecting the joystick signal). Then clicking the button would reactivate servo control. :-)

author
geeameal (author)2016-11-17

how to develop an android app that works exactly like the thumbstick(joystick) in this project? can anybody help me? i want to do this projects but i will use a smartphone and bt module. any help will be appreciated :) tnx

author
Trojank (author)2016-09-05

Yay

author
AbhishekS298 (author)2016-08-20

hey guys i want a little help for my project...........
i want to control 4 servos with 2 joystick module,( with one joystick i want to control 2 servos from each axis)

i made the servo to rotate continuous by using some help from you tube

now i want that if i push the stick up than the servo start rotating and keep rotating until i release the pressure and when i release the pressure from stick the servo should stop immediately and when i push the stick down the servo start rotating in opposite direction (same for other axis with other servo ) , speed doesn't matter for me in this case i just want to use the stick as switches like when i push up it should start rotate clockwise and when i push down it start rotate counter clockwise,

can somebody please help me with the code and connection to the arduino.

thanks in advance.. :)

author
ScienceG2 (author)2016-08-14

Is there a way for me to use a different joystick because I only have this one

1.0x0.jpg
author
Arduino Tech (author)ScienceG22016-08-16

yes you can use this just connect 5v to arduino 5v and gnd to gnd and X to analog 1 and Y to analog 2 and leave sw

author
ScienceG2 (author)Arduino Tech2016-08-17

I already bought the parallax joystick but thanks for your help

author
KylinF (author)2016-07-20

Awesome project! It only took me two tries to get it right. Although I was wondering how would you get the motors to hold position when moved? Any help is much appreciated.

author
bstacy37 (author)2016-06-19

Works great, thanks. I am using it just to pan the camera L/R - is there a way to speed up the process of the servo, to pan with relative speed and smoothness? Thanks

author
bstacy37 (author)bstacy372016-06-19

Solved - choose a better power supply for the Arduino - fast and smooth - thanks again - awesome

author
JoannaK17 (author)2016-06-13

everything's working great but i can't get it to stop spinning :(

it should not be moving when im not touching the joystick

but even at an untouched resting state it's constantly rotating

please help!

author
Standwatei (author)2016-05-05

Hey, great tutorials, thank you for posting them. I'm building a DIY camera pan/tilt head. I've applied your mouse control code and this works decently well, as long as I practice landing the ending position perfectly. The joystick solution is viable but my joystick returns to center. Normally a good thing but how could I have the last signal looped when I release the stick? as In, if the signal is moving above center it reads only those signals at it's current state or higher, until I drop the stick below center at which point the servo begins moving downward.

author
PrashantD1 (author)2016-05-05

Something wrong here, servos aren't moving. On the serial monitor I can notice the values change from 0-1023 when I move the joystick.
I have powered the servos with another power supply, 4xAA battery case. I have also grounded both the sources.

Just a clarification, this is Tower Pro SG90, Red connected to positive, Brown to Negative and Orange to PWM pin on arduino.

I have also grounded both power sources, another cable from negative or servo power source to GND (GND above the digital pins)

Kindly help.

temp_-1801292969.jpgtemp_-2116298311.jpgtemp_-737600529.jpg
author
PrashantD1 (author)PrashantD12016-05-05

Fixed :)... some issue with the breadboard, needed to push the cable hard.

author
IsidoreP (author)2016-04-19

Just connect servo left to pinconnector 3 instead of 4.....otherwise you have to change the code in line "const int servo1 = 3" to "const int servo1 = 4"

Then it will work for sure !

Kind regards

author
Moon74 (author)2015-01-12

Thanks very much Biomech75! My first project on Arduino!

Just one question, I'm using a different joystick and there is one pin called "SW"

I didn't connect it but everything works great. Do you know the function for this pin?

I will try 4 servos now :)

Cheers!!!

2 Servos + Thumbstick.jpg
author
thijs.kogels (author)Moon742015-01-14

The SW pin is used for a push button build in in the joystick, if you press on the joystick it wil activate the button.

Here i have a link to a website that maybay complain some thing about the SW pin and how to use it.

http://www.learningaboutelectronics.com/Articles/Arduino-2-axis-thumb-joystick.php

author
giantpair (author)thijs.kogels2016-01-04

seems quite different to the above code. any ideas how to combine them? I tried and failed (couldn't verify)

author
AlexandreF29 (author)giantpair2016-04-15

Try to declare your button with INPUT_PULLUP :

pinMode(buttonPin, INPUT_PULLUP);

Source : http://arduino.stackexchange.com/questions/20747/turn-on-a-light-when-there-is-an-input/20748#20748

author
bodhi2 made it! (author)2016-02-12

Nice and simple project!

Find below the code to use joystick in order to rotate the servos one step at a time:

https://github.com/bpangrati/servo_joy

2016-02-12 11.07.15.jpg
author
Ravirar (author)2016-01-06

Thanks! Awesome project, good instructions!

author
MikeG21 (author)2016-01-06

This
works great...So how do I change the code to move the servo 360 degrees in both
direction Left and Right or actual sweep of 180 degrees/175 degrees.I noticed when 180 is the value the actual
travel is 90…I would like to double that. Please help.

author
giantpair (author)2016-01-04

Can someone please make the switch (SW) work with this code? I have tried using http://www.learningaboutelectronics.com/Articles/Arduino-2-axis-thumb-joystick.php and http://www.electroschematics.com/8964/turn-on-led-button-arduino/ and have failed both times

author
onii99 (author)2015-11-30

Hi i was just wondering if this would work with a continuos servo

author
GianL (author)2015-09-27

Hi, i love this project!! I wonder how
to let the camera reach the initial position (for example x=90 Y=90) when the
joystick button is pushed.

Thank you for your help

author
PrashantD1 (author)2015-05-24

Hi, thanks a lot for this wonderful tutorial.

I am new to arduino and this being my first project, I am basically making a Rotary head for my Camera.

What I wanted was it to hold the position when I leave the Joystick.

Any suggestions?

Thanks

author
superguy911 (author)PrashantD12015-09-25

Look up while loop.

int pos = 0;

while(joy > 0){

pos++;

if(pos > 180){

pos = 180;

}

servo.write(pos);

}

obiously this code wont work on its own but if you manage to fit this code into the one given my the autor it will sum 1 to the servo variable.

author
samuel123abc (author)2015-05-29

Hello,

Thanks for creating this instructable. I was just wondering, is it possible to power these 2 servos with my arduino, won't the burn overheat, get damaged or any other thing like that, especially when I am using a joystick as well :l Otherwise, great instructable.

Samuel

author
biomech75 (author)2015-05-22

The Key on making this and other tutorials from INSTRUCTURABLES is that if it does not work, the start over again....all of us when doing an instructurable like this one wealso make mistakes so we start over and over again,,,just don't give up and HAVE FUN!!! (Bi()ME(H75

author
bryanlee (author)2015-05-18

Hi there !

I'm doing my project which involves 1 Arduino DC Motor and 1 2-Axis Joystick REV B 27800 .

Struggling to put the pieces together so that the joystick is able to control the D Motor to move front and back according to the direction of the joystick i move to. With the use of the arduino board as well.

Please Help ! :(

author
ling1602 (author)2015-05-15

author
CinthyaI (author)2015-05-13

Hi there!

I'm trying this tutoril for my project,

I'm using two servo: HS311 and HS422, A joystick (Keyes_SJoys), of course an Arduino, and I'm using a battery of 5v for the joystick.

I think i have the conections all right, but when I upload the program on the arduino, the servos start vribating, and don't respond to the joystick, i kept checking the conections on the joystick and it's all right,

What could else be?

Please help! D:

author
jonathanatkinson8 (author)2015-05-05

Well after whining like a baby to arduino on facebook and being upset with the lack of results from these instructions i deconstructed and reconstructed and finally got it. THANKS!!!!

author
mgstudio (author)2015-05-01

Looks great! Can you add a gyroscope to horizontal axis to keep it level? Or even override the horizontal axis with a joystick if you wanted to?

author
techjunkie made it! (author)2015-04-27

Hello, I'm still very new to Arduino as well. I got everything hooked up and noticed an issue in the code. The left/right worked as planned, but the up/down did not respond. After going over the wiring, I looked at the code. I found that you have pwm pin 4 for the up/down, it is set to pin 3 in the code. Once I made the adjustment with the wiring, it worked great. Thank you for taking the time to make this Instructable. I found it very easy to follow, pictures helped a lot, and was fun.

Arduino_servo_joystick.jpg
author
PRO DR MR BOB (author)2015-04-18

i used 2 100k pots insted of a joystick and a pro mini 3v insted of a arduino uno but for some reson the servos arent moveing they only make the servo sound, so i thought maby it was becuse there not getting enouf power so i attached all the + wires to RAW and used 9 volts but it still only makes the sound if u have any idea what could fix this i'd realy like the help

author
dath (author)2015-03-18

hi the joystick that i found is different ,...

could you tell me,if will it be compatable with your project??

tanx,...

Robodo-Dual-axis-Xy-Joystick-SDL350939428-1-b4fe7.jpg
author
NathanG8 (author)dath2015-04-06

Yes, I will refer to his diagram.

Vcc is +5 (Red). Gnd is 0V (Black). Xout is L/R(yellow). Yout is U/D (blue)

About This Instructable

244,167views

369favorites

License:

Bio: Evo 8, Honda K, B Engines Mods
More by biomech75:Arduino thumbstick controllerArduino+Servo+PotentiometerArduino Robotic Arm
Add instructable to: