Instructables

Arduino + 2 servos + mouse

Featured
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.
 
Remove these adsRemove these ads by Signing Up

Step 1: Materials

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

                    Hardware
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.                  

                        Software:
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

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)
musicalmike1 month ago

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 @ musical_mike@hotmail.co.uk.

thank you very much for your help guys :)

Fearce12 months ago

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

Thanks for posting this!

Alibata3 months ago
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. :-)
David972 years ago
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)  David972 years ago
great idea!!
bonzlee biomech754 months ago
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)  bonzlee4 months ago
Have you try my other tutorial - 2 servos move by a thumbstick?
I hope this works If not let me know
bonzlee biomech754 months ago
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.!
SCORN1538 months ago
http://processing.org/
SCORN1539 months ago
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

h3nrikoo11 months ago
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!
Played two days with processing and servos. Added some new features to processing interface. Available at http://www.megafileupload.com/en/file/410547/two-servos-key-mouse-rar.html

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!

https://www.youtube.com/watch?feature=player_embedded&v=LyGXzYBQ3gg :)
biomech75 (author)  Danijel0frk11 months ago
Thanxxx and I am glad that everyone here can use this tutorial to start their own projects...to the outstanding world of Arduino... ;)
Vaibhav1o11 year ago
Friend this is an awesome tutorial!! and can u plz give me the link to the process software please!!
http://processing.org/download/
biomech75 (author)  alexanderall11 months ago
Thanxxx alexanderall ;)
This is a very cool sketch! I've sitting here for 10 minutes looking at the pan/tilt-system moving! Thanks biomech75!
biomech75 (author)  Stealth01131 year ago
No Problem! This is the basics. Now use your imagination!!!
PiJo2 years ago
Hello
I connected the two servos right on schedule. The Arduino software is playing up compile, and upload.
then the Arduino software closed.
Unfortunately I can not move the servo with the mouse.
I use it a Sony VAIO Laptop:

Maybe I can give a tip whoever.
Thank you

biomech75 (author)  PiJo1 year ago
Maybe is the Arduino Software version I have been having issues with the newest version therefore I am using version 0023. Hope this helps ;
biomech75 (author)  PiJo1 year ago
did you open the process software???
after you close the arduino you copy and paste the code to the process software and click run. Then you will see a grey box with the mouse pointer there, move that to start moving the servos
biomech75 (author)  PiJo1 year ago
I was using a laptop and a usb mose during that time hope this help ;)
knazemarek1 year ago
please help me.
I would like to control 3 servo by mouse.
I don´t know how to do it.

//Arduino code:
#include

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

void setup(){
xservo.attach(14); //(analog pin 0) for the x servo
yservo.attach(15); //(analog pin 1) for the y server
zservo.attach(16);

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 = Serial.read(); // 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...
*/
break;
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
*/
xservo.write(v);
v = 0;
break;
case 'y':
yservo.write(v);
v = 0;
zservo.write(v);
v = 0;
break;

}
}
}

//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
int zpos=90;
Serial port; // The serial port we will be using

void setup()
{
size(360, 360);
frameRate(100);
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(175);
rect(0,0,360,360);
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;
zpos = y/2;
//Output the servo position ( from 0 to 180)
port.write(xpos+"x");
port.write(ypos+"y");
port.write(zpos+"y");
}

thank for response.
biomech75 (author)  knazemarek1 year ago
here is the code with some changes (I have not test this code) Let me know if it is working if not I will run some test again ;)
CODE:

//Arduino code:
#include

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

void setup(){
xservo.attach(14); //(analog pin 0) for the x servo
yservo.attach(15); //(analog pin 1) for the y servo
zservo.attach(16); //(analog pin 2) for the z servo

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 = Serial.read(); // 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...
*/
break;
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
*/
xservo.write(v);
v = 0;
break;
case 'y':
yservo.write(v);
v = 0;
break;
case 'z':
zservo.write(v);
v = 0;
break;

}
}
}

//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
int zpos=90; // and the same here
Serial port; // The serial port we will be using

void setup()
{
size(360, 360);
frameRate(100);
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(175);
rect(0,0,360,360);
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;
zpos = y/3;
//Output the servo position ( from 0 to 180)
port.write(xpos+"x");
port.write(ypos+"y");
port.write(zpos+"y");
}
bboudreau1 year ago
Just followed your instructable. Great job. I was scribbling all over a peice of cardboard within 10 minutes.

Thank you for sharing.
madc20121 year ago
Hey very nice tutorial!!
from start to finish it took me about 5 min and everything works great!!!
biomech75 (author)  madc20121 year ago
Thank you! I'm glad you like it.
I left you a comment on your other tutorial - (http://www.instructables.com/id/Arduino-2-Servos-Thumbstick-joystick/), I need to ask the same question. How can I do this with two small dc motors, servos a very noisy and my camera will pick up noise.

Here's a link to the dc motor I'm using,

http://www.servocity.com/html/90_rpm_micro_gearmotorblocks.html

Thanks........Antonio
biomech75 (author)  AntonioLopez1 year ago
For DC motors its different. You will need :( This is a test code try and tell us how does this work).
a dc motor
a transistor (NPN 2N2222) @ RadioShack
a resistor (1K Ohm) @ RadioShack
a diode @ RadioShack

Code:
int potpin = A0;
int val;

void setup() {}

void loop() {
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 225);
analogWrite(3, val);
delay(15);
}

dc motor.png
Oysteinkn1 year ago
Hi!
What would I have to do to invert the axis? ie. make moving the mouse left turn the servo in the opposite direction of what it does now.

Thanks! :D
I figured it out :)

Had to change "xservo.write(v)" to "xservo.write(-1*v+180)", and the same for yservo.

By the way, nice tutorial!
biomech75 (author) 2 years ago
my mistake ....serial port Thnax for the correction
pro2xy2 years ago
are you sure you change the number to change window size.....? I thought it was to change the serial port
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!