Basic Library
Digital I/O
Analog I/O
Advanced I/O
Time
Math
Trigonometry
Random Numbers
Bits and Bytes
Interrupts
Serial Comm.
Standard Library
Ethernet
Servo Motor
Stepping Motor
Character LCD
SPI
I2C (Wire)
SD Card
SD (File Operations)
Periodic Operation
Clock (RTC)
PPG (Pulse Generator)
WiFiEsp
ICS (Serial Servo)
FreeRTOS Tips
Servo Motor
This library allows you to control RC (hobby) servo motors. Servos have integrated gears and a shaft that can be precisely controlled. Standard servos allow the shaft to be positioned at various angles, usually between 0 and 180 degrees. Continuous rotation servos allow the rotation of the shaft to be set to various speeds. To use, specify #include <servo.h>
. You will need to create Servo myservo or a similar instance.
attach
- Description
- Initializes and attaches the Servo output to a pin.
- Syntax
- servo.attach(int pin)servo.attach(int pin, int min, int max)
- Parameters
- pin: The number of the pin to which the servo is attached
min: The pulse width, in microseconds, corresponding to the minimum (0-degree) angle on the servo (defaults to 544)
max: The pulse width, in microseconds, corresponding to the maximum (180-degree) angle on the servo (defaults to 2400). - Returns
- None
write
- Description
- Writes a value to the servo to set the shaft angle, controlling the shaft accordingly. The pulse for moving the shaft to that orientation is output from the pin specified in attach.
- Syntax
- servo.write(int angle)
- Parameters
- angle: The value of the angle to write to the servo, from 0 to 180
- Returns
- None
writeMicroseconds
- Description
- Writes a value in microseconds (uS) to the servo, controlling the shaft accordingly. On a standard servo, this will set the angle of the shaft. On standard servos a parameter value of 1000 is fully counter-clockwise, 2000 is fully clockwise, and 1500 is in the middle.
- Syntax
- servo.writeMicroseconds(int us)
- Parameters
- us: The value of the pulse width in microseconds.
- Returns
- None
read
- Description
- Read the current angle of the servo.
- Syntax
- int servo.read()
- Parameters
- None
- Returns
- pulse width [us]
attached
- Description
- Check whether the servo variable is attached to a pin.
- Syntax
- bool servo.attached()
- Parameters
- None
- Returns
- True if the servo is attached to pin; false otherwise.
detach
- Description
- Detach the servo variable from its pin. This stops the pulse width output from the specified pin.
- Syntax
- servo.detach()
- Parameters
- None
- Returns
- None
Sample Program
This sample rotates the servo motor connected to pin 9.
#include <Arduino.h>
#include <servo.h>
#define INTERVAL 1000
Servo servo;
void setup()
{
servo.attach(9);
}
void loop() {
servo.write(0);
delay(INTERVAL);
servo.write(180);
delay(INTERVAL);
}