electroSome

Interfacing Servo Motor with Arduino Uno

In this tutorial we will learn how to interface servo motor with Arduino Uno. Servo Motor is an electrical linear or rotary actuator which enables precise control of linear or angular position, acceleration or velocity. Usually servo motor is a simple motor controlled by a servo mechanism, which consists of a positional sensor and a control circuit. Servo motor is commonly used in applications like robotics, testing automation, manufacturing automation, CNC machine etc. The main characteristics of servo motor are low speed, medium torque, and accurate position.

Components Required

Hobby Servo Motor

Servo Motor

Specifications

Working

The hobby servo motor which we use here consists of 4 different parts as below.

Potentiometer is connected to output shaft of the servo motor helps to detect position. Based on the potentiometer value, the control circuit will rotate the motor to position the shaft to a certain angle. The position of the shaft can be controlled by varying the pulse width provided in the PWM input pin. Gear assembly is used to improve the torque of the motor by reducing speed.

Circuit Diagram

Interfacing Servo Motor with Arduino Uno – Circuit Diagram

Description

Program

#include <Servo.h>

Servo servo;
int angle = 10;

void setup() {
    servo.attach(3);
    servo.write(angle);
}
void loop() 
{ 
    // rotate from 0 to 180 degrees
    for(angle = 10; angle < 180; angle++)  
    {                                  
        servo.write(angle);               
        delay(15);                   
    } 
    // now rotate back from 180 to 0 degrees
    for(angle = 180; angle > 10; angle--)    
    {                                
        servo.write(angle);           
        delay(15);       
    } 
}

Code Explanation

Functioning

Wire the circuit properly as per the circuit diagram and upload the program. You can see that the servo motor is rotating as per the program. Please see the video below for more details.

Output

Conclusion

Hope you understood about the functioning of the servo motor with the help of Arduino programming. You can modify this code for your robotic applications and have fun. Please feel free to comment below if you have any doubts.