Using the Arduino Servo Library to Control Your Robots Servo Motors

 Four steps to access and use the standard arduino servo library in your sketch code:

  1.  "#include <Servo.h>" Write this at the top of your sketch before the "void setup()" function.
  2. "Servo myservo" Write this to assign a variable name to the Servo object. in the examples we use the variable name of "myservo"
  3. "myservo.attach(2)" Write this and choose the digital pin (2 thru 13) that you want to use to send the control signal to the servo. Place the attach statement inside the "void setup()" function.
  4. "myservo.write(90)" Write this and choose the angular servo position (0 thru 180) that you want to move the servo motor shaft to. Placing this inside the "void setup()" function stets the servo position on start up or reset and will not allow you change the position value after that. Placing the write statement inside "void loop()" gives the ability to change to a new position value every time the loop repeats based on sensor input, math calculations or commands received via the arduino's serial port/USB/UART pins.

 The above code examples can be copied to the Arduino IDE software and uploaded to your device to watch servo motor position control in action. try changing the position value in the servo.write() statement to see different results. You can also try "myservo.write(random(0,180)" to randomly change the servo position value. Also note that enough time must be allowed for the servo to reach its new position before a new position value is written to it. The above example uses the "delay(1000)" statement to wait for 1 second between position writes.

 To control more than one servo motor simply add steps 2 thru 4 above to your code using unique servo names as shown in the "Multi Servo Motors" code example.

 

 These servo code examples show the basic use of the arduino servo library. Check out the Arduino Servo Reference for additional details.

 

 It is important that the servo motors are connected properly to your micro controller board and powered properly. View the Robotio and Arduino wiring pin-out diagrams for more information.

 

 Study the code generated by Servo-Matic to see how Microbotlabs uses the servo library to control the Armuno and Armio robot arms.

 

 Read the Joystick Servo Control tutorial to learn how to connect and read sensor values that are then converted to servo motor position values with your arduino.

//Multi Servo Motors, Position Loop

#include <Servo.h>

Servo myservo;

Servo myservo2;

void setup()

{

  myservo.attach(2);//Signal Pin

  myservo2.attach(3);

}

void loop() {

  myservo.write(0);//Servo Position 1

  myservo2.write(20);

  delay(1000);//Travel time to new position

  myservo.write(90);//Servo Position 2

  myservo2.write(110);

  delay(1000);//Travel time to new position

  myservo.write(180);//Servo Position 3

  myservo2.write(random(0,180));

  delay(1000);//Travel time to new position

}

 

Copyright © MICROBOTLABS

Our Make & Learn Brands: MICROBOTLABS™ ARM.UNO™ ROBOTIO™ PROTIO™ ARMIO™ ANITRON™ ANITRONIC™ ARM.ONE™