Controlling a Servo with Arduino

arduino

Very simple basics of building a circuit to control a servo using Arduino and PWM

For this you will need:

Arduino Board – I’m using a Duemilanova ATMEGA328
Arduino Sketch software – I’m using version 0015
Arduino Servo Library found here save it to lib/targets/libraries if you don’t already have it
A Servo that requires no more than a 5V supply I’m using a Futuba S3113
A Potentiometer (A dimmer switch)
Breadboard & Connector wires
Previously used Arduino, at least to do basic blinking LED

So we’re going to assume that you’ve already used the sketch software and uploaded at least your first sketch to the Arduino board.

First lets start with just connecting a servo to the board.

I’ve used some connecting wires to connect between the servo pins to the ardunio board. The red wire is positive, the black negative/ground and the white is what relays the feedback/instructions.

The red wire goes to the +5v pin, the black goes to the GND pin on the Arduino and I’m going to put the white wire to pin 9 PWM.

Thats it! So now you just need to upload the code below to the board. You can use this example sketch also provided with Arduino

[ad#Google Ad in content]

Arduino Servo Sketch

// Sweep
// by BARRAGAN

#include //include the servo libary

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position

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

void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}

[ad#Google Ad in content]
So you should see the servo move!

If you got this far lets now add in a potentiometer to control the servo, so when you turn the potentiometer it will turn the servo.

The potentiometer, it has 3 pins like the servo, the outside pins are for the supply (+/-) and the middle pin is for the control/ feedback.

First lets setup the breadboard for the circuit, all red wires are positive, black are negative and the 2 white wires are the feedback to the Arduino chip.

Here’s the final setup I did below:

arduino-servo-potentiometer-final

Near the start of the circuit I’ll add the potentiometer and at the end of the cicuit I’ll put the servo.

arduino-potentiometer

arduino-servo

The white servo wire still goes to digital pin 9 PWM and the potentiometer white wire goes to the Analog 0 pin

So if you have all that setup then we just need to use the example sketch below to get it working:
[ad#Google Ad in content]

// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott

#include

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
}

[ad#Google Ad in content]

13 Comments

  • can you control multiple servos using multiple pot by uisng the same idea?

    • yup. the servo library can support 8 servos I think. You just need to use PWM via each pot to control the servos. You’re only limited by the Arduino board but then you can use shift registers if you dont want the servos moving at the same time.

    • hi!i am new in programming.i want to control three servos with three potentiometers.with the first pot i want to move manually the first 2 servos in the same direction.with the second pot i want to control manually the same 2 servos in the opposite directiona and with the third pot i want to control the 3rd servo manually. the range of all the servos is 0degr to 180 degr.can anyone help me plzzzz

  • So I understand how to program the servo and have done so, so that it starts at 0 goes to 170 then back to 0. Now I have a magnetic counter where whenever a piece of metal passes in front of it it closes the circuit. I want to add this to my system so that the closed circuit can signal the servo to go from 0 to 170 and then when it closes again it signals the servo to go from 170 back to 0. How can I program it to do this?

    • Have a look at hall effect sensors instead and have a magnet that passes past the sensor, the hall effect signal can be sent to the Arduino to then on detection execute the desired code.

  • Thanks Larry I will look into the hall effect sensor. My problem though is the actual programming to make the servo do what I need it to do. I am a novice to this type of programming and I need to complete my project in the next week. Do I link it to one of the other pins and tell the audrino to move the servo when the other pin detects a signal? If so how do I do this?

    • The servo interface isn’t too bad. wire in one servo to the Arduino, 2 of the wires are for power, and the other goes to any digital output pin on the Arduino.

      The code is fairly simple to tell the servo what position to set to:

      #include //include the servo libary
      Servo myservo;
      int pos = 0; // variable to store the servo position (0-179)

      void setup()
      {
      myservo.attach(9); // attaches the servo on pin 9 – can be any number
      }

      void loop()
      {
      myservo.write(pos); // sets the servo
      }

      So if you do a hall effect sensor, you then look for the a signal from that (which I have no idea) and that’ll be an if statement in that loop():

      void loop()
      {
      if (hallEffectSensor==true){
      myservo.write(180);
      } else {
      myservo.write(0);
      }
      }

  • I’ve come up with this. I can’t test it until the hall effect sensor comes in but could you tell me if this will work.

    #include //include the servo libary

    Servo myservo; // create servo object to control a servo
    // a maximum of eight servo objects can be created

    int pos = 0; // variable to store the servo position
    int inPin = 8; // select the input pin for the Hall Effect Sensor
    int val = 0; // variable to store the value coming from the sensor
    int mean = 508;
    int sensitivity = 20;

    void setup()
    {
    myservo.attach(9); // attaches the servo on pin 9 to the servo object
    {
    pinMode(myservo, OUTPUT); // declare the ledPin as an OUTPUT
    }
    }

    void loop()
    {
    for(pos = 0)
    val = analogRead(inPin); // read the Hall Effect Sensor
    if (val > (mean + sensitivity) || val (mean + sensitivity) || val < (mean – sensitivity))
    {
    digitalWrite(myservo, pos -= 170); // goes from 170 degrees to 0 degrees
    myservo.write(pos); // tell servo to go to position in variable ‘pos’
    }
    }

  • Hi I am trying to use this servo setup but I am trying to make the servo only trigger when the hall effect sensor goes off. I have the following function but I am getting an error when trying to setup the servo as the output for the hall effect sensor. Can you please help me correct this.

    #include //include the servo libary

    Servo myservo; // create servo object to control a servo
    // a maximum of eight servo objects can be created

    int pos = 0; // variable to store the servo position
    int inPin = 8; // select the input pin for the Hall Effect Sensor
    int val = 0; // variable to store the value coming from the sensor
    int mean = 508;
    int sensitivity = 20;

    void setup()
    {
    myservo.attach(9); // attaches the servo on pin 9 to the servo object
    {
    pinMode(myservo, OUTPUT); // declare the ledPin as an OUTPUT
    }
    }

    void loop()
    {
    for(pos = 0)
    val = analogRead(inPin); // read the Hall Effect Sensor
    if (val > (mean + sensitivity) || val (mean + sensitivity) || val < (mean – sensitivity))
    {
    digitalWrite(myservo, pos -= 170); // goes from 170 degrees to 0 degrees
    myservo.write(pos); // tell servo to go to position in variable ‘pos’
    }
    }

  • sorry I didn’t copy my whole loop equation:
    void loop()
    {
    for(pos = 0)
    val = analogRead(inPin); // read the Hall Effect Sensor
    if (val > (mean + sensitivity) || val (mean + sensitivity) || val < (mean – sensitivity))
    {
    digitalWrite(myservo, pos -= 170); // goes from 170 degrees to 0 degrees
    myservo.write(pos); // tell servo to go to position in variable ‘pos’
    }
    }

  • hi sorry just wanted to ask is it possible to control one servo with 2 sensors

  • Hey Larry,
    I have a question. I have a JRservo DS8711, which I need to control with some specific tolerances. Max input voltage is 6. I am very ignorant to the way the Arduino works and would like some info to how this thing could help me accomplish this. I would like to supply the servo with the max voltage it can take simple to get the max oz/in out of it. What Arduino would you recommend?

    Thank you for and help.
    Jesse

  • I’ve modified this code to include an auto calibrate button, to ensure that you get accurate control of the servo. The code is quite long, but you can find it here:

    http://www.jacob-unwin.com/servo-control-via-potentiometer/

Join the Discussion

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>