The objective of this project is to control a servo motor using a potentiometer with Arduino. By adjusting the potentiometer, the position of the servo motor will change. This project covers PWM for controlling the servo, analog input (analogRead()) for reading the potentiometer, and mapping values from the potentiometer to the servo motor’s angle.
Fundamental Programming Concepts
- PWM (analogWrite()): Controls the position of the servo motor using Pulse Width Modulation.
- Analog Input (analogRead()): Reads the value from the potentiometer and converts it into a digital signal.
- Servo Motor Control: Manages the movement of the servo motor based on the potentiometer’s input.
- Mapping Values: Converts the range of the potentiometer’s analog values to the appropriate range for the servo motor’s movement (0-180 degrees).
Requirement Components
To complete this servo motor control project using Arduino, you will need:
- Arduino Uno Board
- Servo Motor (any standard 9g or larger)
- Potentiometer
- Breadboard
- Jumper Wires
- USB Cable (for connecting Arduino to your computer)
Circuit Diagram
Circuit Connection
Component | Arduino Pin |
Servo Motor (Signal) | Pin 9 |
Servo Motor (Vcc) | 5V |
Servo Motor (GND) | GND |
Potentiometer (Vcc) | 5V |
Potentiometer (GND) | GND |
Potentiometer (Signal) | A0 (Analog Input) |
How to Connect the Circuit
- Servo Motor: Connect the signal pin of the servo motor to Pin 9 on the Arduino, the Vcc pin to 5V, and the GND pin to GND.
- Potentiometer: Connect the middle pin of the potentiometer (signal) to A0 (analog input) on the Arduino, one outer pin to 5V, and the other to GND.
- Ensure the connections are secure and that the power to the servo motor is stable.
Explanation of Circuit
- The potentiometer acts as an input device that allows the user to adjust the servo motor’s position by rotating the potentiometer’s knob.
- The potentiometer’s output is read through analogRead(), and this value is mapped to the appropriate range (0-180 degrees) to control the servo motor.
- The servo motor then adjusts its angle based on the mapped value, with Pin 9 controlling the motor’s position using PWM signals.
Programming Section for servo motor using a potentiometer with Arduino
Arduino Syntax
Topic Name | Syntax | Explanation |
analogRead() | analogRead(pin) | Reads the analog value from the specified pin (A0). |
analogWrite() | analogWrite(pin, value) | Writes a PWM signal to the specified pin to control the servo motor. |
map() | map(value, fromLow, fromHigh, toLow, toHigh) | Maps a value from one range to another, like converting potentiometer values to servo angles. |
Servo.attach() | servo.attach(pin) | Attaches the servo motor to a specific pin (PWM). |
Servo.write() | servo.write(angle) | Sets the position of the servo motor to the specified angle. |
Arduino Code:
Here is the Arduino code to control the servo motor using a potentiometer:
#include <Servo.h> // Include the Servo library
// Create a Servo object
Servo myServo;
// Define the analog pin for the potentiometer
const int potPin = A0; // Potentiometer connected to A0
int potValue = 0; // Variable to store the potentiometer value
int angle = 0; // Variable to store the servo angle
void setup() {
myServo.attach(9); // Attach the servo to pin 9
Serial.begin(9600); // Initialize serial communication
}
void loop() {
// Read the analog value from the potentiometer (0-1023)
potValue = analogRead(potPin);
// Map the potentiometer value to a servo angle (0-180)
angle = map(potValue, 0, 1023, 0, 180);
// Move the servo to the corresponding angle
myServo.write(angle);
// Print the values for debugging
Serial.print("Potentiometer Value: ");
Serial.print(potValue);
Serial.print(" | Servo Angle: ");
Serial.println(angle);
delay(15); // Small delay to ensure smooth movement
}
Steps to Upload Code:
- Connect your Arduino to your computer using a USB cable.
- Open the Arduino IDE and select the correct Board and Port.
- Copy and paste the provided code into a new sketch.
- Click the Upload button to transfer the code to your Arduino.
- Rotate the potentiometer to control the position of the servo motor.
Check Output
Once the code is uploaded and the circuit is properly connected, rotating the potentiometer knob should change the position of the servo motor. The angle of the servo motor will be displayed in the Serial Monitor along with the potentiometer value.
Troubleshooting Tips
- Servo not moving? Double-check the wiring for the servo motor, ensuring the signal pin is connected to Pin 9, and verify that the power to the servo is sufficient.
- Incorrect servo angles? Ensure the map() function is correctly mapping the potentiometer’s analog input (0-1023) to the servo’s angle range (0-180 degrees).
- No output on Serial Monitor? Ensure the correct COM port is selected, and the baud rate is set to 9600 in the Serial Monitor.
Further Exploration
- Multiple Servos: Control more than one servo motor by adding another potentiometer and adjusting the code.
- Add a Display: Display the servo angle on an LCD screen or OLED display for a visual indication of the servo’s position.
- Use for Robotic Arm: This setup can be extended to control multiple servos for a simple robotic arm or other automated projects.
Note
This project teaches the fundamentals of working with PWM, analog input, and servo motor control in Arduino. Understanding how to map values between different ranges (such as potentiometer readings to servo angles) is a critical skill in building more advanced Arduino projects.
FAQ
Q1: How does the analogRead() function work?
The analogRead() function reads an analog input from a pin and returns a value between 0 and 1023, representing the voltage level.
Q2: What does map() do in this project?
The map() function converts the potentiometer’s analog reading (0-1023) into a servo angle (0-180 degrees), allowing the servo motor to move accordingly.
Q3: Can I control multiple servos with one potentiometer?
Yes, you can modify the code to control multiple servos by mapping the same potentiometer value to different servo motors.
Q4: Why do we use the Servo.attach() function?
The Servo.attach() function initializes and links the servo motor to the specified pin on the Arduino, allowing it to be controlled through PWM.
Q5: Can I use this setup for other types of sensors?
Yes, you can modify the code to control the servo using other types of analog sensors, such as temperature sensors, light sensors, or pressure sensors.