In this post, I am going to explain how to attach a Servo motor with Arduino. I will explain about programming and how to connect Servo motor with Arduino.
You will require to install Servo library for functioning of Servo motor
#include <Servo.h> Servo s; int i = 0; void setup() { s.attach(9); } void loop() { for (i = 0; i <= 180; i++) { s.write(i); delay(20); } for (i = 180; i >= 0; i--) { s.write(i); delay(20); } }
If you analyze the above program you will see.
#include<Servo.h> statement includes a Servo library.
Servo s; declares an object of Servo type which is a class.
In setup() function, Servo’s output pin is attached at pin no. 9.
for (i = 0; i <= 180; i++)
{ s.write(i);
delay(20);
}
The above for loop makes move Servo motor 0 to 180 degree.
for (i = 180; i >= 0; i–)
{
s.write(i); delay(20);
}
The above for loop makes move Servo motor 180 to 0 degree.