Control servo to rotate 180 degrees

Control servo to rotate 180 degrees

Overview
In this section, we will use the new method to control the servo.  We found that if the Arduino provided by the library was used, some of the servo can not accurately rotate the angle we need. Through our study different steering gears were found. Different types of steering gear has different operating frequency. So we provide the following method: adjust corresponding frequency according to different servos.
Components
– 1 * Adeept UNO R3 Board
– 1 * USB Cable
– 1 * Adeept Servo
– Several jumper wires

Principle
The servo motor has three wires: power, ground, and signal. The power wire is typically red, and should be connected to the 5V pin on the Arduino board. The ground wire is typically black or brown and should be connected to a ground pin on the Arduino board. Usually the signal pin is yellow, orange or white, and should be connected to a digital pin on the Arduino board. Note that the servo motor draws a considerable amount of power, if you need to drive more than one or two servos, you’ll probably need to power them with an extra supply (i.e. not the +5V pin on your Arduino). Be sure to connect the grounds of the Arduino and external power supply together.
Procedures
1. Build the circuit

2. Program

/***********************************************************
File name: Adeept_servo_PRO.ino
Description:   The servo motor are rotated  from 
               0 degrees to 180 degrees and from 180 degrees to
               0 degrees.
Website: www.adeept.com
E-mail: support@adeept.com
Author: Tom
Date: 2017/01/07 
***********************************************************/
int servopin = 9;    //Define servo interface digital interface 7
int angle =0;        //Angle of rotation of the servo

void servopulse(int angle)//Define an impulse function
{
  int pulsewidth=(angle*11)+500;  //The angle is converted to a pulse width value of 500-2480
  digitalWrite(servopin,HIGH);    //The servo interface level to the highest
  delayMicroseconds(pulsewidth);  //The number of microseconds of the delay pulse width value
  digitalWrite(servopin,LOW);     //The servo interface level to low
  delayMicroseconds(20000-pulsewidth); //20000 can be replaced: "frequency = 1 / (20000/1000000)"
}

void setup()
{
  pinMode(servopin,OUTPUT);//Set the servo interface as the output interface
}

void loop()
{
  for(angle=0;angle<=180;angle++){
      //Reference impulse function
      servopulse(angle);
      delay(20);
  }
//    for(angle=180;angle>=0;angle--){
//      //Reference impulse function
//      servopulse(angle);
//      delay(20);
//  }
}

3. Compile the program and upload to Arduino UNO board
Now, you should see the servo rotate from 0 to 180 degrees, and then do it in the opposite direction.

Leave a Reply