Step 3: New code

Now that you know your servo works, you can begin to incorporate sensors into the mix. the first thing you should do is use a potentiometer. Use anything between 10 and 100k. keep the servo attached as it was using the sweep example. attach the top pin on the pot to 3.3v on the arduino. Connect the bottom pin to ground on the board. Connect the center of wiper pin of the pot to A0 (the first analog pin) on arduino. go to "open" on the IDE again. Go to servo and open "Knob". compile the code and upload it to your board.  When the program is running you will be able to control the position of the servo with a potentiometer. If you cannot find the code, copy this:

// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop()
{
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
}

 
Remove these adsRemove these ads by Signing Up
nilved says: Jan 28, 2012. 12:45 AM
could you hook up the arduino with multiple servos and pots to control them and if so can you post a comment with the code
A.C.E. in reply to nilvedFeb 28, 2012. 12:51 PM
While Higgs is getting more servos, Ill try to help you understand a bit. When you create the servo object (Servo myservo;) this is creating one servo object with the name of "myservo". If you wanted 3 servos, you would need three of those lines, but with different names, such as:

Servo myservo1;
Servo myservo2;
Servo myservo3;

you would also need more potentiometers, so youd need to add more potpins, and have the potentiometers hooked up to their respective pins, I think that would look like this:

int potpin1 = 0;
int potpin2 = 1;
int potpin3 = 2;

where potpin1 is what we are calling analog pin 0, but will control myservo1. You also need more variables to store the data from the potpins, so make those too.

int val1;
int val2;
int val3;

the numbers will correspond with the servos of the same number.

now you just need to add the commands for checking all 3 servos into the loop, which would look like this:

void loop()
{
val1 = analogRead(potpin1); // reads the value of potentiometer 1
val1 = map(val1, 0, 1023, 0, 179); // scale it
myservo1.write(val1); // sets servo 1's position according to the scaled value
val2 = analogRead(potpin2); // reads the value of potentiometer 2
val2 = map(val2, 0, 1023, 0, 179); // scale it
myservo2.write(val2); // sets servo 2's position according to the scaled value
val3 = analogRead(potpin3); // reads the value of potentiometer 3
val3 = map(val3, 0, 1023, 0, 179); // scale it
myservo3.write(val3); // sets servo 3's position according to the scaled value
delay(15); // delays for 15ms to let the servos catch up
}

I think this would work, but someone check over my work because I'm a complete noob at arduino coding. Someone with an arduino and 3 servos should write this and see if it works :p
Tariq802 in reply to A.C.E.Mar 19, 2012. 9:15 AM
before you can start reading and writing you also need to attach the servo objects to their pins ie:

void setup() {
myservo1.attach(9); //attach servo 1 to pin 9
myservo2.attach(10); //attach servo 2 to pin 10
myservo3.attach(11); //attach servo 3 to pin 11
}

etc.
Tariq802 in reply to Tariq802Mar 19, 2012. 9:21 AM
you're also going to want to use a power supply separate from the arduino board if you're using a lot of servos, otherwise you might break something. it could probably handle 3 servos but i definately wouldn't go with more than that without isolating the power supply
Higgs Boson (author) in reply to nilvedJan 28, 2012. 2:46 PM
Yes you can. I'll try and post the code in a few days.
Higgs Boson (author) in reply to Higgs BosonFeb 8, 2012. 6:12 AM
Sorry. This may take longer because I need to get more servos (all of mine are being used in projects right now).
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!