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

Lesson 14: DC Motors DC Motors Lesson 14: The Big Idea

Uploaded by

Mr. Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Lesson 14: DC Motors DC Motors Lesson 14: The Big Idea

Uploaded by

Mr. Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Lesson 14 Lesson 14: DC

DC Motors
Motors

The Big Idea:


The DC motor is a simple device, requiring only a connection to a
source of direct current of voltage capable of delivering the current
the motor requires. This simplicity, however, brings with it three
potential liabilities. First, these motors have electricity-conducting
brushes that produce electrical interference. This can cause prob-
lems with sensors and other devices that work by watching for and
interpreting pulses. Figure 14-1. DC motor
with gear box

Second, to develop much power DC motors turn at speeds too high for directly driving wheels for
cars or propellers for boats. Gearing is used to reduce the revolutions per minute to values more
appropriate for such purposes. The motor shown in Figure 14-1 is attached to a set of gears for
just this purpose. The gears are in the yellow housing. The motor is the attached silver and black
cylinder.
Third, DC motors are difficult to control precisely and, thus, are unsuitable for fine work such
as driving the head on a 3D printer. This can be compensated some by adding components for
detecting the speed and direction of the motor. The disc on the motor in Figure 14-1 contains
magnets that are part of an encoder, a device for just this purpose.

14
Despite their limitations DC motors are useful because they are easy to control, respond quickly to
changes in speed, make a satisfying whirring sound when spinning, and are inexpensive.
In this lesson we learn how to provide appropriate power to a motor and control its direction and
speed from an Arduino.™

Background:
This lesson refers to brushed DC motors. Other types are also in common use, particularly brush-
less motors and servo motors. The theory of operation is not discussed here. Wikipedia offers an
explanation and animated illustrations of the operation of DC motors at https://en.wikipedia.org/
wiki/DC_motor.

Voltage regulation
One characteristic of the DC motor that is very important for the purposes of this lesson is the
motor's electrical resistance. The slower the motor turns the lower its electrical resistance and,
therefore, the greater its demand for electrical current.

Lesson 14    DC Motors  |  159


The reason this is important is that in these lessons the motors are powered by batteries. Batteries,
in turn, have an internal resistance. As the resistance of a motor goes down, the voltage across the
motor also drops as the proportion of the battery's internal resistance to total resistance increases.
The result is all motors being driven by the batteries slow. To the observer, the DC motors just
seem to be running rather poorly.
To minimize that effect, this lesson uses a battery with a higher voltage than what was intended
for the motor and a voltage regulator to maintain the voltage delivered to the motor at a lower
but constant value. This approach works because the internal resistance of the battery impacts the
voltage being delivered to the regulator, not to the motor.

Electrical noise
The DC motor turns because a magnetic field generated by coils of wire switches polarity frequent-
ly. This switching is accomplished by brushes that are constantly breaking and then making elec-
trical connections. Each make and break generates a tiny spark that can be picked up inductively
by nearby wiring, possibly contaminating whatever electrical messages are being conveyed by these
wires.
This interference cannot be completely eliminated, but we do take two suppression steps to reduce
the interference to an acceptable level.
One step is the soldering of a 0.1 microfarad capacitor across the electrical terminals of each motor.
The other is the careful preparation and dressing of the wires that bring the electricity to each mo-
tor. In particular, these wires are:
1. made just long enough to reach the power source,
2. twisted tightly together, and
3. routed in a way that keeps them away from the wiring of devices that are sensitive to interfer-
ence.

H-bridge
The direction a DC motor turns is dictated by the polarity of the electricity delivered to the motor
terminals. Unlike the servo motor described in Lesson 11, with a DC motor, specifying a pulse
width cannot control the motor's direction and speed. However, the digital pins of an Arduino™
can operate an electronic switch capable of controlling the motor's direction and speed.
This device is called an H-bridge. Its internal operations aren't described here, but Figure 14-2
shows how one is configured for use with an Arduino.™ The direction of the attached motor is the
result of the values written to the Arduino™ pins.

160 | Learn to Program in ArduinoTM C: 18 Lessons, from setup() to robots


Figure 14-2. H-bridge configured for use with Arduino™ Uno

The H-bridge is an integrated circuit. The five-volt connection to the Arduino™ powers the cir-
cuit's logic. The six volts from the voltage regulator is power for the motor. The Arduino™ pins are
set to OUTPUT mode. If and which direction the DC motor turns depends on the values written
to those pins, as shown in Table 14-1.
Table 14-1. Results of Arduino™ pin values
Pin connected to MC1 Pin connected to MC2 Motor
HIGH HIGH Stops

14
HIGH LOW Turns counterclockwise
LOW HIGH Turns clockwise
LOW LOW Stops

