Using the SG90 Servo Motor With an Arduino

Hi guys, today, I will show you how to use a servo motor with an Arduino. This tutorial is ideal for beginners because it is easy and it gives them the foundation to build interesting projects like robots for which servos are commonly used.

Servo motors are high torque motors which are commonly used in robotics and several other applications due to the fact that it’s easy to control their rotation. Servo motors have a geared output shaft which can be electrically controlled to turn one (1) degree at a time. For the sake of control, unlike normal DC motors, servo motors usually have an additional pin asides the two power pins (Vcc and GND) which is the signal pin. The signal pin is used to control the servo motor, turning its shaft to any desired angle.

SG90 Servo motor

For this tutorial, we will be using the popular SG90 servo motor and our goal will be to rotate the servo motor from one end to the other.

Servo’s have high current requirement so when using more than one servo motor with the Arduino, it is important to connect their power connections to an external power supply as the Arduino may not be able to source the current needed for the servo. Since we will be using just one servo in this tutorial its fine to power it with an Arduino.

Required Components

The following components are required to build this project:

  1. SG90 Servo
  2. Cheap Arduino Uno
  3. Wires
  4. Breadboard

Each of these components can be bought via the link attached to them.

Schematics

The schematics for this project is quite simple as we will be connecting just the servo motor to the Arduino.

Servo motors generally have three pins/wires, this includes the VCC, GND, and the Signal pin. The Signal pin is the one used to feed the control signal from the microcontroller to the servo, to get the servo rotate to a particular angle.

Connect the Servo to the Arduino as shown in the schematics below.

Schematics

For emphasis, the connection is further described below.

SG90 Servo – Arduino

VCC(Red wire) - 5V
SIG(yellow/orange) - D8
GND(Black/Brown) - GND

The signal pin was connected to the digital pin 8 of the Arduino because it is a PWM pin. Servo directions are sent from the microcontroller to the servo motor as PWM pulses.

With the connection all done, we can now proceed to write the code for the project.

Code

The code for this project is quite easy thanks to the very comprehensive and concise servo.h library developed by the Arduino team to facilitate the use of servo motors in Arduino projects. The library makes it easy to turn the servo at different angles using a single command. The library comes pre-installed in the Arduino IDE removing the need for us to download and install.

We start the code for the project by including the libraries that we will use which in this case is the servo.h library.

#include <Servo.h>

Next, we create an object of the library, to be used as a reference for controlling our servo motor throughout the code.

Servo servo;

With this done, we proceed to the void setup() function. we start the function by attaching the servo object created to pin D8 of the microcontroller, after which we center the servo, turning it to zero degrees.

void setup() {
  servo.attach(8);
  servo.write(angle);
}

With that done, we are ready to move the servo in any direction we desire, and we will be doing this under the void loop function. Thanks, to the servo.h library all we need to do, to rotate the servo to an angle we desire, is to pass the desired angle as an argument into the servo.write() function. To demonstrate this, a for-loop was used to turn the servos at several angles in one direction and another loop was used to turn the servo back to where it started.

void loop() 
{ 
 // scan from 0 to 180 degrees
  for(angle = 10; angle < 180; angle++)  
  {                                  
    servo.write(angle);               
    delay(15);                   
  } 
  // now scan back from 180 to 0 degrees
  for(angle = 180; angle > 10; angle--)    
  {                                
    servo.write(angle);           
    delay(15);       
  } 
}

The complete code for the project is available below and can be downloaded from the download section at the end of the tutorial.

#include <Servo.h>

Servo servo;
int angle = 10;

void setup() {
  servo.attach(8);
  servo.write(angle);
}


void loop() 
{ 
 // scan from 0 to 180 degrees
  for(angle = 10; angle < 180; angle++)  
  {                                  
    servo.write(angle);               
    delay(15);                   
  } 
  // now scan back from 180 to 0 degrees
  for(angle = 180; angle > 10; angle--)    
  {                                
    servo.write(angle);           
    delay(15);       
  } 
}

Demo

Copy the code above and upload to your Arduino and servo motor setup, after a few time, you should see the servo motor start turning as shown in the gif below.

Demo

That’s it for this tutorial guys, the code above can be expanded in several ways for use in different projects involving servo motors, what cool thing will you be building with the servo motor? feel free to share via the comment section.

The video tutorial for this project can be watched on youtube.

Downloads

Comments

  • Pierre Van Overmeire

    In your article on page http://www.electronics-lab.com/project/using-sg90-servo-motor-arduino/ is listed at “REQUIRED COMPONENTS”: Distance Sensor.
    But this sensor is not connected and how it works.

    2 months ago
    • Odunlade Emmanuel

      Hi Pierre, Sorry about that, this was meant to be the first part of a tutorial that uses the other components mentioned up there. Will correct ASAP. However, we have a tutorial on the use of the distance sensor (HC-SR04) located here ( http://www.electronics-lab.com/project/arduino-distance-meter-ultrasonic-sensor-hc-sr04-nokia-5110-lcd-display/ ).

      2 months ago
      • Pierre Van Overmeire

        Hi Emmanuel,

        The -distance-meter-ultrasonic-sensor is a verry good thing, but what I need is Is not really that.
        Actually, it is the combination of the servo and sensor (HC-SR04). may also IR, PIR or Phototransistor.
        The intention is if the sensor is activated to start the servo, when the sensor is no longer active the servo goes back to its original place and stops.
        The distance that the servo in my case should do is 7200 full throttle round trip (± 337 cm).
        The servo separately works for this time but but non-stop.

        2 months ago

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

RELATED PROJECTS

By continuing to use the site, you agree to the use of cookies. more info

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close