Hello. In this article, you will find answers to your searches such as servo motor control with arduino, arduino servo motor.

ARDUINO SERVO MOTOR


Servo motors are electronic components that we can move between 0 and 180 degrees. There are many different types according to the needs. It is evaluated according to the materials it is made of or how much load the servo motor can undergo. In this project, I will use SG90 Servo Motor, which is generally used in small jobs. You can use more powerful engines according to your needs. However, there are important points to be considered here. How many volts does the motor you use?, how many amps it draws under maximum load or idle? etc.Since these chapters will be a bit long and this is not our topic in this article, I am passing this section for now. If you do not have enough knowledge, do not try to control powerful motors or more than one motor with Arduino for now. You can break your Arduino board. Now let's see how to use SG90 Servo Motor with Arduino UNO step by step.

Step 1: Required Components for Arduino Servo Motor


  • SG90 Servo Motor
  • Arduino UNO


Step 2: How to Use a Servo Motor with Arduino


PWM pins on Arduino are used for servo motor control with Arduino. Right next to these pins there is the (~) sign. You can find those pins by looking at this sign. When you buy a servo motor, there are usually three cables soldered on it. SG90 servo motor cables have three different colors. Features of these cables;

  • Brown (GND)
  • Red (5V)
  • Orange (PWM Pin)

Step 3: Arduino Servo Motor Connection




Set up your circuit according to the above scheme. 

Yellow > Orange
Red > Red
Black > Brown

Step 4: Arduino Servo Motor Code


When using components on Arduino, there may be situations that require you to add a library. Servo motor one of the components of it. When using servomotor with Arduino, you need to add library. 

Adding a Library to the Arduino IDE


I will use the library upload manager in the Arduino IDE in this project. I will explain with pictures how to add the library in order to control the Servo motor with Arduino.




Sketch > Include Library > Manage Libraries




On the screen that opens, there is a search section as seen above. We type Servo there and search. In the second part seen in the photo, we do the installation by clicking the install of the part titled Servo. Adding servo motor library is now complete. 

All comments required as a comment line are available. The codes and explanations required to control Servo Motor with Arduino are below.

#include <Servo.h>   // We included the servo library in our project
Servo servoM;        // we created the object and named it servoM.
int position = 0;    // we created a variable.

void setup() {

  servoM.attach(9);  // We indicated to which pin we connected the engine
}

void loop() {

// We made it go forward between 0 and 180 degrees with the For loop..
// we set it to increase by 1 degree at the end of each 20ms.

  for (position = 0; position <= 180; position += 1) { 
                                        
    servoM.write(position);                  // We printed the variable "position", which we determined as the angle value of the servo motor, to the servo motor.
    delay(20);                               // We added a 20 ms delay so that our motor can go to the target angle.
  }
 
 for (position = 180; position >= 0; position -= 1) { 
    servoM.write(pos);              
    delay(20);                       
  }
}

Servo motor control with Arduino is that simple. Hope to see you soon.