Speed
Any pair of Arduino™ digital pins can control the direction of a DC motor. If the digital pin
connected to MC1 is set to HIGH and the digital pin connected to MC2 to LOW, the motor will
turn clockwise, at full speed. One way to control the speed of a DC motor is to reduce the average
voltage. This can be done if one of the digital pins is capable of analog output. Analog output and
how it can result in a lower average voltage is discussed in Lesson 9.
For the example just given, the clockwise rotation of the motor can be controlled by setting the pin
connected to MC1 to send a series of pulses instead of being set to HIGH.
Suppose the motor has connections as seen in Table 14-2.

Lesson 14    DC Motors  |  161


Table 14-2. Arduino™ pin connections to H-bridge
Arduino™ Pin H-bridge connection Notes
3 MC1 Analog-capable
4 MC2 Not analog-capable

The programming statements that cause the motor to turn full-speed counterclockwise are:
digitalWrite( 3, HIGH); // full-speed counterclockwise
digitalWrite( 4, LOW);

The following programming statements will also cause the motor to turn full-speed:
analogWrite( 3, 255); // full-speed counterclockwise
digitalWrite( 4, LOW);

The speed of the motor can now be reduced by reducing the average output of pin 3. This is ac-
complished by reducing the second parameter to a value less than 255.
analogWrite( 3, 200); // less than full-speed
digitalWrite( 4, LOW);

Note: if the analog-capable pin is set to LOW instead of HIGH, the speed control process is similar,
but instead of reducing the parameter from 255 it is increased from 0. The following statements
cause the motor to turn clockwise at full speed:
analogWrite( 3, 0); // Clockwise at full speed
digitalWrite( 4, HIGH);

and at reduced speed:


analogWrite( 3, 100); // Clockwise at reduced speed
digitalWrite( 4, HIGH);

Table 14-3. Vocabulary


Term Definition
DC motor A type of motor that uses brushes to cause changes in a magnetic field in or-
der to turn. Requires a two-wire connection, one positive and one negative.
Speed is controlled by changing the supplied voltage.
H-bridge An electronic circuit that can be used to control the polarity of the voltage
delivered to a DC motor.
162 | Learn to Program in ArduinoTM C: 18 Lessons, from setup() to robots
Term Definition
pinout A description or cross-reference of each pin of an electrical component and
its function. It typically takes the form of a table or diagram.

Description:
The amount of current drawn by a DC motor can be quite high. Further, the speed of a DC mo-
tor changes as the supply voltage changes. For these reasons a voltage regulator is used to supply a
constant 6 volts from a 9-volt battery pack.

Programming and controlling the DC motor


The programming to control a DC motor is discussed in Background. Unlike working with
servo motors (Lesson 11) no library need be imported. The only statements required in the
setup() method are those for initializing the digital pins to be used to OUTPUT. Defining the
pin numbers at the beginning of the sketch is simply good practice.
Example 14-1. Initializing sketch for control of DC motor connected to Arduino™
pins 3 and 4
#define pinMC1 3
#define pinMC2 4

void setup(){
pinMode(pinMC1, OUTPUT);
pinMOde(pinMC2, OUTPUT);
}

H-bridge integrated circuit


The actual H-bridge used for this lesson is the L293 integrated circuit from Texas Instruments. The
pinout of this circuit is shown in Figure 14-3.
14

Figure 14-3. Pinout of H-bridge integrated circuit used in this lesson

Lesson 14    DC Motors  |  163


This circuit is capable of delivering up to one amp per motor, though at such levels a heat sink
should be attached. These lessons do not make such demands on the H-bridge, so no heat sink is
required.

Goals:
By the end of this lesson readers will
1. Know how to wire a DC motor in such a way as to protect the motor pins and reduce
electrical noise.
2. Know how an H-bridge works and how to connect one to a power source, an Arduino™
and to a DC motor.
3. Know how to program an H-bridge to control a motor's direction and speed.

Materials:
Quan- Catalog
Part Image Notes
tity Number
Single-board computer. This
board is delicate and should
1 Arduino™ Uno be handled with care. When 3102
you are not using it, keep it in
a box or plastic bag.
This is a standard USB adapt-
er cable with a flat connec-
1 USB Cable 2301
tor on one end and a square
connector on the other.
Computer with at least
one USB port and ac- The operating system of this
1 cess to the Arduino™ --- computer must be Windows, ---
website, Macintosh OS/X, or Linux.
http://www.arduino.cc.

1 DC motor DC motor attached to gears. 3132

Holds 6 AA cells for a total of


1 Battery holder 3114
+9 volts.

164 | Learn to Program in ArduinoTM C: 18 Lessons, from setup() to robots


Quan- Catalog
Part Image Notes
tity Number

6-volt regulator capable of


1 Voltage regulator 1104
delivering up to 1 amp.

1 Capacitor 0.33 mfd 0202

1 Capacitor 0.1 mfd 0203

1 Bread-board Used for prototyping. 3104

As Used with bread-boards for


Jumper wires 3105
req'd. wiring the components.

Fit Arduino™ power jack.


1 Plug 3109
2.1mm.

