Arduino Potentiometer-Controlled Servo Experiment

Arduino Potentiometer-Controlled Servo Experiment

Introduction

In this experiment, you’ll learn how to control the position of a servo motor using a potentiometer. The potentiometer acts as an input, allowing you to vary the servo's position by adjusting the potentiometer's knob.

Components Needed

Circuit Setup

  1. Connect the middle pin of the potentiometer to the Arduino's analog input pin A0.
  2. Connect one of the side pins of the potentiometer to the 5V pin on the Arduino and the other side pin to GND.
  3. Connect the servo motor's Signal pin to digital pin 9 on the Arduino.
  4. Connect the servo motor's VCC pin to the breadboard power supply.
  5. Connect the servo motor's GND pin to the breadboard power supply's GND.
  6. Ensure the Arduino GND and the breadboard power supply GND are connected together.

Note: Always power the servo motor using an external power supply, as driving it directly from the Arduino's 5V pin may cause instability or damage to the Arduino.

Code for Potentiometer-Controlled Servo

Upload the following code to your Arduino to control the servo position with the potentiometer:


// Include the Servo library
#include 

Servo myServo;         // Create a Servo object
const int potPin = A0; // Analog pin for the potentiometer

void setup() {
    myServo.attach(9); // Attach the servo to digital pin 9
}

void loop() {
    // Read the potentiometer value (0 - 1023)
    int potValue = analogRead(potPin);

    // Map the potentiometer value to a range for the servo (0 - 180 degrees)
    int angle = map(potValue, 0, 1023, 0, 180);

    // Set the servo to the calculated angle
    myServo.write(angle);

    delay(15); // Small delay to allow the servo to reach the position
}
            

Explanation

The potentiometer acts as an input device that provides a variable resistance based on its position. By reading the analog value from the potentiometer, we can control the servo position as follows:

Troubleshooting