Servo Motor Working Principle
A servo motor is a type of motor that is used to control the angular position of a shaft. It does this by sending a series of pulses to the motor, with the length of each pulse determining the position of the shaft. The pulses are sent at a frequency of 50 Hz, which means that there is one pulse every 20 milliseconds.
The position of the shaft is determined by the width of the pulse. A pulse width of 1 ms or less will rotate the shaft to 0 degrees, a pulse width of 1.5 ms will rotate the shaft to 90 degrees, and a pulse width of 2 ms or more will rotate the shaft to 180 degrees.
Servo motors are often used in robotics and other applications where precise angular control is required. They are also used in some toys and other consumer products.
Connecting a Servo Motor to Arduino
To connect a servo motor to Arduino, you will need to connect the following three wires:
- Power: The power wire is typically red and should be connected to the 5V pin on the Arduino.
- Ground: The ground wire is typically black or brown and should be connected to a ground pin on the Arduino.
- Signal: The signal wire is typically yellow or orange and should be connected to a PWM pin on the Arduino.

Once the wires are connected, you can start writing code to control the servo motor.
Arduino Servo Motor Code
The following code will rotate a servo motor from 0 degrees to 180 degrees and back again:
#include <Servo.h>
Servo myservo;
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (int i = 0; i <= 180; i++) {
myservo.write(i);
delay(15);
}
for (int i = 180; i >= 0; i--) {
myservo.write(i);
delay(15);
}
}
This code will first attach the servo motor to pin 9 on the Arduino. Then, it will loop through the values from 0 to 180, writing each value to the servo motor. This will cause the servo motor to rotate from 0 degrees to 180 degrees.
After the servo motor has reached 180 degrees, the code will loop through the values from 180 to 0, again writing each value to the servo motor. This will cause the servo motor to rotate from 180 degrees back to 0 degrees.
The delay()
function in the code is used to slow down the rotation of the servo motor. This is necessary because the servo motor can only rotate so fast. If you remove the delay()
function, the servo motor will rotate very quickly.
Conclusion
This is just a brief overview of how to interface a servo motor with Arduino. There are many other things you can do with servo motors, such as controlling them with a potentiometer or reading their position with an encoder. For more information, please consult the Arduino documentation or other online resources.