Call Us Now
0242159130 | 0546096840
Up to 2% reward

For new all Customers

Delevery schedule

We deliver  the same day through VIP bus parcel Service in Ghana

Discount on order gift

Save ¢10.00 when you spend ¢600+! 

How to Control Servo motor

How to  Control Servo motor

In this tutorial, we will introduce a new electronic device(Servo) to you,and tell you how to control it with the Arduino UNO. 

Hardware required

1 * Arduino UNO

1 * USB Cable 

1 * servo motor

Principle of operation

  • Servo motor

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. 

  • Servo Library 

This library allows an Arduino board to control RC (hobby) servo motors. Servos have integrated gears and a shaft that can be precisely controlled. Standard servos allow the shaft to be positioned at various angles, usually between 0 and 180 degrees. Continuous rotation servos allow the rotation of the shaft to be set to various speeds.

  • Key function

attacth()

Attach the Servo variable to a pin. Note that in Arduino 0016 and earlier, the Servo library supports only servos on only two pins: 9 and 10

Syntax:

servo.attach(pin)

servo.attach(pin, min, max)

parameters

servo: a variable of type Servo

pin: the number of the pin that the servo is attached to min (optional):

the pulse width, in microseconds, corresponding to the minimum (0-degree) angle on the servo (defaults to 544) max (optional):

the pulse width, in microseconds, corresponding to the maximum (180-degree) angle on the servo (defaults to 2400) 

Experiment  code

//File name:   Experiment 15

/********************************

Description:  The servo motor are rotated to 15 degrees, 30

             degrees, 45 degrees, 60 degrees, 75 degrees,

             90 degrees, 75 degrees, 60 degrees, 45 degrees,

             30 degrees, 15 degrees, 0 degrees, and then from

             0 degrees to 180 degrees and from 180 degrees to

             0 degrees.

*******************************/

//Website: www.okuelectronics.com

//E-mail: support@okuelectronics.com

//Author: Okudzeto osei prince

//Date: 2019/08/18

#include <Servo.h>

Servo myservo;//create servo object to control a servo


void setup()

{

  myservo.attach(9);//attachs the servo on pin 9 to servo object

  myservo.write(0);//back to 0 degrees

  delay(1000);//wait for a second

}


void loop()

{

  myservo.write(15);//goes to 15 degrees

  delay(1000);//wait for a second


  myservo.write(30);//goes to 30 degrees

  delay(1000);//wait for a second.33


  myservo.write(45);//goes to 45 degrees

  delay(1000);//wait for a second.33


  myservo.write(60);//goes to 60 degrees

  delay(1000);//wait for a second.33


  myservo.write(75);//goes to 75 degrees

  delay(1000);//wait for a second.33


  myservo.write(90);//goes to 90 degrees

  delay(1000);//wait for a second


  myservo.write(75);//back to 75 degrees

  delay(1000);//wait for a second.33


  myservo.write(60);//back to 60 degrees

  delay(1000);//wait for a second.33


  myservo.write(45);//back to 45 degrees

  delay(1000);//wait for a second.33


  myservo.write(30);//back to 30 degrees

  delay(1000);//wait for a second.33


  myservo.write(15);//back to 15 degrees

  delay(1000);//wait for a second


  myservo.write(0);//back to 0 degrees

  delay(1000);//wait for a second

  for (int num = 0; num <= 180; num++)

  {

    myservo.write(num);//back to 'num' degrees(0 to 180)

    delay(10);//control servo speed

  }

  for (int num = 180; num >= 0; num--)

  {

    myservo.write(num);//back to 'num' degrees(180 to 0)

    delay(10);//control servo speed

  }

}

 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.

Add Comment