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.
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.
The following components are required to build this project:
Each of these components can be bought via the link attached to them.
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.
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.
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.
Next, we create an object of the library, to be used as a reference for controlling our servo motor throughout the code.
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.
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.
The complete code for the project is available below and can be downloaded from the download section at the end of the tutorial.
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.
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.