1.5 volt, AA to fit battery


6 Alkaline cells 3118
holder.
14
Can be used to control one or
1 H-bridge 1307
two DC motors.

Lesson 14    DC Motors  |  165


Procedure:

1. Attach the power plug to the wires of the battery holder. The red wire solders to the center tab
of the holder and the black to the outside tab, as shown in Figure 14-4.

Figure 14-4. Power plug assembly

2. Prepare the DC motor for connection to the H-bridge.

The terminals of the DC motor are delicate and easily broken. The wiring tech-
nique shown here will protect these terminals.
Caution

a. Begin by attaching the 0.1 microfarad capacitor across


the motor terminals. Do not solder the connections yet.

Figure 14-5. Attaching capacitor


to terminals of DC motor
b. Cut 12-inch lengths each of red and black #24 solid copper hookup wire. Bare approxi-
mately ¼ inch of wire at each end of each wire. Solder the wires to the motor terminals,
taking care to solder the capacitor leads as well.

c. Route each wire under the plastic motor retainer and tie a
knot over the top. The purpose of this knot is to protect the
motor terminals from flexing when the red and black wires
are moved.

Figure 14-6. Wire routing on DC


motor with square knot
166 | Learn to Program in ArduinoTM C: 18 Lessons, from setup() to robots
d. Twist the remainder of the red and black wire
tightly together.

Figure 14-7. Twisted wire routing


on DC motor

Together, the capacitor and wire twisting act to reduce the electrical interference caused by the
motor's brushes.
3. Connect the DC motor and the H-bridge integrated circuit to the Arduino™ as shown in Fig-
ure 14-8. Notice how the voltage regulator is used to supply the H-bridge with 6 volts.

A mistake in wiring that shorts the output of the voltage regulator to ground can
cause the regulator to become very hot, hot enough to raise a blister if touched.
Caution Take care with the wiring.

14

Figure 14-8. Wiring diagram for control of DC motor by Arduino™ via H-bridge
Lesson 14    DC Motors  |  167
4. Place the six AA cells in the battery holder and plug it into the Arduino.™ The green "ON"
light on the Arduino™ should be lit. The motor might or might not start to turn. This depends
on the state of pins 3 and 4 from whatever sketch was uploaded to the Arduino.™
5. Connect the Arduino™ to the computer and open the Arduino™ IDE. Create a new sketch.
Name it Lesson14DCMotorTest.
6. Enter the following programming statements into this new sketch. This sketch can also be
downloaded from LearnCSE.com.

168 | Learn to Program in ArduinoTM C: 18 Lessons, from setup() to robots


Complete listing 14-1. Lesson14DCMotorTest
/* Lesson14DCMotortest.ino
* by W. P. Osborne
* 7/5/15
*/

#define MC1 3 // pin to connect to MC1


#define MC2 4 // pin to connect to MC2
#define CW 1 // motor to turn clockwise
#define CCW 2 // motor to turn counterclockwise
#define STOPMOTOR 3 // motor to stop

void setup() {
// Initialize the pins sending the control
// signals to the h bridge IC
pinMode(MC1, OUTPUT);
pinMode(MC2, OUTPUT);
}

void loop() {
// run counterclockwise for five seconds
runMotor(CW);
delay(5000);

// stop for one second


runMotor(STOPMOTOR); 14
delay(1000);

// run counterclockwise for five seconds


runMotor(CCW);
delay(5000);

// stop for three seconds


runMotor(STOPMOTOR);
delay(3000);
}

// PRECONDITION: direction is set to CW, CCW, or


// STOPMOTOR. Other values are ignored.
// POSTCONDITION: if the value of direction is recognized
Lesson 14    DC Motors  |  169
// the MC1 and MC2 pins of the Arduino™ are set to the
// values required to set the direction of the motor
// attached to the h-bridge.
void runMotor(byte direction){
if(direction == CW){
digitalWrite(MC1, LOW);
digitalWrite(MC2, HIGH);
} else if(direction == CCW){
digitalWrite(MC1, HIGH);
digitalWrite(MC2, LOW);
} else if(direction == STOPMOTOR){
digitalWrite(MC1, LOW);
digitalWrite(MC2, LOW);
}
}

Notice the following about this sketch:


• It makes use of definitions at the top to create constants that can be referred to in the pro-
gramming code. This improves the readability of the statements.
• The actual commands are sent to the H-bridge via a helper method, runMotor.

Exercise:
Exercise 14-1. Changing turning direction and speed
Create a new sketch named Lesson14Exercise1 that is identical to the sketch called
Lesson14DCMotorTest.ino except that the loop() method in the new sketch is modified
so that after turning counterclockwise for five seconds the motor pauses one second, turns clock-
wise at a noticeably slower rate for five seconds, pauses for one second, turns counterclockwise at
the same slower rate for five seconds, then pauses three seconds.

170 | Learn to Program in ArduinoTM C: 18 Lessons, from setup() to robots

You might also like