Pantech.AI

How to Interface Servo Motor with Arduino Uno

Overview of Servo Motor

A servo motor is an electrical device designed for precise control of angular movement. It is commonly used in applications requiring accurate motion control, such as operating a robotic arm.

The rotation angle of a servo motor is controlled by applying a PWM (Pulse Width Modulation) signal.

By adjusting the width of the PWM signal, the rotation angle and direction of the motor can be modified.

For more detailed information on servo motors and their usage, refer to the “Servo Motor” topic in the Sensors and Modules section.

Connection Diagram of Servo Motor with Arduino

Interfacing Servo Motor With Arduino UNO

Servo Motor Example using Arduino

Controlling the Position of a Servo Motor Using a Potentiometer

In this project, we will use the Servo library provided with the Arduino IDE.

The library includes two examples that we will be working with.

  • To open the Knob example, navigate to File > Examples > Servo > Knob.
  • To open the Sweep example, go to File > Examples > Servo > Sweep.

Note: The Servo examples can be found under the Examples section for any board, located below the Built-In Examples. You may need to scroll down to locate them.

https://www.arduino.cc/en/reference/servo

Code For Servo Position Control Using Potentiometer and Arduino

/*
 Controlling a servo position using a potentiometer (variable resistor)
 by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>

 modified on 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Knob
*/

#include <Servo.h>

Servo myservo;  /* create servo object to control a servo */

int potpin = 1;  /* analog pin used to connect the potentiometer */
int val;    /*  variable to read the value from the analog pin */

void setup() {
  Serial.begin(9600);
  myservo.attach(9);  /* attaches the servo on pin 9 to the servo object */
}

void loop() {
  val = analogRead(potpin);            /* reads the value of the potentiometer (value between 0 and 1023) */
  Serial.print("Analog Value : ");
  Serial.print(val);
  Serial.print("\n");
  val = map(val, 0, 1023, 0, 180);     /* scale it to use it with the servo (value between 0 and 180) */
  Serial.print("Mapped Value : ");
  Serial.print(val);
  Serial.print("\n\n");
  myservo.write(val);                  /* sets the servo position according to the scaled value */
  delay(1000);                         /* waits for the servo to get there */
}

Functions Used

Servo myservo
  • This creates an object called myservo of the Servo class.
myservo.attach(pin)
  • This function attaches the servo object to a specific pin. The pin refers to the pin number to which the servo is connected.
myservo.write(angle)
  • This function sets a value for the servo, controlling the position of the shaft. The angle can range from 0 to 180 degrees.
map(value, fromLow, fromHigh, toLow, toHigh)
  • This function is used to map a number from one range to another. It converts a “value” within the range of “fromLow” to “fromHigh” into an equivalent value within the range of “toLow” to “toHigh.” In this process, “fromLow” maps to “toLow,” “fromHigh” maps to “toHigh,” and intermediate values are adjusted accordingly.

Code For Sweeping The Shaft Of Servo Back And Forth

/* Sweep
 by BARRAGAN <http://barraganstudio.com>
 This example code is in the public domain.

 modified 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>

Servo myservo;  /* create servo object to control a servo */
/* twelve servo objects can be created on most boards */

int pos = 0;    /* variable to store the servo position */

void setup() {
  myservo.attach(9);  /* attaches the servo on pin 9 to the servo object */
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { /* goes from 0 degrees to 180 degrees in steps of 1 degree */
    myservo.write(pos);              /* tell servo to go to position in variable 'pos' */
    delay(15);                       /* waits 15ms for the servo to reach the position */
  }
  for (pos = 180; pos >= 0; pos -= 1) { /* goes from 180 degrees to 0 degrees */
    myservo.write(pos);              /* tell servo to go to position in variable 'pos' */
    delay(15);                       /* waits 15ms for the servo to reach the position */
  }
}

Leave a Comment

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