Connect the orange wire of servo to pin-0. If your Servo Motor doesn’t work properly. Connect its orange wire at pin number: 3, 5, 6, 9, 10 or 11 of Arduino Board and then change the code line as i.attach(0) or i.attach(5) and so on…

The Connection Diagram

The Code

/*
 * Basic Servo Motor Control Program
 * Vidyasagar Academy
 * www.vsa.edu.in
 * Date: 10 July, 2020
 */
 
#include <Servo.h> // include the Servo library 

Servo i; // create a servo variable ‘i’

void setup() 
  { 
     i.attach(0); // connect the orange wire of servo to pin-0 
     // If your Servo Motor doesn’t work properly, 
     // connect its orange wire at pin number 
     // 3, 5, 6, 9, 10 or 11 of Arduino Board
     // and then change the code line as i.attach(3)
     // or i.attach(5) and so on...
  }

void loop()
  {  
     i.write(0); // resets to initial position
     delay(2000);    
     i.write(90); // rotates by 90 degree
     delay(2000);     
     i.write(45); // rotates in opposite direction by 45 degree
     delay(2000);
     i.write(180); // rotates by 90 degree
     delay(2000);   
     i.write(90); // rotates back by 90 degree
     delay(2000);
   }

Leave a Reply