| ||
|
Shopping Cart
|
| Home | MP3 Player | 8051 Tools | All Projects | PJRC Store | Site Map |
|
You are here:
Teensy
| Search PJRC |
|
|
Servo LibraryThe Servo or PWMServo library allows you to connect servo motors, commonly used in remote control airplanes and other vehicles.
Hardware RequirementsServo can control up to 12 or 24 individual motors, using any pins.PWMServo can control up to 3 individual motors, using specific pins.
Both Servo and PWMServo use hardware timers, causing specific PWM pins to become unusable with analogWrite(). PWMServo is much more tolerant of interrupt latency that Servo. Libraries which disable interrupts, like NewSoftSerial, are incompatible with Servo, but can be used with PWMServo.
![]() Most Servo motors require +5 volt power, at considerable current. Usually the black wire connects to ground and the red wire to +5 volts. Please check the specifications for your motors to be sure. A few very small motors, like the ones above, can run from USB power. Direct connection to a PC or Mac or powered USB hub can provide 500 mA of current. Please be careful not to connect too many motors which could have a combined usage higher than 500 mA. Larger motors will need a separate power supply. Usually the motor power supply ground much connect to the Teensy/USB ground pin, but the +5 volt lines remain separate, so the motors get their power from the external power supply, not USB. Basic UsageServo myservo;Create an instance of the Servo library. You will need to create an instance for each motor, giving each motor a unique name of your choice. myservo.attach(pin);Specify the pin number where the motor's control signal is connected. Position Controlmyservo.write(angle);Move the servo motor to a new angle, between 0 to 180 degrees. Some servo motors are not capable of a full 180 degree travel or have stability problems at the far ends of this range. Example ProgramThe sweep example program slowly moves a servo back and forth. This program can be opened from the File -> Examples -> Servo menu. incremented, and written back.
// Sweep // by BARRAGAN <http://barraganstudio.com> #include <Servo.h> 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(20); // attaches the servo on pin 20 } void loop() { for(pos = 10; pos < 170; pos += 1) // goes from 10 degrees to 170 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 } } Timer UsageThe Servo library uses a timer. Actually, a timer is used for each 12 motors. When the Servo library takes control of a timer, the PWM pins normally controlled by that timer will no longer function correctly.This table can help you avoid using PWM pins which require the timers used by Servo.
Interrupt LatencyServo uses precisely timed interrupts to generate the control signals to many servo motors. Normally you do not need to be concerned about these interrupts. However, if your program disables interrupts, or you use a library which disables interrupts for substantial periods of time, Servo's signal accuracy can be degraded. Typical problems involve servo motors making small, unintended movements at infrequent, seemingly random times.Libraries which disable interrupts for long times, likely to cause substantial problems:
Arduino Issue #146Teensyduino contains interrupt-safe implementation of digitalWrite() and pinMode(), so use of these functions or libraries such as LiquidCrystal that many heavy use of these functions should not impact Servo on Teensy.However, some libraries directly manipulate pins without disabling interrupts, which is likely to interfere with Servo. Libraries believed to contain issue #146 include:
More DetailsPlease refer to the official Servo library documentation for more details. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||