Jump to content

Experiment 3: Interfacing Potentiometer for Position Control Simulation

From cognifront
Revision as of 04:17, 9 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.

⚠️ Disconnect your Arduino from the computer while making or removing component connections.

⚠️ Important Note:
1. Connect only the Arduino Uno (without any component connections) to the computer via USB.
2. Upload the code to the Arduino.
3. Disconnect the Arduino from the computer and remove the USB cable.
4. Make all component connections using patch cords as per the circuit diagram.
5. Then connect the 9V adapter to provide external power supply to the Arduino.

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) D8
LCD VCC 5V
LCD GND GND
LCD SDA A4
LCD SCL A5
// © 2025, Cognifront. Member of The IoT Kits Family.
// Experiment 3: Interfacing Potentiometer for Position Control Simulation
// Added: Display potentiometer value and servo angle on 16x2 I2C LCD

/*
  🔌 LCD CONNECTIONS (16x2 I2C Display with Arduino UNO)
  ------------------------------------------------------
  LCD Pin     →  Arduino Pin
  ----------------------------
  GND         →  GND
  VCC         →  5V
  SDA         →  A4   (I2C Data line)
  SCL         →  A5   (I2C Clock line)

  ⚙️ NOTE:
  - Most common I2C LCD addresses are 0x27 or 0x3F.
  - If no text appears, try changing the address in this line:
        LiquidCrystal_I2C lcd(0x27, 16, 2);
      to
        LiquidCrystal_I2C lcd(0x3F, 16, 2);
*/

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>

// Initialize LCD with I2C address (0x27 by default), 16 columns, 2 rows
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Define potentiometer and servo pins
const int Pot = A0;    // Analog pin connected to potentiometer output (middle pin)
Servo myservo;         // Servo object

void setup() {
  pinMode(Pot, INPUT);             // Potentiometer input
  myservo.attach(8);               // Servo signal wire → D8 on Arduino

  Serial.begin(9600);              // For serial monitoring
  Serial.println("Potentiometer to Servo Angle Control");
  Serial.println("==================================");

  // Initialize the LCD
  lcd.init();                      // Start LCD
  lcd.backlight();                 // Turn on LCD backlight
  lcd.clear();

  // Display static text headers
  lcd.setCursor(0, 0);
  lcd.print("Pot: ");
  lcd.setCursor(0, 1);
  lcd.print("Angle: ");
}

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

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

  // Move servo to calculated angle
  myservo.write(angle);

  // Show values on Serial Monitor
  Serial.print("Potentiometer Value: ");
  Serial.print(potVal);
  Serial.print("   |   Servo Angle: ");
  Serial.println(angle);

  // Display on LCD
  lcd.setCursor(5, 0);      // Position after "Pot: "
  lcd.print(potVal);
  lcd.print("   ");         // Clear trailing digits

  lcd.setCursor(7, 1);      // Position after "Angle: "
  lcd.print(angle);
  lcd.print((char)223);     // Degree symbol (°)
  lcd.print("   ");         // Clear trailing digits

  delay(100);               // Small delay for smooth motion
}

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.