0% found this document useful (0 votes)
20 views7 pages

Servo Motor Control with Arduino IDE

Uploaded by

Muhammad Khan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views7 pages

Servo Motor Control with Arduino IDE

Uploaded by

Muhammad Khan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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() {
[Link](9600);

[Link](16, 2); // Initialize LCD


[Link]();
[Link](60); // Adjust the frequency if necessary
}
void loop() {
int currentPosition = readServoPosition();
// Display position on serial monitor
[Link]("Servo Position: ");
[Link](currentPosition);

// Display position on LCD screen


[Link]();

[Link]("Position: ");
[Link](currentPosition);

delay(1000); // Delay for readability, adjust as needed

}
int readServoPosition() {
int pulseWidth = [Link](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() {

[Link](9600);
[Link](16, 2);

[Link]();
[Link](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);


[Link](servoPin, 0, pulseWidth)}

void displayPosition(int angle)


{ [Link]("Servo Position:
"); [Link](angle);
[Link]();

[Link]("Position: ");
[Link](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() {

[Link](9600); [Link](16, 2);

[Link]();
[Link](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);
[Link](servoPin, 0, pulseWidth);}

void displayPosition(int angle)


{ [Link]("Servo Position: ");
[Link](angle); [Link]();

[Link]("Position: "); [Link](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.

Common questions

Powered by AI

Arduino code for displaying servo motor positions involves specific setups and loop functions. In the setup(), 'Serial.begin(9600)' initializes serial communication, and 'lcd.begin(16, 2)' initializes the LCD. Inside loop(), a function like 'readServoPosition()' determines the servo position by mapping PWM pulse widths to angles. This position is printed to the serial monitor with 'Serial.print()' and to the LCD using 'lcd.print()'. 'lcd.clear()' ensures each new position overwrites the previous one on the screen. Such structured code integrates various functions to deliver real-time feedback on motor positions .

Using an LCD to display real-time feedback in servo motor control projects offers several benefits. It provides immediate, visual confirmation of operations, aiding in debugging and monitoring without needing a computer connection. It can enhance user interface and experience, especially in standalone systems. However, there are limitations such as added hardware complexity and power consumption. The LCD requires dedicated pins and addressing, which can complicate hardware setups. Additionally, display size and clarity could restrict the amount and detail of information shown, potentially limiting usefulness in more complex systems .

Using a potentiometer introduces analog input control, allowing for dynamic and precise manipulation of servo motor positions. By connecting a potentiometer to one of the Arduino's analog pins, users can convert the variable resistance into an analog voltage that is read as a value between 0 and 1023. This value can be mapped to a specific angular range (e.g., 0-180 degrees for a servo) using the 'map' function in Arduino. Consequently, the servo motor's position can be adjusted by physically rotating the potentiometer knob, enhancing real-time control and user interaction with the system .

Adjusting the PWM frequency impacts how effectively a servo motor can interpret control signals from an Arduino. PWM frequency determines how fast the PWM signal repeats. A servo requires signals at a certain frequency to interpret position accurately; typically around 50-60 Hz is common for RC servos. Setting the frequency too low or high can lead to erratic motor behavior or inability to reach specified angles, as servos may not react well to signals outside their design specifications. Proper adjustment ensures smooth and precise movements, maintaining the servo's accuracy and power efficiency .

Setting up a hardware configuration for an Arduino-based servo control system involves several key considerations. First, ensuring adequate power supply to servos to prevent underperformance is crucial; typically, 5V is used. Proper shielding and wiring to prevent signal interference and secure connections for reliability are also key. Each servo's signal wire needs secure connection to a PWM-capable pin on the Arduino. Additionally, any added components like an LCD or potentiometer must be correctly interfaced — I2C for LCDs and analog for potentiometers — ensuring compatibility and proper address or pin usage. These considerations ensure the system functions effectively and reliably .

Servo motors, with precise control over angular position and speed, are pivotal in robotics for tasks requiring exact movements, such as joint or actuator positioning. Coupled with Arduino's PWM capabilities that translate digital signals into analog positions, robotics applications can achieve fine-grained motion control. Arduinos enable real-time adjustments and responsive interactions based on sensor feedback, making them ideal for complex tasks like navigating terrains or executing repetitive tasks accurately. This synergy facilitates sophisticated robotic designs with cost-effective and programmable control systems .

To control multiple servo motors using Arduino, essential components include the Arduino board, servo motors, a servo driver like Adafruit_PWMServoDriver, and an LCD screen for feedback. The servo motors are connected to the Arduino via PWM pins for signal input, while a power supply connects to the VCC and GND pins of the servos. The LCD screen connects through the I2C interface of the Arduino to display servo position information. These components interconnect allowing the Arduino to send PWM signals to the servos, which interpret these signals as position commands, while feedback from an LCD indicates position updates .

Calibrating the I2C address is crucial for ensuring that the Arduino can correctly communicate with the LCD screen. Each device on the I2C bus needs a unique address to avoid conflicts that can lead to communication errors. During setup, it's important to verify the I2C address of the LCD and adjust the code accordingly. This involves checking the device data sheet or using a scanner sketch to determine the current address and setting it in the LiquidCrystal_I2C library parameters within the Arduino code. Proper calibration ensures effective data transmission and accurate display outputs .

Libraries such as 'Servo' and 'LiquidCrystal_I2C' significantly streamline the software development process in Arduino projects. The 'Servo' library simplifies the control of servo motors by abstracting the details of writing PWM signals, allowing easy position manipulation using simple 'write()' commands. Similarly, 'LiquidCrystal_I2C' facilitates the interface of an LCD screen with the Arduino, managing communication over the I2C bus and providing functions for text printing and display manipulation. These libraries drive efficiency by reducing the need for extensive coding, allowing developers to focus on higher-level logic and integration .

To upload code for servo motor control on an Arduino, follow these steps: Connect the Arduino to a computer using a USB cable. Open the Arduino IDE and ensure the correct board type (e.g., 'Arduino Uno') and port are selected from the 'Tools' menu. Install necessary libraries like 'Servo' and 'LiquidCrystal_I2C,' if not already installed. Write or open the Arduino sketch with the desired servo control code. Click the 'Upload' button in the IDE to compile and upload the code to the Arduino. Verify successful upload as indicated by the 'Done Uploading' status message. Finally, monitor the system to ensure the uploaded code functions as expected, using tools like the serial monitor for feedback .

You might also like