0% found this document useful (0 votes)
2 views

5. Controlling Motor with Arduino (7) (1)

The document explains the differences between DC, Servo, and Stepper motors, detailing their operation, control methods, and applications. DC motors provide continuous rotation and speed control via PWM, Servo motors offer precise angular control with feedback, and Stepper motors allow for accurate positioning through defined steps. It also includes sample Arduino programs for controlling each type of motor, demonstrating how to implement motor control in practical applications.

Uploaded by

Ibrahem Masoud
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

5. Controlling Motor with Arduino (7) (1)

The document explains the differences between DC, Servo, and Stepper motors, detailing their operation, control methods, and applications. DC motors provide continuous rotation and speed control via PWM, Servo motors offer precise angular control with feedback, and Stepper motors allow for accurate positioning through defined steps. It also includes sample Arduino programs for controlling each type of motor, demonstrating how to implement motor control in practical applications.

Uploaded by

Ibrahem Masoud
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Controlling Stepper/DC/Servo

Motors with Arduino


What's the difference between
DC, Servo & Stepper Motors?

Stepper motors

Servo motor
DC Motors
• DC (Direct Current) Motors are two wire (power & ground),
continuous rotation motors.
• When you supply power, a DC motor will start spinning until that
power is removed.
• Most DC motors run at a high RPM (revolutions per minute),
examples being computer cooling fans, or radio controlled car wheels.
DC Motors(Cont.)
• The speed of DC motors is controlled using pulse width modulation
(PWM), a technique of rapidly pulsing the power on and off.
(Remember last lecture)
• The percentage of time spent cycling the on/off ratio determines the
speed of the motor, e.g. if the power is cycled at 50% (half on, half
off), then the motor will spin at half the speed of 100% (fully on).
Each pulse is so rapid that the motor appears to be continuously
spinning with no stuttering.
Servo Motors
• Servo motors are generally an assembly of four things: a DC motor, a gearing set,
a control circuit and a position-sensor (usually a potentiometer).
• The position of servo motors can be controlled more precisely than those of
standard DC motors, and they usually have three wires (power, ground & control).
• Power to servo motors is constantly applied, with the servo control circuit
regulating the draw to drive the motor. Servo motors are designed for more
specific tasks where position needs to be defined accurately such as controlling
the rudder on a boat or moving a robotic arm or robot leg within a certain range.
• Servo motors do not rotate freely like a standard DC motor. Instead the angle of
rotation is limited to 180 Degrees (or so) back and forth. Servo motors receive a
control signal that represents an output position and applies power to the DC
motor until the shaft turns to the correct position, determined by the position
sensor.
Servo Motors(Cont.)
• PWM is used for the control signal of servo motors. However, unlike DC
motors it’s the duration of the positive pulse that determines the position,
rather than speed, of the servo shaft. A neutral pulse value dependent on
the servo (usually around 1.5ms) keeps the servo shaft in the center
position.
• Increasing that pulse value will make the servo turn clockwise, and a
shorter pulse will turn the shaft anticlockwise. The servo control pulse is
usually repeated every 20milliseconds, essentially telling the servo where
to go, even if that means remaining in the same position.
• When a servo is commanded to move, it will move to the position and hold
that position, even if external force pushes against it. The servo will resist
from moving out of that position, with the maximum amount of resistive
force the servo can exert being the torque rating of that servo.
Stepper Motors
• A stepper motor is essentially a servo motor that uses a different method of
motorization.
• Where a servo motor uses a continuous rotation DC motor and integrated
controller circuit, stepper motors utilize multiple toothed electromagnets
arranged around a central gear to define position.
• Stepper motors require an external control circuit or micro controller (e.g. a
Raspberry Pi or Arduino) to individually energize each electromagnet and make
the motor shaft turn.
Stepper Motors(Cont.)
• When electromagnet ‘A’ is powered it attracts the gear’s teeth and
aligns them, slightly offset from the next electromagnet ‘B’. When ‘A’
is switch off, and ‘B’ switched on, the gear rotates slightly to align
with ‘B’, and so on around the circle, with each electromagnet around
the gear energizing and de-energizing in turn to create rotation.
• Each rotation from one electromagnet to the next is called a "step",
and thus the motor can be turned by precise pre-defined step angles
through a full 360 Degree rotation.
Stepper Motors(Cont.)
• Stepper motors are available in two varieties; unipolar or bipolar.
• Bipolar motors are the strongest type of stepper motor and usually have four or
eight leads. They have two sets of electromagnetic coils internally, and stepping is
achieved by changing the direction of current within those coils.
• Unipolar motors, identifiable by having 5, 6 or even 8 wires, also have two coils,
but each one has a center tap. Unipolar motors can step without having to
reverse the direction of current in the coils, making the electronics simpler.
However, because the center tap is used to energize only half of each coil at a
time they typically have less torque than bipolar.
• The design of the stepper motor provides a constant holding torque without the
need for the motor to be powered and, provided that the motor is used within its
limits, positioning errors don't occur, since stepper motors have physically pre-
defined stations.
Summary
• DC Motors: Fast, continuous rotation motors – Used for anything that
needs to spin at a high RPM e.g.car wheels, fans etc.
• Servo Motors: Fast, high torque, accurate rotation within a limited angle –
Generally a high performance alternative to stepper motors, but more
complicated setup with PWM tuning. Suited for robotic arms/legs or
rudder control etc.
• Stepper Motors: Slow, precise rotation, easy set up & control – Advantage
over servo motors in positional control. Where servos require a feedback
mechanism and support circuitry to drive positioning, a stepper motor has
positional control via its nature of rotation by fractional increments. Suited
for 3D printers and similar devices where position is fundamental.
DC motor control
• To control a DC motor is relatively simple. Since the motion of a DC
motor is totally determined by the voltage provided (the speed is
proportional to the voltage level and the direction is controlled by the
polarity), we need only supply such a voltage signal to the motor,
which can be generated by many controllers.
• The problem is power; most motors require a driving currency as
large as several amperes, while most controllers can only provide
voltage signals at mA level. Thus we need a driver to amplify the
signal.
Good for motor voltages
from 4.5V up to 36V!
DC motor control (Cont.)
• A possible connection is shown as below:
Controlling DC Motor Speed
• The voltage supplied to a DC motor controls its speed
• Arduino cannot supply variable DC output
• Arduino lacks a true analog output
• Use Pulse-width modulation (PWM) to simulate a variable DC
supply voltage
• PWM is a common technique for supplying variable power levels to
“slow” electrical devices such as resistive loads, LEDs, and DC
motors
• Arduino Uno has 6 PWM pins: Digital I/O pins 3, 5, 6, 9,10, and 11

