Arduino-Servo Library

This article details the use of the Servo library in Arduino.

The servo motor is operated by a program using "Servo", a library for servo motor control.

For other Arduino functions and libraries, please refer to the following article.

TOC

Servo Library

FunctionsParametersReturnsExplanation
attach(int pin, int min, int max)int pin: The pin number to attach
int min: Pulse width(usec) at angle 0°, Defaults to 544
int max: Pulse width(usec) at angle 180°, Defaults to 2400
Index valueAttach the Servo variable to the pin
bool attached(void)NoneAttached: true
Not attached: false
Check whether the Servo variable is attached to the pin
void detach(void)NoneNoneDetach the Servo variable from the pin
void write(int angle)int angle: Specified angleNoneRotate servo motor to specified angle
void writeMicroseconds(int uS)int uS: Pulse width in usecNoneRotate servo motor by specifying pulse width
int read(void)NoneServo motor angleReads the current servo motor angle

attach()/Attach the Servo variable to the pin

attach()/Attach the Servo variable to the pin
  • Function: attach(int pin, int min, int max)
  • Parameter: int pin⇒The pin number to attach
  • : int min⇒Pulse width(usec) at angle 0°, Defaults to 544
  • : int max⇒Pulse width(usec) at angle 180°, Defaults to 2400
  • Return: Index value

The attach() function attaches a servomotor pin number as its parameter. You can also set the pulse width for angles 0° and 180° with the parameters min and max.

attached()/Check whether the Servo variable is attached to the pin

attached()/Check whether the Servo variable is attached to the pin
  • Function: bool attached(void)
  • Parameter: None
  • Return: Attached⇒true/Not attached⇒false

The attached() function checks whether the servo motor pin number is attached by the attach() function. The return value is "true" if there is attachment and "false" if there is no attachment.

detach()/Detach the Servo variable from the pin

detach()/Detach the Servo variable from the pin
  • Function: void detach(void)
  • Parameter: None
  • Return: None

The detach() function detaches the servo motor pins attached by the attach() function and returns them to the unattached state.

write()/Rotate servo motor to specified angle

write()/Rotate servo motor to specified angle
  • Function: void write(int angle)
  • Parameter: int angle⇒Specified angle
  • Return: None

The write() function rotates the servomotor up to the angle specified by the parameter.

writeMicroseconds()/Rotate servo motor by specifying pulse width

writeMicroseconds()/Rotate servo motor by specifying pulse width
  • Function: void writeMicroseconds(int uS)
  • Parameter: int uS⇒Pulse width in usec
  • Return: None

The writeMicroseconds() function rotates a servomotor by specifying a pulse width in the parameter. The angle at which the servomotor rotates is determined by the pulse width it receives.

read()/Reads the current servo motor angle

read()/Reads the current servo motor angle
  • Function: int read(void)
  • Parameter: None
  • Return: Servo motor angle

The read() function returns the current servo motor angle.

Sample Programs (Sample Sketches)

The servo motor is operated by a program using "Servo", a library for servo motor control.

Let's share this post !
TOC