Servo Motor

Aim

To control the rotation of a Servo Motor using a Potentiometer.

Components required for the experiment

Arduino UNO Board, Breadboard,  Servo Motor and Potentiometer (10K Ohms)

What is Servo Motor and where it is used?

A servo motor is an electromechanical device that uses position feedback to control its motion and final position. It contains a small DC motor, a gearbox, and a control circuit that allows it to rotate to a specific angle or position. Servo motors are used in a variety of applications, such as RC cars, aeroplanes, and boats, as well as in industrial automation and robotics. They are particularly useful in applications where precise control of movement is required.

 

 

 

Circuit Diagram

To practice, make the circuit diagram with the exact pin-to-pin connection as shown below. This will help you to complete the experiment successfully. Click on the image to enlarge.

Code

#include <Servo.h>
Servo myServo;              // create servo object to control a servo
int potpin = A0;            // analog pin used to connect the potentiometer
int val;                    // variable to read the value from the analog pin
void setup()
{
Serial.begin(9600);       // initialize serial communication at 9600 bits per second
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)
int angle = map(val, 0, 1023, 0, 180);  // maps the potentiometer value (0 to 1023) to angle (0 to 180)
myServo.write(angle);                   // sets the servo position according to the mapped angle value
Serial.print("Potentiometer value: ");
Serial.print(val);
Serial.print(", angle: ");
Serial.println(angle);
delay(15);                              // waits for the servo to reach the position
}

Code Explained

This code controls the rotation of the Servo motor by the rotation of the potentiometer.

#include <Servo.h>;

We start by including the Servo library, which has all the essential header files to control the

servo motor.

Servo myServo;

We create a variable myServo to control the Servo Motor.

int potpin = A0;

We have used analog pin A0 to connect the potentiometer.

int val;

Here, we have created a variable to read the data from the analog pin A0.

void setup()
{
Serial.begin(9600);
myServo.attach(9);
}

In the setup function, we start serial communication with a baud rate of 9600 and attach the

servo motor to pin9 of Arduino.

void loop()
{
val = analogRead(potpin);

In the loop function, we first read the potentiometer value using analogRead() function, which

returns a value between 0 to 1023.

int angle = map(val, 0, 1023, 0, 180);

We create a variable angle to store the servo motor angle value.

Also, we map the potentiometer value from the range of 0 to 1023 to the range of 0 to 180

(which is the range of servo motor angles).

myServo.write(angle);

We then use the write() function of the Servo library to set the angle of the servo motor to the

mapped value.

Serial.print("Potentiometer value:");
Serial.print(val);
Serial.print(", angle:");
Serial.println(angle);

Finally, we use the Serial.println() function to print the potentiometer value and the

corresponding angle to the serial monitor.

delay(15);
}

This is the delay time taken by the servo motor to reach the new position.

Output

  1. Press the start simulation button in the interface.

  2. Move the mouse cursor to the Potentiometer dial.

  3. Click and hold the mouse button to hold the Potentiometer dial and keep the mouse button pressed.

  4. While holding the mouse button, move the potentiometer dial to adjust its position.

  5. Observe the movement of the servo motor as you adjust the potentiometer dial.

 

Conclusion

The Servo Motor shaft will rotate clockwise or anti-clockwise depending on the rotation of the potentiometer.

Assignment

Add a buzzer in the circuit and connect it to pin 6 of Arduino. Change the code, for servo motor angle greater than 120 degrees, the buzzer should turn ON for 3 seconds and then turn OFF.