Wednesday, February 8, 2012

Playing with Arduino


Learning how to use an Arduino board is a straight forward process if you follow the tutorials on Arduino page (http://arduino.cc/en/Tutorial/HomePage). You just need some basic programming skills and some basic electronic knowledge; the tutorials put everything on a very simple way.

The Arduino Environment is a higher level code based on C++ for microcontrollers; there are many libraries that cover the low level setup. For example if you use the PWM (Pulse With Modulation) library, you just have to specify the duty cycle of it; frequency of the signal is a fixed value, you can't change it easily unless you go the C++ code. This implies that Arduino code is less flexible but it also makes it a lot (really a lot!) easier to use.

After doing a lot of the examples on the tutorials, my objective was to control the servo motors to be used on my robot. So I used the servo library from Arduino to control the position of 1 servo by manipulating a potentiometer, then the same for 2 servos and then for all the 5 servos.

As I mentioned on the previous post, the Arduino Uno board can source up to 2 servos; so for sourcing all the 5 servos I used an external 5V power supply. Be careful that if you use a wall adapter power supply, not to buy any cheap one because it can be very inaccurate on the voltage it says to provide.

Then I created a costume sequence with the servos, that's a bit trickier since you have to code each servo position separately, thus you have to know where every servo should be at every moment of the sequence. This exercise is very useful since it's something pretty similar than controlling the robotic arm in an open loop project.

You can take a look at how the servos worked on the following video:


Next step will be to build the mechanical part of the robot and create some sequence for it, so that the arm can show off.


14 comments:

  1. hi im a rookie. i tried to control 5 servos with my arduino but it didnt work. i want to power up the servos externally but i dont know how to do it. im using 5 push buttons and a pot knob to control the servos. And what battery should i use. and how to connect it with servo and arduino.


    Operating Voltage : 4.8-6.0V

    PWM Input Range : Pulse Cycle 20±2ms, Positive Pulse 1~2ms

    STD Direction : Counter Clockwise / Pulse Traveling 1500 to 1900µsec

    Stall Torque : 3 Kgf.cm(41.3 oz/in) at 4.8V, 3.2 Kgf.cm(44 oz/in) at 6V

    Operating Speed : 0.2 sec/ 60° at 4.8V, 0.18 sec/ 60° at 6V at no load

    Weight : 38g (1.27 oz)

    Size : 41.3*20.3*38.7*48.5*10

    Plug Available : FUT, JR

    Special Feature : Heavy Duty Plastic Gears, Economy Servo

    this is the servo spec

    what battery should i use to control 5 servos of above spec.

    ReplyDelete
  2. Hi, I recommend you to first use a external power source (connected to the wall). Once you are able to control all the servos, you can try with batteries

    ReplyDelete
  3. Sure, the code to move those 5 servos is:


    #include

    Servo myservo1; // create servo object to control a servo
    Servo myservo2;
    Servo myservo3;
    Servo myservo4;
    Servo myservo5;

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

    void setup()
    {
    myservo1.attach(9); // attaches the servo on pin 9 to the servo object
    myservo2.attach(10);
    myservo3.attach(11);
    myservo4.attach(5);
    myservo5.attach(6);
    }

    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)
    myservo1.write(val); // sets the servo position according to the scaled value
    myservo2.write(val);
    myservo3.write(val);
    myservo4.write(val);
    myservo5.write(val);
    delay(15); // waits for the servo to get there
    }

    ReplyDelete
  4. Bonjour,
    pouvez-vous poster le code qui permet de faire la sequence fixe en modalité sweep?
    J'essaie de faire la même chose sans delay mais j'aurai besoin d'aide.
    Roberto

    ReplyDelete
  5. Certes, il est:

    #include

    Servo myservo1; // create servo object to control a servo
    Servo myservo2;
    Servo myservo3;
    Servo myservo4;
    Servo myservo5;

    int counter = 0;
    //int decreaser = 180;
    int pos1=0;
    int pos2=0;
    int pos3=0;
    int pos4=0;
    int pos5=0;
    boolean goingup = true;

    void setup()
    {
    myservo1.attach(9); // attaches the servo on pin 9 to the servo object
    myservo2.attach(10);
    myservo3.attach(11);
    myservo4.attach(5);
    myservo5.attach(6);
    }

    void loop()
    {
    pos1= counter;
    pos2= counter-45;
    pos3= counter-90;
    pos4= counter-135;
    pos5= counter-180;

    if (pos1<0) {
    pos1=0;
    } else if (pos1 > 180) {
    pos1=180;
    }

    if (pos2<0) {
    pos2=0;
    } else if (pos2 > 180) {
    pos2=180;
    }

    if (pos3<0) {
    pos3=0;
    } else if (pos3 > 180) {
    pos3=180;
    }

    if (pos4<0) {
    pos4=0;
    } else if (pos4 > 180) {
    pos4=180;
    }

    if (pos5<0) {
    pos5=0;
    } else if (pos5 > 180) {
    pos5=180;
    }

    myservo1.write(pos1); // sets the servo position according to the scaled value
    myservo2.write(pos2);
    myservo3.write(pos3);
    myservo4.write(pos4);
    myservo5.write(pos5);
    delay(15); // waits for the servo to get there

    if (counter==360) {
    goingup = false;
    }else if (counter==0){
    goingup= true;
    }

    if (goingup==true){
    counter++;
    }else {
    counter--;
    }
    }

    ReplyDelete
  6. how many servo motors arduino can control in the same time? thx

    ReplyDelete
  7. sir external power suppy to five servo motor but servo is not working
    what is circuit for external power suppy to servo motor and arduino please sent the complete circuit for arduino and five servo motor which external power supply to sachinbhargava9493@gmail.com

    ReplyDelete
  8. either code does not work... does after that include in the first line, it must be something?

    ReplyDelete
  9. #include

    Of Course!!, My fault, Sorry!!

    ReplyDelete
  10. #include Servo.h with > and <

    ReplyDelete
  11. AND HOW would control 5 SERVOMOTORS WITH A SENSOR CNY70 TAKING THIS AS SENSOR DISTANCE WHILE SOME OBJECT TO THIS SENSOR THE IMMEDIATE RESPONSE SERVOS TURN ... .. PLEASE APPROACHES

    ReplyDelete
  12. AND HOW would control 5 SERVOMOTORS WITH A SENSOR ULTRASONIC TAKING THIS AS SENSOR DISTANCE WHILE SOME OBJECT TO THIS SENSOR THE IMMEDIATE RESPONSE SERVOS TURN ... .. PLEASE APPROACHES

    ReplyDelete
  13. You could give me a diagram of how to connect all 5 servos to power? please

    setsunahsu5566123@gmail.com

    ReplyDelete