Introduction: Arduino + 2 Servos + Mouse

Picture of Arduino + 2 Servos + Mouse
Here is a tutorial on how to make 2 servos move with a mouse using the Arduino board.
There is not a simple tutorial than this one and I have search the web looking for one with no result.
This one is simple and easy for everybody to follow. Enjoy! ;)
Dont forget to vote for me on the Arduino Challenge!!

Note: This tutorial is for those who are using the Arduino already. But If you haven't just yet with this tutorial you can.

Step 1: Materials

Picture of Materials

You will need the following: (not in a particular order)

1. Arduino Board (my version is UNO).
2. 2 servos Parallax (Radio Shack).
3. Web Cam (optional) to see the movement.
4. 2 rubber bands to hold the Webcam in position.
5. Bread Board to make any connections easy.
6. 1 cable tie
7. Something to hold the servos in place.
8. A computer.                  

1. Arduino Software (to upload the code to move the 2 servos).
2. Process Software (to move the servos using the mouse)

Step 2: Connections

Picture of Connections

Before you upload the code or use the Process sofware. You must connect
the servos using the picture.
Be advice that I label  the servos so I can hook them the right way.
Also this works when connecting the webcam  to them, you can see witch one
goes up & down and witch one goes right & left.
Follow the diagram for more help.
I have use a bread board to make the connections easy.
Connections: (each servo)
Black to GND
Red to 5V
Yellow or White to Analogs 0 and Analog 1
(Use the bread board to provide 5v to each servo)

Step 3: Arduino Software

Picture of Arduino Software

After all the connections were made please double check them always.
Connect the Arduino Board to the computer. (USB cable)
Open the arduino Software
a) copy and paste the code in the arduino sketch
b) Always verify any code you upload, this saves you time.
c) After you verify the code, proceed to upload it to the Arduino board.
d) Close the Arduino Software.

