How To Optimize Servo With Arduino Uno-3 Basic Examples

IMG 20230512 163749 scaled How To Optimize Servo With Arduino Uno-3 Basic Examples

Servo motors are highly valuable devices known for their ability to accurately rotate to a specified position. An introductory guide to using servo with Arduino Uno, perfect for beginners.

A servo is a self-sufficient device used to control a mechanical system’s position, speed, or acceleration. It consists of a small electric motor, a motor driver (such as a potentiometer), and a feedback control circuit. The motor in a servo is designed to rotate between specific angles, usually 0 to 180 degrees.

It can be instructed to move to a particular angle and hold its position accurately. The control circuit receives signals from an external source, such as a microcontroller or remote control, and adjusts the motor’s position based on feedback from the sensor. This allows for precise control of the servo’s movement.

It is commonly used in various applications, including robotics, automation, remote-controlled vehicles, and other precision control systems.

Servo motors are widely compatible with Arduino boards, making them popular for robotics and other projects. Arduino provides a convenient platform for controlling servos using its built-in libraries and PWM (Pulse Width Modulation) capabilities.

What kind of signal is required as an input signal

The servo needs just one input signal from the Arduino input pin to rotate the servo arm at the desired position. A servo motor receives a control input signal from an Arduino, typically in the form of a pulse-width modulation (PWM) signal. The PWM signal consists of a square wave with a fixed frequency, where the width of the pulse within each cycle determines the position of the servo.

In a standard servo motor control system, the position of the servo’s shaft is determined by the pulse width of the control signal it receives. The control signal is divided into cycles, with each cycle lasting 20 milliseconds (ms). At the beginning of each cycle, the signal is set to HIGH. The duration of time the signal remains HIGH within the cycle, ranging from 1 to 2 milliseconds, determines the servo’s position.

Specifically, a pulse width of 1 millisecond (ms) corresponds to a position of 0 degrees, while a pulse width of 2 milliseconds (ms) corresponds to a position of 180 degrees. The 1.5 millisecond (ms) represents 90 degrees. Positions between 0 and 180 degrees can be achieved by varying the pulse width between these two values proportionally.

What will you learn from this article?

In this article, I will explain how a servo can be used with Arduino uno using three basic examples.

In the first example, you will understand how a servo can be connected to an Arduino uno board and work according to Arduino code.

The second example is an updated version of the first one. It will help you to understand how a servo can be rotated at different angles in a loop as per the code.

The third example will help you to understand the controlling of the servo using analog input provided by the potentiometer.

Example 1: How to connect the servo with Arduino uno board

Let’s get started with the first example, we need the following items:

Servo with Arduino

Circuit diagram

A servo motor has a built-in electric motor, a motor driver, and a feedback control circuit. The Servo motor has a female connector with three wired pins:

  • A yellow, white, or orange wired pin used as an input pulse or control signal pin, must be connected to the digital pin of the Arduino board.
  • A red-wired pin used for the power supply must be connected to the 5-volt pin of the Arduino board
  • the black/darkest wired pin is used as the ground, and must be connected to the GND pin of the Arduino board.
how to operate servo using arduino

According to the connection diagram, the yellow colored pin is connected to pin 5, the red wired pin is connected to the 5-volt power supply pin, and the last black pin is connected to the GND pin of the Arduino board.

Code 1

The following code will move the servo to 45, wait for 2 seconds, then turn it to 90, and wait for 2 seconds, then move to 180, and again take a pause for 2 seconds, and then go back to 45.

//move the servo from 45 degree to 90 degree to 180 degree and repeat
//for more detail about this project please visit:https://arduinounomagic.com/servo-with-arduino-uno/
//
//Copyright (C) 2007 Free Software Foundation, Inc. <[email protected]>
//
//for more projects based on Arduino uno please visit:https://www.arduinounomagic.com/

#include <Servo.h>    // Include the Servo library
 Servo servo_s;       // Declare the Servo object

void setup() {
 
servo_s.attach(5);    // Attaching servo to Pin No.5

}

void loop() {

  servo_s.write(45);   //move servo at 45
  delay(2000);         //wait for 2 sec
  
  servo_s.write(90);    //move servo at 90
  delay(2000);          //wait for 2 sec

  
  servo_s.write(180);   //move servo at 180
  delay(2000);          //wait for 2 sec

}

Example 2: How to rotate servo at different angles

For the second example, you need the following components:

Servo with Arduino

Circuit diagram

This example has the same circuit connection as the first one. According to the connection diagram, the yellow colored pin is connected to pin 5, the red wired pin is connected to the 5-volt power supply pin, and the last black pin is connected to the GND pin of the Arduino board.

servo with arduino

Code 2

Following code rotates servo from 0 degree to 45 degrees, wait for a moment to indicate the stoppage, then 46 degrees to 90 degrees, wait for a second to indicate the stoppage, then 91 degrees to 120 degrees, wait for a moment to indicate the stoppage, and then 121 degrees to 180 degrees, and again wait for one more second to indicate the stoppage, and return back to 0 degree.


