A servomotor is also defined as a rotary actuator that allows for very fine control of angular positions.
Many servos are widely available and quite cheap. I have had nice results with a 43 R servo, by Spring Model Electronics. You can find the datasheet at http://www.sparkfun.com/datasheets/Robotics/servo-360_e.pdf.
Servos can drive a great amount of current. This means that you wouldn't be able to use more than one or two on your Arduino board without using an external source of power.
Whenever we need a way to control a position related to a rotation angle, we can use servos.
Servos can not only be used to move small parts and make objects rotate, but can also be used to move the object including them. Robots work in this fashion, and there are many Arduino-related robot projects on the Web that are very interesting.
In the case of robots, the servo device case is fixed to a part of an arm, for instance, and the other part of the arm is fixed to the rotating part of the servo.
There is a nice library that should be used at first, named Servo
.
This library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega.
By using other Arduino boards over Mega, we can figure out some software limitations. For instance, pins 9 and 10 cannot be used for PWM's analogWrite()
method (http://arduino.cc/en/Reference/analogWrite).
Servos are provided in three-pin packages:
Basically, the power supply can be easily provided by an external battery, and the pulse still remains the Arduino board.
Let's check the basic wiring.
The following diagram is that of a servo wired to an Arduino for both power supply and control:
The corresponding circuit diagram is as follows:
We are basically in a very common digital output-based control pattern.
Let's check the code now.
Here is a firmware that provides a cyclic movement from 0 degrees to 180 degrees. It is also available in the Chapter09/OneServo/
folder.
#include <Servo.h> Servo myServo; // instantiate the Servo object int angle = 0; // store the current angle void setup() { // pin 9 to Servo object myServo myServo.attach(9); } void loop() { for(angle = 0; angle < 180; angle += 1) { myServo.write(angle); delay(20); } for(angle = 180; angle >= 1; angle -=1) { myServo.write(angle); delay(20); } }
We first include the Servo
library header.
Then we instantiate a Servo
object instance named myServo
.
In the setup()
block, we have to make something special. We attach pin 9 to the myServo
object. This explicitly defines the pin as the control pin for the Servo
instance myServo
.
In the loop()
block, we have two for()
loops, and it looks like the previous example with the piezoelectric device. We define a cycle, progressively incrementing the angle variable from 0 to 180 and then decrementing it from 180 to 0, and each time we pause for 20 ms.
There is also a function not used here that I want to mention, Servo.read()
.
This function reads the current angle of the servo (that is, the value passed to the last call to write()
). This can be useful if we are making some dynamic stuff without storing it at each turn.
18.190.207.144