In previous lesson, we have learned about how to make an attractive Knight-Rider and Dark-Spot circuits. Now you know how to control output devices with Arduino. You know how to control brightness with Arduino. Today, you are going to learn how to control Servo Motor with Arduino.
This is an image of a Servo motor.
So, you can identify what is servo motor now. We can learn
what is the use of servo motor now. Servo motor is a motor that comes with
built-in gears and circuits. Gears are for decreasing the speed of servo motor
and circuit if for tuning servo motor to given angle and control speed. Simply,
servo motor is a motor that gives you ability to change direction of the motor
to given angle with given speed. There are different types of servo motors. The
weight that can be carry by servo motor changes according to servo motor type.
Most common servo motor for Arduino projects is “Micro Servo Motor”. Micro
servo can carry up to 1.2Kg weight. Micro servo is about few grams. So, servo
motor can do much. Let’s see how to control servo motor.
You have to make circuit like this to do a project with servo
motor. Make sure you connected your servo motor to PWM pin of Arduino. You have
learned about PWM pins in Arduino in pervious lessons. PWM pin is defined as ~ this
symbol. Always yellow wire is the signal wire in every servo motor. Brown wire
is the Ground wire and red is VCC wire. You have to give 5v to turn on the
micro servo.
So first we are going to turn the servo motor to 180
degrees. Here is the code for it.
First of all, you have to install servo library to make
servo motor project. Below is the link to download servo library. But recommended
option is installing the library through Arduino library manager. We learnt how
to install library in previous lessons. Press the code in the GitHub and then
press the ‘Download Zip’ button.
So, we included the library in the first line using ‘#include’
function. In the second line, we created object named myservo. It must be created
an object in servo projects. You can give any name instead of myservo. In void
setup, we initialized that our servo motor is in 9th pin. We use “myservo.write();”
function to turn servo motor to specific degree. If you want to turn servo
motor to 90 degrees, you can type like this ”myservo.write(90);”.
So, in this code, we are going to turn servo motor to different
degrees. So, you can easily understand how this code is going to work.
So, this is about servo motors. Servo motor is a long topic.
We can learn how to control the speed of a servo motor in next post. Also, you
can learn for loop in next post. If you have any kind of question, please be
kind to comment it below.