Servo Motor Control with Motor Shield



Hobby servos are the easiest way to get going with motor control. They have a 3-pin 0.1" female header connection with +5V, ground and signal inputs. The motor shield simply brings out the PWM output lines from Arduino pins 9 and 10 to two 3-pin headers so that its easy to plug in and go. They can take a lot of power so a 9V battery wont last more than a few minutes!
The nice thing about using the onboard PWM is that its very precise and goes about its business in the background. You can use the built in Servo library.

Hardware Reqiured:

- Arduino
- Motor Shield (Used like Adafruit)
- Servo Motor
- Battery & Power Jack

Recommended site to buy the required hardware:
https://www.banggood.com/?p=CS120478587752016125

AFMotor Library Description

Download AFMotor Library (GitHub)
https://github.com/adafruit/Adafruit-Motor-Shield-library

To install, click on Downloads in the middle of the page, select Download as zip and uncompress the folder.
Rename the folder to AFmotor (check that the renamed folder contains the .cpp and .h files) and install into the Arduinosketches/libraries folder.

Get The Code :
#include <AFMotor.h>
#include <Servo.h>              // Add library
Servo name_servo;               // Define any servo name

int servo_position = 0 ;

void setup() {
  
name_servo.attach (10);          // Define the servo signal pins

}

void loop() {
 for (servo_position = 0; servo_position <=180; servo_position +=1){

    name_servo.write(servo_position);
    delay(10);
  }

  for (servo_position=180; servo_position >= 0; servo_position -=1){

    name_servo.write(servo_position);
    delay(10);
  }
}