Motor Control Laboratory Activity 3
Motor Control Laboratory Activity 3
I. OBJECTIVES
At the end of the activity the student should be able to:
1. Understand how DC motors, servo motors and stepper motor works.
2. Understand how to control motor using H-Bridge (L293D) circuits.
3. Interface DC motors, servo motors using H-Bridge (L293D) and stepper motor
to an Arduino board.
III. DISCUSSION
DC Motor
A DC motor is a fairly simple electronic motor that
uses electricity and a magnetic field to produce torque, which
turns the motor. There are two controllable parameters of a
DC motor; direction and speed. To control the direction, the
polarity of the motor is reversed. To control the speed, the
input voltage is varied using pulse width modulation
(PWM).
Figure 1. A DC Motor
Driving DC Motors
DC motors rotate continuously when a DC voltage is applied across them. These
motors are commonly used as the driving motors in radio control (RC) cars, in power drills
and saws, and as the motors that make the discs spin in DVD players. DC motors are
great because they come in a huge array of sizes and are generally very cheap. By
adjusting the voltage you apply to them, you can change their rotation speed. Using a
gearbox, you can trade their speed for torque. By reversing the direction of the voltage
applied to them, you can change their direction of rotation as well. This is generally done
using an H-bridge.
Brushed DC motors, such as the ones used in this activity, employ stationary
magnets (the stator) and a spinning coil (the rotor). Electricity is transferred to the coil
using “brushes,” hence the name brushed DC motors. Unlike brushless DC motors (such
as the stepper motors that will be discussed later in this activity), brushed DC motors are
cheap and offer easier speed control. However, brushed DC motors do not last as long
because the brushes can wear out over time.
1
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo
Brushed DC motors work through an inductive force. When current passes through
the spinning coil, it generates a magnetic field that is either attracted to or repelled by the
stationary magnets, depending on the polarity. By using the brushes to swap the polarity
each half-rotation, you can generate angular momentum.
Direction Control
To control a DC motor from a microcontroller,
a switching arrangement known as H- Bridge is used.
When switches 1 and 4 are closed and 2 and 3 are
open, voltage flows from the supply to 1 to the motor to 4 to
ground. When 2 and 3 are closed and 1 and 4 are open,
polarity is reversed, and voltage flows from the supply to 3
to the motor to 2 to ground.
Although an H-bridge is easy to construct, it is
usually easier to use a controller manufactured
specifically for the job. A pre-manufactured H-bridge chip
will include diodes to protect the transistors from back Figure 2. Schematic of an
voltage, sometimes a current sensing pin to sense the H-bridge Circuit
current the motor is drawing, and much more. One available
chip is the one used in this activity, which is the L293D push-pull four channel driver. This
chip is capable of controlling two DC motors at once.
• GROUND/HEATSINK (pins 4, 5, 12, and 13). The four pins in the middle
connect to a shared ground between your 9V and 5V supplies. They also
heatsink the driver into the ground of your circuit. On a printed circuit board,
2
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo
chips are often designed to shunt excess heat to ground because the ground
often has the most copper surface area.
• VCC2 (pin 8). VCC2 supplies the motor current, so you connect it to 9V.
• VCC1 (pin 16). VCC1 powers the chip’s logic, so you connect it to 5V.
• 1Y and 2Y (pins 3 and 6). These are the outputs from the left driver. The motor
wires connect to these pins.
• 1A and 2A (pins 2 and 7). The states of the switches on the left are controlled
by these pins, so they are connected to I/O pins on the Arduino for toggling.
• 1,2EN (pin 1). This pin is used to enable or disable the left driver. It is connected
to a PWM pin on the Arduino, so that speed can be controlled dynamically.
• 3Y and 4Y (pins 11 and 14). These are the outputs from the right driver.
Because you are using the left driver only, you can leave them disconnected.
• 3A and 4A (pins 10 and 15). The states of the switches on the right are
controlled by these pins, but you are using only the left driver in this example,
so you can leave them disconnected.
• 3,4EN (pin 9). This pin is used to enable or disable the right driver. Because
you will not be using the right driver, you can disable it by connecting this pin
directly to ground.
Speed
The speed of a DC motor is proportional to the supplied voltage. If the voltage drops
too far, the motor won’t get enough power to turn, but within a certain range, usually 50%
of the rated voltage, the motor will run at varying speeds. The most effective way to adjust
the speed is by using pulse width modulation (PWM). This means that you pulse the motor
on and off at varying rates, to simulate a voltage. Below are some examples of pulse
widths and the voltages they would simulate.
Figure 5. PWM signal with a 50% duty cycle. A 50% duty cycle results to an
effective voltage which is about half the total voltage.
Figure 6. PWM signal with a 25% duty cycle. A 25% duty cycle results to an
effective voltage which is about a quarter of the total voltage.
3
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo
Servo Motor
A servo motor is an electromechanical device in which an electrical input
determines the position of the armature of a motor. Servos have integrated gears and a
shaft that can be precisely controlled. They are available in a wide range of sizes and
capabilities, with some modified for continuous rotation, and others designed for rotation
over a small range with high torque.
A servo motor can be a continuous rotation or a standard servo. Unmodified servos
always have a fixed range (usually from 0 to 180
degrees) because there is a potentiometer in
line with the drive shaft, which is used for
reporting the present position. Servo control is
achieved by sending a pulse of a particular
length. In the case of a standard rotation servo,
the length of the pulse determines the absolute
position that the servo will rotate to. If you
remove the potentiometer, however, the servo is
free to rotate continuously, and the pulse length
sets the speed of the motor instead
Unlike their DC motor counterparts, servo
motors have three pins: power (usually red), Figure 7. Servo motors
ground (usually brown or black), and signal or
control (usually white or orange). These wires are color-coded, typically in the same order,
and generally look like the ones shown in Figure 8.
The color-coding might vary slightly between servos, but the color schemes just
listed are the most common. (To make sure always check the servo’s documentation
before experimenting.) Like DC motors, servos can draw quite a bit of a current (usually
more than the Arduino can supply). Although you can sometimes run one or two small
servos directly from the Arduino’s 5V supply, you will generate a separate 5V power
supply for the servos so that you have the option to add more if you need to. Unlike DC
motors, servos have a dedicated control pin that instructs them what position to turn to.
The power and ground lines of a servo should always be connected to a steady power
source.
Servos are controlled using adjustable pulse widths on the signal line. For a
standard servo, sending a 1 ms 5V pulse turns the motor to 0 degrees, and sending a 2
ms 5V pulse turns the motor to 180 degrees, with pulse lengths in the middle scaling
linearly. A 1.5 ms pulse, for example, turns the motor to 90 degrees. Once a pulse has
been sent, the servo turns to that position and stays there until another pulse instruction
is received. However, if you want a servo to “hold” its position (resist being pushed on and
try to maintain the exact position), you just resend the command once every 20 ms. The
Arduino servo commands that you will later employ take care of this for you. The timing
diagram shown in Figure 8 shows how servo control works.
4
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo
Standard servos allow the shaft to be positioned at various angles, usually between
0 and 180 degrees. Continuous rotation servos allow the rotation of the shaft to be set to
various speeds.
The most commonly used methods in the Servo.h library are listed below.
Sample Program:
#include <Servo.h> //import Servo.h library
Servo myservo; //instantiate a Servo object void setup()
{
myservo.attach(9); // attach the servo on pin 9
}
void loop()
{
//set the angle of the servo to 90 degrees myservo.write(90);
}
If Servo Misbehaves
Your servo may behave erratically, and you may find that this only happens when
the Arduino is plugged into certain USB
ports. This is because the servo draws quite
a lot of power, especially as the motor is
starting up, and this sudden high demand
can be enough to drop the voltage on the
Arduino board, so that it resets itself.
If this happens, then you can usually
cure it by adding a high value capacitor
(470uF or greater) between GND and 5V as
shown in figure 9.
The capacitor acts as a reservoir of
electricity for the motor to use, so that when
it starts, it takes charge from the capacitor as
well as the Arduino supply. The longer lead of Figure 9. Servo with capacitor
5
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo
the capacitor is the positive lead and this should be connected to 5V. The negative lead
is also often marked with a '-' symbol.
Stepper Motor
Stepper motors are used to move in
discrete steps, which means you can control the
steeper motors with precision. There are many
usages of stepper motors, which includes
medical scanners, 3D printers, in camera for
auto focus, and many more. In this activity, a
28BYJ-48 Stepper motor will be used to
interface to Arduino Uno using a ULN2003
driver.
Since the stepper motor has a stride angle of 5.625° and a gear ratio of 1:64, we
can determine to total number of steps by multiplying the number of steps in one revolution
with its gear ratio. In the case of this motor:
6
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo
Therefore, we can conclude that this stepper motor has a 4096 Steps in one
revolution while 2048 steps in half revolution.
Control
Step motor converts pulse to angle displacement. So, when the stepper driver
receives a certain pulse signal, it will move the step motor to a certain angle. With this,
the angle of the stepper moved is determined by the number of the pulse. On the other
hand, the speed of the stepper motor is determined by the frequency of the pulse. The
following image is the switching sequence of the stepper motor.
Referring to the table above, we can drive the motor by enabling the pins in an 8-phase
order from left to right. The Lead wire color can be found on the stepper motor. (Clockwise
movement):
7
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo
damage it. In the middle of the board, we have the ULN2003 chip. At the bottom are the
4 control inputs that should be connected to four Arduino digital pins.
IV. DIAGRAM
Figure 13(b). DC Motor Control Connection with Button Switch using H-Bridge
8
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo
9
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo
V. PROCEDURES
Part I. Driving DC Motor
void setup()
{
Serial.begin(9600);
pinMode(inPin1, OUTPUT);
pinMode(inPin2, OUTPUT);
Serial.println("+ - to set direction, any other key stops motor");
}
void loop()
{
if(Serial.available()){
char ch=Serial.read();
if(ch == '+')
{
Serial.println("CW");
digitalWrite(inPin1, LOW);
digitalWrite(inPin2, HIGH);
}
else if(ch == '-')
{
Serial.println("CCW");
digitalWrite(inPin1, HIGH);
digitalWrite(inPin2, LOW);
}
else
{
Serial.println("STOP");
digitalWrite(inPin1, LOW);
digitalWrite(inPin2, LOW);
}
}
}
3. Open the Serial Monitor and type plus ‘+’ and minus ‘-‘sign one at a time and
observed the movement of the DC motor.
10
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo
void setup()
{
//initialize I/O pins
pinMode(inputPin, INPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
}
void loop()
{
//detect state change in the tact switch
currentState = digitalRead(inputPin);
if (currentState != prevState)
{
//if the current state is HIGH then the button
//went from off to on
if (currentState == HIGH)
{
//if the tact switch is pressed, toggle the
//direction of rotation of the DC motor
dir = !dir;
}
}
prevState = currentState;
3. Push the button switch connected to the Arduino. Observe the DC motor as you
push the button switch.
void setup()
{
//initialize I/O pins
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorEnable, OUTPUT);
pinMode(Ledpin, OUTPUT);
//signal start of calibration
digitalWrite(Ledpin, HIGH);
11
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo
void loop()
{
//read LDR value
int x = analogRead(LDRPin);
3. Place your hand over the LDR (Photoresistor) and calibrate the light intensity
moving your hand with varying distance for 5 seconds and after 5 observed the
behavior of the DC motor by placing again your hand over the LDR.
#include <Servo.h>
void loop()
{
//go from 0 degrees to 180 degrees
//in steps of 1 degree
for(pos = 0; pos < 180; pos += 1)
{
12
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo
void setup()
{
//initialize I/O pins
pinMode(Sbutton1, INPUT);
pinMode(Sbutton2, INPUT);
void loop()
{
//call the function posControl1 when signal received
//from pin 2 changes from LOW to HIGH
void posControl1()
{
dir1 = !dir1; //toggle dir1
if (dir1 == HIGH)
{
myservo.write(90);//position servo to 90 degrees
}
else
{
myservo.write(180);//position servo to 180 degrees
}
}
13
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo
void posControl2()
{
dir2 = !dir2; //toggle dir2
if (dir2 == HIGH)
{
myservo.write(45);//position servo to 45 degrees
}
else
{
myservo.write(135);//position servo to 135 degrees
}
}
3. Pressed the switch one after another and observed the movement of the servo
motor
#define IN1 8
#define IN2 9
#define IN3 10
#define IN4 11
int Steps = 0;
boolean Direction = true;//
unsigned long last_time;
unsigned long currentMillis ;
int steps_left=2048;
long time;
void setup()
{
Serial.begin(115200);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
// delay(1000);
}
void loop()
{
while(steps_left>0){
currentMillis = micros();
if(currentMillis-last_time>=1000){
stepper(1);
time=time+micros()-last_time;
last_time=micros();
steps_left--;
}
}
Serial.println(time);
Serial.println("Wait...!");
delay(2000);
Direction =!Direction;
steps_left = 2048;
}
14
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
break;
case 1:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, HIGH);
break;
case 2:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
break;
case 3:
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
break;
case 4:
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
case 5:
digitalWrite(IN1, HIGH);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
case 6:
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
case 7:
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
break;
default:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
}
SetDirection();
}
}
void SetDirection(){
if(Direction==1){ Steps++;}
if(Direction==0){ Steps--; }
if(Steps>7){Steps=0;}
if(Steps<0){Steps=7; }
}
15
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo
B. Speed Control
1. Using the previous connection (half stepping), Encode the sketch below and
upload it to the Arduino board.
void setup() {
//declare the motor pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
Serial.begin(9600);
}
void loop(){
if(count < countsperrev )
clockwise();
else if (count == countsperrev * 2)
count = 0;
else
anticlockwise();
count++;
}
void clockwise()
{
for(int i = 7; i >= 0; i--)
{
setOutput(i);
delayMicroseconds(motorSpeed);
}
}
16
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo
2. Observed what happened to the Stepper motor and try to vary the speed.
a. Create a program that controls the speed and direction of rotation of a DC motor.
Use a button switch to toggle between the forward and reverse rotation of the DC
motor and an LDR to vary its speed. Use the RGB LED to indicate its current
direction of rotation.
b. Using a LDR as an input, create a program that controls the position of the servo
motor. If the ambient light is “dark”, turn the servo motor at 0 degrees; “medium
dark”, 45 degrees; “medium”, 90 degrees; “medium bright”, 135 degrees; and
“bright”, at 180 degrees. Print the detected ambient light intensity and servo’s
angle to the LCD Display.
c. Another stepping mode used to drive stepper motors is the high torque stepping.
Create a program that utilizes this mode for a stepper motor. Use a tact switch to
toggle the stepper’s direction of rotation (clockwise or counterclockwise) and LED
to its direction of rotation.
17
CP E 12 - MICROPROCESSOR SYSTEMS
Department of Computer Engineering
Instructor: Engr. Daniel Gracias V. Esquejo
Functionality (35)
Code Efficiency (35)
Breadboard Component
Layout/Fritzing Design
Connections (20)
Wiring
(Neatness, color coding) (5)
Timeliness (5)
TOTAL EARNED POINTS
18