Here is the code: ( I have tested this code several times before I pasted it here

//Arduino code:
#include <Servo.h>

Servo yservo;  Servo xservo; // servos for x and y
//set initial values for x and y
int ypos = 0;
int xpos= 0;

void setup(){
  xservo.attach(14); //(analog pin 0) for the x servo
  yservo.attach(15);  //(analog pin 1) for the y server
  Serial.begin(19200); // 19200 is the rate of communication
  Serial.println("Rolling"); // some output for debug purposes.

void loop() {
  static int v = 0; // value to be sent to the servo (0-180)
  if ( Serial.available()) {
    char ch =; // read in a character from the serial port and assign to ch
    switch(ch) { // switch based on the value of ch
      case '0'...'9': // if it's numeric
        v = v * 10 + ch - '0';
           so if the chars sent are 45x (turn x servo to 45 degs)..
           v is the value we want to send to the servo and it is currently 0
           The first char (ch) is 4 so
           0*10 = 0 + 4 - 0 = 4;
           Second char is 4;
           4*10 = 40 + 5 = 45 - 0 = 45;
           Third char is not a number(0-9) so we  drop through...
      case 'x': // if it's x
       ....and land here
       where we send the value of v which is now 45 to the x servo
       and then reset v to 0
        v = 0;
      case 'y':
        v = 0;

Step 4: Processing Software

Picture of Processing Software

Leave the Arduino Board connected to the computer.
Open Processing Software.

a) Copy and paste the code.(there is a value that you can paly with is in line 17 :port = new Serial(this, Serial.list()[0], 19200); The [0] in bold is the value you can change and it is for the size of the box to control the servos you can put 0,1,2,3 but I prefer 0 because is the right size for me.
b) Click Run.
c) If you follow everything correct you will see a gray box
    next to the Processing Software. Move the mouse pointer
    to that area to start moving the servos.(green is for up & down, red is for right & left).
Note: The box is very sensitive so it means that when you finish paying with the servos
          you should put the two marks in the middle and disconnect the board.
          If not then the servos will keep running and it would damage them unless you have a 
          fully 360 rotating servo.  


//Processing code:
import processing.serial.*;       

int xpos=90; // set x servo's value to mid point (0-180);
int ypos=90; // and the same here
Serial port; // The serial port we will be using

void setup()
  size(360, 360);
  println(Serial.list()); // List COM-ports
  //select second com-port from the list (COM3 for my device)
  // You will want to change the [1] to select the correct device
  // Remember the list starts at [0] for the first option.
  port = new Serial(this, Serial.list()[0], 19200);

void draw()
  fill(255,0,0); //rgb value so RED
  rect(180, 175, mouseX-180, 10); //xpos, ypos, width, height
  fill(0,255,0); // and GREEN
  rect(175, 180, 10, mouseY-180);
  update(mouseX, mouseY);

void update(int x, int y)
  //Calculate servo postion from mouseX
  xpos= x/2;
  ypos = y/2;
  //Output the servo position ( from 0 to 180)

Step 5: Troubleshooting

Picture of Troubleshooting

Common problems of this project:
1. The servos do not move after follow all the directions. 
A. Double check the connections, If not then the servos don't work.
2. The code do not compile on Arduino Software.
A. Double check that at the end were the last key } is there is no space.
3. When copy code to Processing Software  and click Run there is an error.
A. This happens because you have the arduino Software open. After upload the code to the board close it the click run
     or start the Processing Software part again.
4. I change the :port = new Serial(this, Serial.list()[0], 19200);  value to 1,2 or 3 and there is an error.
A. Then leave the value to [0] and it will work.
5. How to provide 5V to both servos at the same time.
A. Use the bread Board for this. (look at the picture).

Any other queations  please leave a comment and I will be glad to help you. ;)


ibyte8bits made it! (author)2017-11-02

Thanks for clear instructions and code. Discovered my serial port on Linux was number 32. Nice to see you thought of listing the ports.

skumar231 (author)2017-05-28

HI there i make this

do wan't to see the video click the link

peraz91 (author)2016-10-17

Arduino Mega does not work, the problem may be the mega?

Sziszka90 (author)2016-09-07

I used your code in a project. I would like to control 6 servos by 6 knobs through serial. I tried with only one servo, but it works with every knobs. I identify the servos by letters like a,b,c,d,e,f. ....any idea? Can I use multiple port.write() function? If anybody can help I would send my code.

EricD8 (author)2015-10-16

runs great through serial moniter but running it on micro i had to move the servos to 8 and 9

processing code doesnt like me for some reason though

doesnt want COM10 and says there is an error at line 11

Standwatei (author)EricD82016-04-30

i also got an error on line 11. did you ever resolve this by chance?

AC DC (author)2016-04-29

How do i choose Com-Port?
COM1 and COM5 are listed in the console, but i can't choose either of them

zoubir.santouh (author)2016-03-30


zoubir.santouh (author)2016-03-30


RileyD8 (author)2016-03-11

Why are analog pins used and can other pins be used instead?

scalpshifter (author)2016-01-10

Hi - thank you so much for teaching me a lot about servo control via Processing and Arduino.

I am building a GUI for a scientific instrument using CP5 in Processing and standardanalogfirmata on the arduino side. Is there a way to integrate your servo code with the safirmata code? I've tried a few experiments but they all failed. I can control the servos but they do not behave nearly as ideally using safirmata as they do with your arduino sketch and the servo.h include - I need to use the firmata.h include for the other controllers - any advice would be much appreciated! Thanks in advance. Eric.

ajain73 (author)2015-12-27

this is a nice project

and please send me the code for same project but with adxl 335(accelerometer)

it would help me a lot


please send on this email (

Poelemoel made it! (author)2015-11-18

This is great, it works really good! I am on my way creating a etch a sketch so this was really helpful. Thanks ;)

AmazingL (author)2015-10-25

Hi! biomech75, u can do it with dc servo??

biomech75 (author)AmazingL2015-11-11

yes but you need the motor shield for that and they are not that precise

MohammedM32 (author)AmazingL2015-11-03


rcandido1 made it! (author)2015-09-22


agou tentando ler na entrada do Joystick ....para ler esses dados

anto pV (author)2015-08-15

my project involve measuring the distance to an object. i want my servos to point at the object that i want to measure the distance to. is there any way i can superimpose the image from a camera so that i can see the object on the processing screen, and when i place my mouse over the object the servos point at it?

dlanday (author)2015-06-30

my servo's run when I implement the code from the Arduino IDE. But, they continuously run without stopping when the code is run. I can even type 0 or 45x etc into the serial monitor to make the servos run faster. the gui box pops up when i run the code in Processing, but it doesn't give me control over the servos, they just continue to spin around. Do you have any idea what Im doing wrong? thanks for the great project idea by the way

imran iftikhar (author)2015-02-16

sir, kindly inform me soon, because i am working on my final year project. I am working on a robotic arm control by a mouse.the mouse is directly connected to arduino uno not to computer.

imran iftikhar (author)2015-02-16

sir kindly inform me that,can i connect mouse to arduino uno directly or not.

ShrobonB (author)2014-12-19

Thank you a lot for this tutorial . It worked effortlessly .

The question i have is can you just modify the code so that my servo's remain stable at all position . I mean the point that you mentioned about spoiling the servo's .

TylerS2 (author)2014-12-15

how can i change the port in processing software

TylerS2 (author)2014-12-15

how can i change my port in processing software

TylerS2 (author)2014-12-15

how can i change my port in processing software

TylerS2 (author)2014-12-15

how can i change my port in processing software

TylerS2 (author)2014-12-15

how can i change my port in processing software

TylerS2 (author)2014-12-15

how can i change my port in processing software

t3rm1t (author)2014-11-13

Do you know how should i change the code if i have continuous servo?

BallscrewBob (author)2014-07-01

Great project but running the bare processing code on a mega 2560 the servos wont move.

If I just type x and y values eg. x10 y10 etc. into IDE serial monitor (not running processing) it will move the servos to required position so my issue is in the processing software. Using processing 2.2.1 and IDE 1.5.6 r2

Tried changing the serial value from 0 to 1 to 2 an still no joy.

Have ports 1,3,5 and my mega is on port 5 as my UNO uses port 3

Can i manually set the port some other way as I think that is my issue ?

biomech75 (author)BallscrewBob2014-09-10

I will try and I will let you know

BallscrewBob (author)biomech752014-09-10

Now working in an XP VM just fine. Just didnt like win 7 x64 for some reason.

musicalmike (author)2014-02-23

thank you soooooo much for this tutorial, IVE made it and it works absolutely perfectly!!!! your amazing.

i do have one question to ask.

have you been able to come up with a code to control servos with the computer keyboard? ive looked on google for codes but they have nothing so i thought i would ask you.

if you do, would you mind emailing me or posting it or if anyone else has any ideas @

thank you very much for your help guys :)

