LAB 8: SPEED AND POSITION CONTROL OF MULTIPLE SERVO
MOTORS USING ARDUINO AND SERVO DRIVER
Excellent Proficient Basic Below Basic Student’
Criteria
s Score
Introduction provides a
Introduction is
clear background. Introduction is
adequate, but the
Problem, objectives, and confusing and the
Introduction Problem, objectives Introduction is
apparatus/software is Problem, objectives and
and missing
defined clearly with apparatus/software is
apparatus/software is
orienting material for not defined clearly
audience. defined clearly
Points are clearly Most points are
Report presented and discussed in ordered and Report is
Points are not
Organization a logical order. Easily discussed well. No disorganized and layout
ordered.
followed. Page layout is major problems with is somewhat weak.
effective. layout.
Result
Most but not all
Discussion and Clear, insightful Conclusions are Conclusions
points contained in
Conclusions conclusions. inadequate. are missing.
the conclusion.
Lab 08: Speed and position control of
multiple servo motor using Arduino and servo
driver
Introduction:
Servo motors are widely used in various applications that require precise
control over angular position and rotational speed. They are commonly employed in
robotics, automation systems, and model making. In this lab, we explore the control
of multiple servo motors using Arduino, a popular microcontroller board, and a servo
driver.
The objective of this lab is to develop a system that can control the servo motor's
speed and position accurately, providing real-time feedback through an LCD display.
By achieving this, we can effectively manipulate the physical movements of the
servo motor and monitor the desired position and speed values simultaneously.
Servo Motor:
A servo motor is an electro-mechanical device that is used to control the
position of an object with high precision. Servo motors are commonly used in a
wide range of applications such as robotics, CNC machines, aerospace, and
automation. They are popular because of their accuracy, speed, and torque
capabilities. In this detailed report, we will look at the basics of servo motors,
their working principle, types, applications, and advantages.
Figure 1
Task No 1:
Display the angular position value of servo motor on serial monitor
and LCD:
Following is a step-by-step procedure to display the angular position value of a
servo motor on both the serial monitor and an LCD screen:
1. Hardware Setup: Connect the servo motor to the Arduino board. Connect
the power supply (5V or 6V) to the VCC and GND pins of the servo motor.
Connect the signal wire of the servo motor to a PWM pin on the Arduino.
Connect the LCD screen to the Arduino board using the I2C interface. Ensure the I2C
address and dimensions of the LCD screen are correctly set.
2. Software Setup: Launch the Arduino IDE (Integrated Development
Environment) on your computer. Install the necessary libraries if not already
installed. For the servo motor control, install the "Servo" library. For the LCD screen,
install the appropriate LiquidCrystal_I2C library.
3. Write the Arduino Code:
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#include <LiquidCrystal_I2C.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
const int servoMin = 150; // Minimum pulse length for your
servo motor
const int servoMax = 600; // Maximum pulse length for your
servo motor
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust I2C address and
dimensions as needed
void setup() {
Serial.begin(9600);
lcd.begin(16, 2); // Initialize LCD
pwm.begin();
pwm.setPWMFreq(60); // Adjust the frequency if necessary
}
void loop() {
int currentPosition = readServoPosition();
// Display position on serial monitor
Serial.print("Servo Position: ");
Serial.println(currentPosition);
// Display position on LCD screen
lcd.clear();
lcd.print("Position: ");
lcd.print(currentPosition);
delay(1000); // Delay for readability, adjust as needed
}
int readServoPosition() {
int pulseWidth = pwm.getPWM(0); // Assuming servo is
connected to channel 0
int currentPosition = map(pulseWidth, servoMin, servoMax, 0,
180);
return currentPosition;
}
4. Upload the Code: Connect the Arduino board to your computer using a
USB cable. Select the appropriate board and port from the "Tools" menu in
the Arduino IDE. Click the "Upload" button to upload the code to your
Arduino board.
5. Monitor the Results: Open the serial monitor in the Arduino IDE to
observe the angular position values of the servo motor. Check the LCD
screen for the real-time display of the angular position values.
By following these steps, we should be able to display the angular position value
of the servo motor on both the serial monitor and the LCD screen.
Figure 2
Task No 2:
To Rotate the servo from angle 0 to 180 degree and back to zero:
To rotate the servo from angle 0 to 180 degree and back to zero the connection and
setup is same only difference is the code
Write the Arduino Code: Here's code to rotate the servo for specific angle.
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#include <LiquidCrystal_I2C.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int servoPin = 0;
const int servoMin = 150;
const int servoMax = 600;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
pwm.begin();
pwm.setPWMFreq(60); }
void loop() {
// Rotate servo from 0 to 180 degrees
for (int angle = 0; angle <= 180; angle++) {
setServoPosition(angle);
displayPosition(angle);
delay(10); // Adjust delay for servo movement speed}
// Rotate servo from 180 to 0 degrees
for (int angle = 180; angle >= 0; angle--) {
setServoPosition(angle);
displayPosition(angle);
delay(10); // Adjust delay for servo movement speed}}
void setServoPosition(int angle) {
int pulseWidth = map(angle, 0, 180, servoMin, servoMax);
pwm.setPWM(servoPin, 0, pulseWidth)}
void displayPosition(int angle)
{ Serial.print("Servo Position:
"); Serial.println(angle);
lcd.clear();
lcd.print("Position: ");
lcd.print(angle);}
Upload the Code: Connect your Arduino board to your computer using a USB
cable. In the Arduino IDE, select the appropriate board and port from the "Tools"
menu. Click the "Upload" button to upload the code to your Arduino board.
Test the Servo Motors: Once the code is uploaded, the servo motors connected to
the servo motor driver will move according to the specified positions.
Task No 3:
Controlling a servo position using a potentiometer (variable resistor).
1. Hardware Setup: Connect the servo motor to the Arduino board.
Connect the power supply (5V or 6V) to the VCC and GND pins of the
servo motor. Connect the signal wire of the servo motor to a PWM pin on
the Arduino. Connect a potentiometer to one of the analog input pins
(e.g., A0) on the
Arduino. Connect one end of the potentiometer to the 5V pin and the other end to the
GND pin. Connect the wiper (middle pin) of the potentiometer to the analog input
pin
2. Software Setup: Launch the Arduino IDE (Integrated Development
Environment) on your computer. Install the necessary libraries if not already
installed. For the servo motor control, install the "Adafruit_PWMServoDriver" library. For the
LCD screen, install the appropriate "LiquidCrystal_I2C" library.
3. Write the Arduino Code: Open a new sketch in the Arduino IDE and include the
required libraries at the top of your code.
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h> #include
<LiquidCrystal_I2C.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); LiquidCrystal_I2C
lcd(0x27, 16, 2);
const int servoPin = 0; const
int servoMin = 150; const int
servoMax = 600; const int potPin
= A0; void setup() {
Serial.begin(9600); lcd.begin(16, 2);
pwm.begin();
pwm.setPWMFreq(60); }
void loop() {
int potValue = analogRead(potPin);
int angle = map(potValue, 0, 1023, 0, 180);
setServoPosition(angle); displayPosition(angle);
delay(10);}
void setServoPosition(int angle) {
int pulseWidth = map(angle, 0, 180, servoMin, servoMax);
pwm.setPWM(servoPin, 0, pulseWidth);}
void displayPosition(int angle)
{ Serial.print("Servo Position: ");
Serial.println(angle); lcd.clear();
lcd.print("Position: "); lcd.print(angle);}
4. Upload the Code: Connect the Arduino board to your computer using a USB cable.
Select the appropriate board and port from the "Tools" menu in the Arduino IDE.
Click the "Upload" button to upload the code to your Arduino board.
5. Monitor the Results: Open the serial monitor in the Arduino IDE to observe the
angular position values of the servo motor as you rotate the
potentiometer. Check the LCD screen for the real-time display of the angular position values.