Jump to content

Experiment 3: Interfacing Potentiometer for Position Control Simulation

From cognifront
Revision as of 16:51, 16 August 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.

Arduino Connections

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);       // Set potentiometer pin as input (optional, analogRead does this automatically)
  myservo.attach(8);         // Attach the servo signal wire to digital pin 8
}

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);  
  
  // Short delay to allow the servo to move smoothly
  delay(10);          
}

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.