biomech75 (author)musicalmike2014-09-10

SURE!!! ill try a new code for it ..... and I will let you know

dylanpahina (author)2014-06-20


Great project! I am real novice, and i want to implement your design to move x and y tracks for a 3d printer, and i was wondering would this work by using stepper motors(12v 4wire) and if so what would the circuit look like? and would the code change much?

thanks in advance, any direction would be so helpful!

biomech75 (author)dylanpahina2014-09-10

yup dylanpahina but the power source must be an external one because the 12v power required by those servos

Fearce1 (author)2014-02-04

I used this example to control a remote Helicopter via servos to handheld remote.

Thanks for posting this!

Alibata (author)2013-12-31

Hello sir Biomech75! I want to ask for your help. How can I put the video frames from the webcam (as I plan to use a webcam) in the same box in the processing software where the mouse is being moved for servos positioning? i will use it for my project in school.
Thank you sir! I would appreciate any help. :-)

David97 (author)2012-03-07

Very nice, I thought of connecting it with a x-bee and bluetooth modem to a computer and then mounting it on a rc car.

biomech75 (author)David972012-03-07

great idea!!

bonzlee (author)biomech752013-12-04

Good day.! We are having our thesis(Computer controlled taser gun) right now, we are in need of a program that will control 3 servos via computer individually. The 1st servo must be in charge of horizontal movement, then the 2nd for vertical movement and the 3rd one for triggering the taser gun. We do appreciate your help and advice Sir. Thank u for ur kind consideration :-)

biomech75 (author)bonzlee2013-12-05

Have you try my other tutorial - 2 servos move by a thumbstick?
I hope this works If not let me know

bonzlee (author)biomech752013-12-09

I've seen ur tutorial - 2 servos move by a thumbstick Sir it was very nice :-)
But as much as possible Sir we would like to control the 3 servo by using the keypad of the computer. What I mean Sir, is that we would like to use ur great idea of controlling 2 servos by the mouse(horizontally & vertically) then the 3rd servo w/c is for triggering the gun will be controlled by computer keypads like the command "ENTER" or by right clicking(pressing) the mouse. Thank you very much Sir.!

SCORN153 (author)2013-08-03

SCORN153 (author)2013-06-30

Got the Arduino software...Where do you download the Process Software (to move the servos using the mouse)???

I can't find the Processing program for download anywhere

h3nrikoo (author)2013-05-15

Thank you so much for the tutorial, it worked perfectly. However, I was trying to change the x-axis servo to a 360 degree servo and it just kept spinning. Can you please help me change the code for Alduino and Processing?
(I would want it to spin from degree 0 to 360 or close, but not all the way around. If this is not possible with the mouse control, is there a way to "convert" the 360 degree servo to a 180 degree servo?)
On forehand thank you!

Danijel0frk (author)2013-04-11

Played two days with processing and servos. Added some new features to processing interface. Available at

The BIG plan is to make several servo pairs with lasers/high powered leds, and via FFT library construct light show :)

Big thanks to biomech75! :)

biomech75 (author)Danijel0frk2013-05-09

Thanxxx and I am glad that everyone here can use this tutorial to start their own the outstanding world of Arduino... ;)

Vaibhav1o1 (author)2013-04-02

Friend this is an awesome tutorial!! and can u plz give me the link to the process software please!!

alexanderall (author)Vaibhav1o12013-04-28

About This Instructable




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