/

Arduino Project: Controlling a Servo Motor with a Potentiometer

Arduino Project: Controlling a Servo Motor with a Potentiometer

In this Arduino project, we will learn how to control a servo motor using a potentiometer. By reading the rotation of the potentiometer through an analog input pin, we can obtain values ranging from 0 to 1023. We will utilize these values to rotate the servo motor from 0° to 180°.

First, let’s build the circuit and then proceed to write the program. Connect the power pins (5V and GND) to the + and - breadboard lines, as shown in the image below.

Arduino Project Servo Motor

Next, connect the signals from the power pins to the input pins of the potentiometer, as illustrated in the image below.

Arduino Project Servo Motor

Now, connect the output pin of the potentiometer to analog I/O pin A0, as shown in the images below.

Arduino Project Servo Motor
Arduino Project Servo Motor

Finally, connect the servo motor by connecting the brown wire to 0V, the red wire to 5V, and the orange wire to pin 9, as depicted in the images below.

Arduino Project Servo Motor
Arduino Project Servo Motor

Now, let’s switch to the Arduino IDE to write the program. First, we need to acquire the input from the potentiometer. Use the following code:

1
2
3
4
5
6
7
8
void setup() {
Serial.begin(9600);
}

void loop() {
int value = analogRead(A0);
Serial.println(value);
}

Upload this program to the Arduino, and you should see an output ranging from 0 to 1023 printed in the serial monitor.

Next, we need to remap the values from 0-1023 to 0-180, which will be fed to the servo motor. We can achieve this using the map() function:

1
value = map(value, 0, 1023, 0, 180);

Now, we will utilize the Servo library, which is a built-in library in the Arduino IDE. To include this library, go to the Sketch menu, select Include Library, and choose Servo from the options.

Arduino Project Servo Motor

The inclusion of the Servo library will add the line #include <Servo.h> at the top of the file. If you need more information about this library, you can open the Tools menu and select Manage libraries. This will open the Library Manager, where you can search for “servo” and find the Servo library.

By clicking on the “More info” link, you can access the Arduino Servo Library Reference page for detailed information about the library, including usage information, available methods, and examples.

In our code, we will use two methods from the library: attach() and write(). The attach() method specifies which port to use for the servo, while the write() method moves the servo motor to the desired angle (0 to 180).

To implement these methods, we need to declare a Servo object. Before the setup() function, add the line Servo myservo; along with the inclusion of the Servo library:

1
2
#include <Servo.h>
Servo myservo;

In the setup() function, attach the servo to I/O pin 9:

1
2
3
4
5
6
#include <Servo.h>
Servo myservo;

void setup() {
myservo.attach(9);
}

Finally, in the loop() function, call the myservo.write() method and pass the remapped value (0-180) to rotate the servo motor:

1
2
3
4
5
6
7
8
9
10
11
12
#include <Servo.h>
Servo myservo;

void setup() {
myservo.attach(9);
}

void loop() {
int value = analogRead(A0);
value = map(value, 0, 1023, 0, 180);
myservo.write(value);
}

Now you have a complete Arduino project to control a servo motor using a potentiometer. Enjoy exploring and experimenting with this setup!

tags: [“Arduino”, “servo motor”, “potentiometer”, “circuit”, “analog input”, “map() function”, “Servo library”]