

To see the demonstration of the above code, upload the code to Arduino. This will position the servo arm according to the values held in the angle variable that will first vary from 0-180 degrees and then 180 to 0 degrees by using a for loop. Here we are passing the ‘angle’ variable as a parameter inside this function. Inside the loop() function, we will move the servo motor’s arm clockwise and anti-clockwise using the write() method on the servo object. Notice this is the Arduino pin that is connected with the PWM pin of the servo motor. Inside the setup() function, we will attach digital pin10 with the servo object.

The ‘angle’ variable holds the current position of the servo motor’s arm. Servo myloop // create servo object to control a servo Then we will create an object of this library called ‘myloop.’ This will be used to control the servo motor. Myloop.write(angle) // move servo in opposite directionĭelay(20) // waits 20ms between servo commandsįirstly we will include the built-in Servo.h library. Myloop.attach(10) // attached the servo on pin 10 to the servo objectįor (angle = 0 angle = 1 angle -= 1) // goes from 180 degrees to 0 degrees Int angle = 0 // variable to store the servo position The following code is written for calculated angle position control of servo motor using Arduino UNO with the Servo.h library.
#SG90 SERVO MOTOR ARDUINO HOW TO#
This basic sketch will show us how to control a servo motor’s position with specific or calculated angle. The width of the pulse has a direct relation with the angular position of the motor.įor SG90 type servo motors, the pulse width of 1ms crossponds to 0-degree rotation, 1.5ms to 90 degree and 2ms to 180 degree rotation. That means the control signal pulse should be applied to the PWM pin every 20ms duration. The frequency of the PWM signal should be 50Hz. To control rotation, we apply pulses to the PWM pin of the servo motor. The position of the servo rotator is directly related with the width pulse applied to the PWM signal of the motor. How to control Servo motor rotator movement? You just have to call a function used in servo.h library and its as simple as that. So you don’t need to write lengthy code for servo motor. Fortunately Arduino IDE already has a built-in library (servo.h ) for servo motor control.

We can calculate the servo motor’s position by varying the duration of pulses to servo with the help of Arduino Uno R3. A pulse of duration 1 millisecond causes the servo motor to move one end and duration of 2 millisecond causes the motor to move other end. However, the main disadvantage of servo motor is its lower speed and low power limitation as compared to other motors.Ī servo motor responds to changes in duration of pulses. So the main advantage of servo motor is that it doesn’t require any interfacing circuit. H bridge is used to rotate motor either in clock wise or anti clock wise direction. Servo motor can not move with continuous motion unless feedback potentiometer is connected. H bridge is to control the position of motor (clock wise or anti clock wise).Pulse to voltage converter is for feedback voltage to potentiometer.Output shaft is used to connect with physical objects.Gears are used to control position in degrees.Servo motor consists of following parts or in other words complete servo motor package consists of following components : Servo motors are very easy to interface with Arduino because they have built-in motor controllers. This table briefly describes all three pins: Wire of motor Mostly microcontrollers or development boards have onboard 5 volts supply which we can use to power SG90 servos. Only 5 volts of power signal is required to power this motor. Vcc and ground, as their name suggests, are the power supply pins which are used to power servo motor. On top of that, it requires only one output pulse signal to control its movement.

Moreover, it is small enough that it can easily fit into your robotics ARM or obstacle avoidance robotics projects. It can not move continuously but rotates up to 180 degrees and each step can be of maximum 90 degrees. SG90 is a low cost and high output power servo motor. Servo motor have many applications in robotics and industry for position based motion control system. It can easily control physical movement of objects due to its position controlled feature. Servo motor is a position controlled motor. Similarly in robotics, servo motor control mechanism is very useful to control position of robotics. Movement of objects can be controlled by controlling motion of motors. After that, we will look at how to interface it with Arduino and control its movement. Firstly, we will see an introduction of the servo motor. In this guide, we will learn about SG-90 servo motor and how to use them with Arduino UNO.
