Chapter 4

Using Transistors and Driving Motors

Chapter Summary

This chapter covered the following concepts:

  • DC motors use electromagnetic induction to create mechanical action from changes in current.
  • Motors are inductive loads that must utilize proper protection and power circuitry to interface safely with your Arduino.
  • DC motor speed and direction can be controlled with PWM and an H-bridge.
  • Servo motors enable precise positioning and can be controlled using the Arduino Servo library.
  • A linear regulator can be used to create a secondary 5V supply from a 9V battery.
  • IR distance sensors return analog values representing distances detected by bouncing infrared light off objects.
  • Code commenting is critical for easing debugging and sharing.

Parts List

Useful Links


  • On page 73, in the “Creating Short Circuits with H-Bridges” excerpt, the book reads: “What would happen if both switches on the left or both switches on the right were opened?” It should read: “What would happen if both switches on the left or both switches on the right were closed?”
  • In Figure 4-14, There is a wire missing. The top and bottom GND strips on the breadboard must be connected.

Color Wiring Diagrams


Follow along with this video for a visual tutorial on motors and transistors:

Watch a demo of the sweeping IR distance sensor created in the chapter:


Chapter 4 Code Download

All code is licensed via the GNU GPL v3. Code is maintained and updated on GitHub. The download zip linked above always contains the most recent version of the code examples that have been pushed to the GitHub Code Repository.

Taking it Further

With your skills from this chapter, combined with your knowledge of analog and digital inputs and outputs and your growing knowledge of the Arduino programming knowledge, you are already equipped to do a number of exciting projects. You can accomplish the following few things by using knowledge you already have. Try building one of these ideas or coming up with your own:

  • A roving car with a scanning distance sensor that can avoid walls
  • A motion-sensitive light
  • A personal thermostat and fan (Fans are just DC motors with blades on them.)
  • A code-activated lock box (Push debounced buttons in a certain order to disengage a servo motor.)
  • A light-activated jack-in-the-box that uses a servo motor to pop up when you turn on the lights

References & Credits

  • Figure 4-2: Transistor Symbol Credit: Public Domain from Wikipedia. (source)
  • Figure 4-6: H-Bridge Datasheet excerpts Credit:  Image used with permission courtesy of Texas Instruments, (source, PDF)
  • Figure 4-9: Photo of Parallax servo Credit: Used with permission from Parallax Inc. Copyright © 2013 Parallax Inc. All rights reserved. Photo by Jeremy Blum.