How to interface and sweep a servo motor

First of all , you need to download "servo motor library" . Then add the library into your Arduino IDE . Next Write or copy the code properly in your Arduino IDE .

Equipments that we need :

 

  1. Arduino Uno
  2. Servo motor
  3. Arduino IDE ( For programming )

 

Follow this diagram and connect the servo .

arduino-servo-motor-connection-1150x800

Code

//  www.juniormakersomu.com

// Learn arduino programming with juniormakersomu

// This code is for continuously sweep the servo betwewn 0 to 180 degree

#include <Servo.h>

Servo myservo;                            // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;                                  // variable to store the servo position

void setup() {
myservo.attach(9);                        // attaches the servo on pin 9 to the servo object
}

void loop() {
for (pos = 0; pos <= 180; pos += 1)          // goes from 0 degrees to 180 degrees
// in steps of 1 degree
{

myservo.write(pos);                   // tell servo to go to position in variable 'pos'
delay(15);                                   // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) {               // goes from 180 degrees to 0 degrees
myservo.write(pos);                                       // tell servo to go to position in variable 'pos'
delay(15);                                                        // waits 15ms for the servo to reach the position
}
}