Working Of Servo Motor In Arduino Mega 2560


In this article I will explain the working of a servo motor. It will work according to the rotating method and the system can be used for door locking purposes.
Figure 1: Servo
  • It is small and cost-effective.
  • It is used for small robotics and radio controls.
  • The servo first pin can be connected to the Gnd
  • The second pin can be connected to the Vcc
  • The Third pin can be connected to the digital pin 09 of the Arduino Mega 2560.
  1. #include <Servo.h>    
  2. Servo myservo;  // create servo object to control a servo    
  3. void setup()    
  4. {    
  5.   myservo.attach(9);  // attaches the servo on pin 9 to the servo object    
  6. }    
  7. void loop(){    
  8.   myservo.write(0);     // sets the servo at 0 degree position    
  9.   delay(1000);          // waits for the servo to get there    
  10.   myservo.write(90);    // sets the servo at 90 degree position    
  11.   delay(1000);          // waits for the servo to get there    
  12.   myservo.write(180);   // sets the servo at 180 degree position    
  13.   delay(1000);          // waits for the servo to get there    
  14.   myservo.write(90);    // sets the servo at 90 degree position    
  15.   delay(1000);          // waits for the servo to get there    
  16. }    
  • The servo can work in the condition of the degree.
  • It can first rotate in the 0 degree
  • Then 90, 180, and it will automatically return to 90 degree.
Figure 2: Output
Read more articles on Arduino:

Recommended Ebook

Printing in C# Made Easy

Download Now!
Similar Articles