14
Arduino Uno has 6 PWM pins
• Look for the ~ prefix on the digital pin label, e.g. ~3

https://youtu.be/XRehsF_9YQ8
15
Sample Program for DC motor Control
//introduce the pins to be used with the dc motor void turnClockwise() void stop_Motor()
int En = 10; // should be a PWM pin { {
int In1 = 9; digitalWrite(In1, LOW);
int In2 = 8; digitalWrite(In1, HIGH); digitalWrite(In2, LOW);
digitalWrite(In2, LOW); analogWrite(En, 0);
void setup() analogWrite(En, 150); delay(100);
{ delay(1000); }
// set all the motor control pins as output }
pinMode(En, OUTPUT);
pinMode(In1, OUTPUT);
pinMode(In2, OUTPUT); void turnCounterClockwise()
} {
digitalWrite(In1, LOW);
void loop() digitalWrite(In2, HIGH);
{ analogWrite(En, 150);
turnClockwise(); delay(1000);
stop_Motor(); }
turnCounterClockwise();
}
DC Motor Driving with IC L293D
As L293D IC has two H-Bridges, each H-Bridge can drive a
DC motor (Max two DC motors can be driven by L293D).
Example Lets write a sketch for controlling a DC motor speed with a Potantiometer.
Also, conrolling the direction of the motor rotation by using a Push button.
• #define POT A0 void loop()
• #define EN1 3//pwm input {
• #define In1 4//In1 pos = analogRead(A0);
• #define In2 7//In2 //Serial.println(pos);
• volatile int State=HIGH; veloc = map(pos, 0, 1023, 0, 255);
• int pos, veloc; digitalWrite(In1,State);
digitalWrite(In2,!State);
• void setup() analogWrite(EN1, veloc);
• { Serial.println(State);
• pinMode(EN1, OUTPUT); }
• pinMode(In1, OUTPUT);
• pinMode(In2, OUTPUT); void ChangeDirection(void)
• Serial.begin(9600);
{
• attachInterrupt(digitalPinToInterrupt(2), ChangeDirection, RISING );
State=!State;
• }
}
Servo motor control
• Servos integrating feedback processing circuits usually have three leads:
power (+5V), ground, and control.
• It is controlled by PWM signal. The duty cycle of the PWM determines the
angular position of the servo.
• Usually the servo motor has a motion range, for example, -900~90 0.
• A 50% duty cycle corresponds to 0 0;
• 0% corresponds to -90 0; and
• 100% corresponds to 90 0.
• Therefore we need only program the PWM output to control the accurate
position of the servo.
Servo motor Arduino connection

