Roboelectrixx

Control Servo Motor with Arduino Uno

Let’s take a look into a simple interfacing project this time. This is actuator interfacing with Arduino Uno and the actuator being servo motor, specifically SG90 servo motor. SG90 is a lightweight (just 9g) and tiny servo motor which has quite good output toque. We can use Arduino IDE to code this servo and control its movements precisely. We can rotate 180 with this servo motor.

Working of the Project

This project uses SG90 servo motor interfaced with Arduino Uno which is programed to turn the servo motor from 0 degrees to 180 degrees and back to 0 degrees.

Components required

You can find the following electronic components in our website.

Powering SG90 servo motor

For demo purposes, with zero load on the servo motor, we are powering the servo motor using Arduino 5V pin. But it is important to keep in mind that the motor should be powered separately. This servo motor has input voltage of 4.8V to 6V DC. It is recommended that the servo motor should be powered externally (using a dedicated power supply) and the voltage should be within the accepted range. The maximum current draw from Arduino is 0.8A only. So when we use an external power supply, it will make sure that the Arduino board won’t be damaged due to excess current draw.

The overshooting/undershooting problem with SG90s

There is a common problem when dealing with SG90 (or even MG90S) that is the overshooting or undershooting. This is a problem has a bit to do with Control Systems. In general, we can say, the systems that are overdamped miss the target value, that causes the “undershoot”. This means, the servo would not really reach 0 to 180 degrees or other specified value. Whereas those systems that are underdamped go over the target. This causes the situation to “overshoot”. This is when the servo motor exceeds the specified degree and sweeps more area than it is supposed to do.

Servo Motor

There are a couple of fixes available online for this overshoot/undershoot problem. You could use a better servo motor like “Tower pro MG 995” servos. This is not a micro servo like SG90 but it is more precise and it can also deliver more power. There are other servo motors that are used for model aircraft building; they are known to be more precise. They give very good results but are quite expensive.  If you really want to use SG90 servo motor only and get precise degree turn, then, consider the following points to get better results:

  • Power the servo motor properly according to its specifications.
  • Buy a different servo motor or try with various servo motors.
  • Edit its header file “Servo.h” and see if you can fix the problem by adding a feedback loop.

Servo Control Circuit Diagram

Servo Control Circuit Diagram

The circuit connections for this project are very simple as the servo motor has only 3 pins. The red wire of the servo goes to 5V pin of Arduino Uno. The Black wire of the servo goes to Arduino Uno’s ground pin (GND). And the yellow wire (called the control pin of servo) goes to Arduino pin 8. This completes the circuit connections of the servo motor with Arduino Uno.

Code Explanation

#include
Servo servo;
void setup() {
// put your setup code here, to run once:
servo.attach(8);
servo.write(0);
delay(2000);
}
void loop() {
// put your main code here, to run repeatedly:
servo.write(180);
delay(1000);
servo.write(0);
delay(1000);
}

First, we need to include a library called “Servo.h” to be able to control various servo motors. If you don’t already have this library in your Arduino IDE, then you can go to “tools” à “Manage Libraries…” and type “Servo” in the Library Manager and install the one from “Michael Margolis, Arduino”.

Next, we declare a variable called “servo”. In void setup function, we use the servo.attach function to tell the Arduino board that the control pin of the servo motor is attached to pin 8 of Arduino (the function attaches the servo variable to the pin). The servo.write function is used to tell the servo the degree to which it should turn. At the beginning the default state of servo is considered as zero degree we keep this as origin position that is zero degrees. So we write servo.write(0).  Then a delay function is used to create a delay of 2ms.

Next, in void loop, we use the servo.write function again to tell the servo to turn to 180 degrees and the delay function will hold this position for 1ms. Then the servo is instructed again to go back to 0 degrees, as we had initialized before. The delay function will hold this position for 1ms. This is repeated until the power is disconnected or servo is disconnected.

Leave a Reply