How to use a Servo Motor with Arduino

How to use a Servo Motor with Arduino

Servo with Arduino

INTRODUCTION

A servo motor is an electrical device that can push or rotate an object with great precision. It also is known as a rotary actuator or linear actuator that allows for precise control of angular or linear position, velocity and acceleration.

APPLICATIONS

The servo is commonly used in radio-controlled airplanes to position control surfaces like elevators and rudders. The servo can also be used for robotic arms, limbs and grippers.

WIRING DIAGRAM

GND – GND

VCC - 5V

OUT – Digital Pin 9

You will need a 10k or 1k potentiometer to control the servo as shown in the above picture. The middle pin connects to Analog Pin A0 and the outer pins to 5V and Ground.

Servo arduino connections diagram

CODE

 

#include <Servo.h>

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

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

void setup() {
  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)
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
}

END RESULT

Rotate the nob of the potentiometer and observe the motion of the servo motor.

ADDITIONAL RESOURCES

https://www.instructables.com/id/Arduino-Servo-Motors/

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

https://www.allaboutcircuits.com/projects/servo-motor-control-with-an-arduino/