https://youtu.be/ZySGP4AwGCY
https://youtu.be/t4KRksxjP6k
Servo motor Sample Program
void loop()
• #include<Servo.h> {
if(t)
• Servo servo; {
• int servoPosition = 0; servoPosition+=90;
• int t=1; servo.write(servoPosition); // rotate 90 degrees
delay(1000);
• void setup() servoPosition-=90;
• { servo.write(servoPosition); // rotate -90 degrees to initial
• servo.attach(9); /*attaches the position
servo on pin 9 to the servo delay(1000);
object*/ }
• servo.write(servoPosition); t=0; //set t=0 so it only runs for once
/*initiallize the servo position to 0 }
degrees*/ /*The library function servo.write() uses degrees as parameter
• } NOT percentage*/
Servo Example 2
• Write a sketch to control a Servo Motor by a Potantiometer
Example 2 sketch
• #include<Servo.h> void loop()
• Servo servo; {
• int servoPosition = 90; int PotVal=analogRead(A0);
• int t=1; servoPosition=map(PotVal,0,1023,0,180);
• void setup() Serial.println(servoPosition);
• { servo.write(servoPosition);
• servo.attach(9); /*attaches the servo delay(1000);
on pin 9 to the servo object*/
• servo.write(servoPosition); }
/*initiallize the servo position to 0
degrees*/
• Serial.begin(9600);
• }
Controlling stepper motor with Arduino
• Based on the introduction above, we can see to control a stepper
motor with a driver, we need only provide a pulse signal and a
direction signal. These can be simply generated by the output pins of
Arduino.

https://youtu.be/Sl2mzXfTwCs
Controlling a Stepper Motor With an H-Bridge
• As L293D IC has two H-Bridges, each H-Bridge will drive one of the electromagnetic coils of a stepper motor.
• By energizing these electromagnetic coils in a specific sequence, the shaft of a stepper can be moved
forward or backward precisely in small steps.
• However, the speed of a motor is determined by the how frequently these coils are energized.
• Below image illustrates driving stepper with 2 H-Bridges.
Stepper Motor Driving with IC L293D
Example
Example (sketch for the circuit on the prev. slide)
// Include the Arduino Stepper Library void loop()
#include <Stepper.h> {
// Number of steps per output rotation // step one revolution in one direction:
const int stepsPerRevolution = 200; Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
// Create Instance of Stepper library delay(500);
Stepper myStepper(stepsPerRevolution, 12, 11, 10, 9);
void setup() // step one revolution in the other direction:
{ Serial.println("counterclockwise");
// set the speed at 20 rpm: myStepper.step(-stepsPerRevolution);
myStepper.setSpeed(20); delay(500);
// initialize the serial port: }
Serial.begin(9600);
}
Sample Program for Stepper Motor Control

#include <Stepper.h> void loop() {


// read the sensor value:
const int stepsPerRevolution = 200; int sensorReading = analogRead(A8);
// change this to fit the number of steps per revolution // map it to a range from 0 to 100:
// for your motor int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// set the motor speed:
if (motorSpeed > 0) {
// initialize the stepper library on pins 8 through 11: myStepper.setSpeed(motorSpeed);
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); // step 1/100 of a revolution:
myStepper.step(stepsPerRevolution / 100);
int stepCount = 0; // number of steps the motor has taken }
}
void setup() {
// nothing to do inside the setup
}

You might also like