Arduino - Servo.attach()
Description
Attach the Servo variable to a pin. Note that in Arduino 0016 and earlier, the Servo library supports only servos on only two pins: 9 and 10.
Syntax
servo.attach(pin)
servo.attach(pin, min, max)
Parameters
- servo: a variable of type Servo
- pin: the number of the pin that the servo is attached to
- min (optional): the pulse width, in microseconds, corresponding to the minimum (0-degree) angle on the servo (defaults to 544)
- max (optional): the pulse width, in microseconds, corresponding to the maximum (180-degree) angle on the servo (defaults to 2400)
Example
Hardware Required
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
Arduino Code
#include <Servo.h>
Servo myservo;
void setup() {
myservo.attach(9);
}
void loop() {}
※ NOTES AND WARNINGS:
When calling Servo.attach() function, the servomotor rotates to the last value set by Servo.write() or Servo.writeMicroseconds() function, or 90 ° by default. Therefore, To position the servomotors at a certain position upon initialization, use Servo.write() or Servo.writeMicroseconds() function right before calling Servo.attach().
See Also
※ ARDUINO BUY RECOMMENDATION
Arduino UNO R3 | |
Arduino Starter Kit |