Lesson 14: DC Motors DC Motors Lesson 14: The Big Idea
Lesson 14: DC Motors DC Motors Lesson 14: The Big Idea
DC Motors
Motors
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.
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.
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.
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);
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.
void setup(){
pinMode(pinMC1, OUTPUT);
pinMOde(pinMC2, OUTPUT);
}
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. 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.
The terminals of the DC motor are delicate and easily broken. The wiring tech-
nique shown here will protect these terminals.
Caution
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.
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.
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);
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.