//move from 0 to 45 degrees to 90 degrees to 120 degrees to 180 degrees and back to 0
//for more detail about this project please visit:https://arduinounomagic.com/servo-with-arduino-uno/
//
//Copyright (C) 2007 Free Software Foundation, Inc. <[email protected]>
//
//for more projects based on Arduino uno please visit:https://www.arduinounomagic.com/



#include <Servo.h>  // Includes servo library.
 
Servo servo_1;      // Creating Servo object.
 
int servo_position = 0;   // Storing servo position (0 degree to 180 degree)
 
void setup() 
{ 
  servo_1.attach(5);  // Attaching servo to Pin No.3 
} 
 
 
void loop() 
{ 

  for(servo_position = 0; servo_position <= 45; servo_position++) // servo move from 0 to 45
   {                                   // increment of 1 degree in each step
    servo_1.write(servo_position);          // commanding servo to reach at Servo_pos.
    delay(20);                         // waiting a bit for the servo to reach commanded position. 
   } 
  delay(1000);                         //Delay of 1 second to observe stoppage of servo. 
  for(servo_position = 46; servo_position <= 90; servo_position++)  // servo move from 46 to 90 degree 
   {                                   
    servo_1.write(servo_position);              
    delay(20);                        
   } 
  delay(1000);
  for(servo_position = 91; servo_position <= 120; servo_position++)  // servo move from 91 to 120 degree. 
   {                                  
    servo_1.write(servo_position);              
    delay(20);                        
   } 
  delay(1000);
  for(servo_position = 121; servo_position <= 180; servo_position++) // servo move from 121 to 180 degree. 
   {                                   
    servo_1.write(servo_position);               
    delay(20);                        
   } 
  delay(1000);  
  for(servo_position = 180; servo_position>=1; servo_position--)     // go back to 0 degree.  
   {                                
    servo_1.write(servo_position);               
    delay(20);                       
   } 
  delay(1000);
} 

Example 3: How to rotate the servo using a potentiometer

In this example, you will learn how a potentiometer can be used to rotate the servo using Arduino. For this example following electronic items are required:

servo with preset image scaled How To Optimize Servo With Arduino Uno-3 Basic Examples

Circuit diagram

According to the connection diagram, the yellow colored pin is connected to pin 5, the red wired pin is connected to the 5-volt power supply pin, and the last black pin is connected to the GND pin of the Arduino board.

A 10 k preset is a type of potentiometer with a resistance value of 10 kilohms (10,000 ohms). It is a variable resistor that allows you to manually adjust the resistance within its specified range by rotating its shaft or sliding its slider. Like most potentiometers, a 10k preset has three terminals – two outer terminals and a middle terminal.

  • The two outer terminals are connected to the ends of the resistive element inside the preset.
  • The middle terminal, also known as the wiper terminal, is connected to a movable contact point along the resistive element. The position of the wiper determines the effective resistance between the middle terminal and either of the outer terminals.

Connect the potentiometer to the Arduino Uno

  • Connect one end of the potentiometer to the 5V pin on the Arduino Uno.
  • Connect the other end of the potentiometer to the GND (ground) pin on the Arduino Uno.
  • Connect the middle pin of the potentiometer (variable output) to the A5 analog input pin on the Arduino Uno.
how to operate servo with potentiometer using arduino

Code 3

The following code rotates the servo at any angle between 0 degree to 180 degrees with the help of a potentiometer manually.

 
//operating servo using potentiometer  (10k preset)for analog input
//for more detail about this project please visit:https://arduinounomagic.com/servo-with-arduino-uno/
//
//Copyright (C) 2007 Free Software Foundation, Inc. <[email protected]>
//
//for more projects based on Arduino uno please visit:https://www.arduinounomagic.com/

#include <Servo.h> 
Servo servo_1;      // Creating Servo object.
 
int input_pin = A5;  // Analog Input Pin (Potentiometer)
int input_val;       // to store analog Input value (0-1023)
int servo_angle =0;  // Servo angle value (0-180 degree)
 
void setup() 
{ 
  servo_1.attach(5);  // Atteching Arduino's Pin 5 to servo. 
} 
 
void loop() 
{ 
  input_val = analogRead(input_pin);             // To read analog input value (0-1023) 
  servo_angle = map(input_val, 0, 1023, 0, 179); // Converting input value (0-1023) to servo angle (0-180)
  servo_1.write(servo_angle);                 // Commanding servo to reach servo angle 
  delay(300);                                // waiting for servo to reach commanded angle. 
}  

Output

Please check out the following output video to better understand the working of all the examples.

Hope you enjoy the article and learn something new. I tried to explain all the small details in a very easy way.

If you have any doubts or questions please let me know in the comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *