Jump to content

Experiment 3: Interfacing Potentiometer for Position Control Simulation

From cognifront
Revision as of 14:43, 8 October 2025 by Siraj (talk | contribs)

Experiment 3: Interfacing Potentiometer for Position Control Simulation

Objective: To simulate position control by using a potentiometer as analog input to drive a servo motor.

Task:

  1. Connect potentiometer to Arduino analog input and servo to PWM output.
  2. Map the potentiometer values to servo angles.
  3. Observe servo position change as potentiometer is rotated.

Circuit Diagram (without LCD)

Circuit Diagram

Circuit Diagram (with LCD)

Circuit Diagram

Board Connections (without LCD)

Board Connections

Arduino Connections (without LCD)

Component Arduino Pin
Potentiometer Output A0
Servo Signal D8/(PWM)


// © 2025, Cognifront. Member of The IoT Kits Family.

// Experiment 3: Interfacing Potentiometer for Position Control Simulation

// Define the analog pin connected to the potentiometer
const int Pot = A0;

// Include the Servo library to control servo motors
#include <Servo.h>

// Create a Servo object to control a servo motor
Servo myservo;

void setup() {
  pinMode(Pot, INPUT);            // Optional: analogRead sets pin mode automatically
  myservo.attach(8);              // Attach the servo signal wire to digital pin 8
  
  Serial.begin(9600);             // Start serial communication for monitoring
  Serial.println("Potentiometer to Servo Angle Control");
  Serial.println("==================================");
}

void loop() {
  // Read the potentiometer value (0 to 1023 from ADC)
  int potVal = analogRead(Pot);

  // Map potentiometer range (0–1023) to servo range (0–180 degrees)
  int angle = map(potVal, 0, 1023, 0, 180);

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

  // Display potentiometer value and corresponding angle
  Serial.print("Potentiometer Value: ");
  Serial.print(potVal);
  Serial.print("   |   Servo Angle: ");
  Serial.println(angle);

  // Short delay to allow the servo to move smoothly
  delay(100);
}

Board Connections (with LCD)

Board Connections


Arduino Connections (with LCD I2C)

Component Arduino Pin
Potentiometer Output A0
Servo Signal (PWM) D9
LCD VCC 5V
LCD GND GND
LCD SDA A4
LCD SCL A5
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>

// CHANGE THIS if your I2C address is different (common addresses: 0x27 or 0x3F)
LiquidCrystal_I2C lcd(0x27, 16, 2);

Servo myServo;

const int servoPin = 9;    // Servo signal
const int potPin   = A0;   // Potentiometer wiper

// Optional: tweak smoothing
const uint8_t SMOOTH_SAMPLES = 5; // set to 1 to disable smoothing

void setup() {
  Wire.begin();
  lcd.init();
  lcd.backlight();

  myServo.attach(servoPin);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Servo Pos Ctrl");
  lcd.setCursor(0, 1);
  lcd.print("Initializing...");
  delay(800);
  lcd.clear();
}

int readPotSmoothed() {
  long sum = 0;
  for (uint8_t i = 0; i < SMOOTH_SAMPLES; ++i) {
    sum += analogRead(potPin);
    delay(2);
  }
  return (int)(sum / SMOOTH_SAMPLES);
}

void loop() {
  // read potentiometer (0..1023)
  int potVal = readPotSmoothed();

  // map to servo angle (0..180)
  int angle = map(potVal, 0, 1023, 0, 180);
  angle = constrain(angle, 0, 180);

  // move servo
  myServo.write(angle);

  // display on LCD
  lcd.setCursor(0, 0);
  lcd.print("Angle: ");
  // degree symbol (char 223 often works on HD44780)
  lcd.print(angle);
  lcd.print((char)223);
  lcd.print("  "); // pad to clear leftover digits

  lcd.setCursor(0, 1);
  lcd.print("Pot: ");
  lcd.print(potVal);
  lcd.print("    "); // pad to clear

  // small delay to make updates smooth and readable
  delay(120);
}

Observation

  • As the potentiometer knob is rotated, the servo motor smoothly changes its position.
  • The servo angle corresponds proportionally to the potentiometer voltage input (0° to 180° mapped).

Conclusion

  • A potentiometer can be used as an analog input device to control the angular position of a servo motor.
  • This demonstrates how analog signals can be mapped to control actuators in real-time.