Servo motor Interfacing and Control using Arduino
In this article, we will learn how to interface and control servo motors using Arduino Uno R3.
Arduino is an open-source electronics platform. It consists ATmega328P 8-bit Microcontroller. It can be able to read inputs from different sensors & we can send instructions to the microcontroller in the Arduino. It provides Arduino IDE to write code & connect the hardware devices like Arduino boards & sensors.
Servo motor:
It is an electric device that is used to control angular rotation. It is a rotary actuator or linear actuator. Servo motors have servomechanism. The servo mechanism helps the servo motor to control and monitor the motion of the motor. The user can control the rotation speed and position (angle) precisely.
Servo motor pins:
A servo motor has 3 pins.
- VCC ( 5 Volts )
- GND
- Signal ( Control input signal )
Components required:
- Servo motor
- Arduino Uno R3
- Jumper wires
Servo motor configuration:
- Connect the Signal pin of the Servo motor to the vacant PWM pin of the Arduino.
- Connect the VCC of the servo motor to the 5V pin.
- Connect the GND of the servo motor to the GND.
- Import the header file to the Arduino code.
- Compile and upload the code to the Arduino Uno R3.
Circuit Diagram:
Arduino code:
#include <Servo.h>
Servo servo1; // Create object for Servo motor 1
Servo servo2; // Create object for Servo motor 2
int position = 0; // Variable to store the position
void setup()
{
servo1.attach(3); // Set PWM pin 3 for Servo motor 1
servo2.attach(5); // Set PWM pin 5 for Servo motor 2
}
void loop()
{
// Rotating Servo motor 1 in Anti clockwise from 0 degree to 180 degree
for (position = 0; position <= 180; position++)
{
servo1.write(position); // Set position of Servo motor 1
delay(10);
}
// Rotating Servo motor 1 in clockwise from 180 degree to 0 degree
for (position = 180; position >= 0; position--)
{
servo1.write(position); // Set position of Servo motor 1
delay(15); // Short delay to control the speed
}
// Rotating Servo motor 2 in clockwise from 0 degree to 180 degree
for (position = 0; position <= 180; position++)
{
servo2.write(position); // Set position of Servo motor 2
delay(10); // Short delay to control the speed
}
// Rotating Servo motor 2 in Anti clockwise from 180 degree to 0 degree
for (position = 180; position >= 0; position--)
{
servo2.write(position); // Set position of Servo motor 2
delay(15); // Short delay to control the speed
}
}
Note: The direction of rotation may be varied depends on the model of the servo motor.
Output:
Online Simulation: Click here
Applications:
- Robotics arms
- Manufacturing automation