# Arduino Lesson 13. DC Motors

## Overview

In this lesson, you will learn how to control a small DC motor using an Arduino and a transistor.

![](https://cdn-learn.adafruit.com/assets/assets/000/002/338/medium800/learn_arduino_overview.jpg?1396782013)

You will use an Arduino analog output (PWM) to control the speed of the motor by sending a number between 0 and 255 from the Serial Monitor.

# Arduino Lesson 13. DC Motors

## Parts

To build the project described in this lesson, you will need the following parts.

# Arduino Lesson 13. DC Motors

## Breadboard Layout

When you put together the breadboard, there are two things to look out for.

Firstly, make sure that the transistor is the right way around. The flat side of the transistor should be on the right-hand side of the breadboard.

Secondly the striped end of the diode should be towards the +5V power line - see the image below!

Info: 

![](https://cdn-learn.adafruit.com/assets/assets/000/002/346/medium800/learn_arduino_breadboard.jpg?1396782072)

The motor can be connected either way around.

# Arduino Lesson 13. DC Motors

## Arduino Code

Load up the following sketch onto your Arduino. &nbsp;

```auto
/*
Adafruit Arduino - Lesson 13. DC Motor
*/


int motorPin = 3;
 
void setup() 
{ 
  pinMode(motorPin, OUTPUT);
  Serial.begin(9600);
  while (! Serial);
  Serial.println("Speed 0 to 255");
} 
 
 
void loop() 
{ 
  if (Serial.available())
  {
    int speed = Serial.parseInt();
    if (speed >= 0 && speed <= 255)
    {
      analogWrite(motorPin, speed);
    }
  }
} 
```

The transistor acts like a switch, controlling the power to the motor, Arduino pin 3 is used to turn the transistor on and off and is given the name 'motorPin' in the sketch.

When the sketch starts, it prompts you, to remind you that to control the speed of the motor you need to enter a value between 0 and 255 in the Serial Monitor.

![](https://cdn-learn.adafruit.com/assets/assets/000/002/347/medium800/learn_arduino_serial_monitor.jpg?1396782077)

In the 'loop' function, the command 'Serial.parseInt' is used to read the number entered as text in the Serial Monitor and convert it into an 'int'.

You could type any number here, so the 'if' statement on the next line only does an analog write with this number if the number is between 0 and 255.

# Arduino Lesson 13. DC Motors

## Transistors

The small DC motor, is likely to use more power than an Arduino digital output can handle directly. If we tried to connect the motor straight to an Arduino pin, there is a good chance that it could damage the Arduino.

A small transistor like the PN2222 can be used as a switch that uses just a little current from the Arduino digital output to control the much bigger current of the motor.

![](https://cdn-learn.adafruit.com/assets/assets/000/002/348/medium800/learn_arduino_transistor.png?1396782158)

The transistor has three leads. Most of the electricity flows from the Collector to the Emitter, but this will only happen if a small amount is flowing into the Base connection. This small current is supplied by the Arduino digital output.

The diagram below is called a schematic diagram. Like a breadboard layout, it is a way of showing how the parts of an electronic project are connected together.

![](https://cdn-learn.adafruit.com/assets/assets/000/002/349/medium800/learn_arduino_schematic.jpg?1396782164)

The pin D3 of the Arduino is connected to the resistor. Just like when using an LED, this limits the current flowing into the transistor through the base.

There is a diode connected across the connections of the motor. Diodes only allow electricity to flow in one direction (the direction of their arrow).

When you turn the power off to a motor, you get a negative spike of voltage, that can damage your Arduino or the transistor. The diode protects against this, by shorting out any such reverse current from the motor.

# Arduino Lesson 13. DC Motors

## Other Things to Do

Try reversing the connections to the motor. What happens?  
  
Try entering different values (starting at 0) into the Serial Monitor and notice at what value the motor starts to actually turn. You will find that the motor starts to 'sing' as you increase the analog output.  
  
Try pinching the drive shaft between your fingers. Don't hold it like that for too long, or you may cook the transistor, but you should find that it is fairly easy to stop the motor. It is spinning fast, but it does not have much torque.

[Click Here for the Next Lesson](http://learn.adafruit.com/adafruit-arduino-lesson-14-servo-motors)
 **About the Author**  
  
Simon Monk is author of a number of books relating to Open Source Hardware. The following books written by Simon are available from Adafruit: [Programming Arduino](https://www.adafruit.com/products/1019 "Link: https://www.adafruit.com/products/1019"), [30 Arduino Projects for the Evil Genius](https://www.adafruit.com/products/868 "Link: https://www.adafruit.com/products/868") and [Programming the Raspberry Pi](https://www.adafruit.com/index.php?main_page=adasearch&q=programming+raspberry+pi "Link: https://www.adafruit.com/index.php?main\_page=adasearch&q=programming+raspberry+pi").
## Related Guides

- [Adafruit IO Home: Lights and Temperature ](https://learn.adafruit.com/adafruit-io-house-lights-and-temperature.md)
- [CLUE Metal Detector in CircuitPython](https://learn.adafruit.com/clue-metal-detector-circuitpython.md)
- [Color Remote with Circuit Playground Bluefruit](https://learn.adafruit.com/color-remote-with-circuit-playground-bluefruit.md)
- [Crickit Paddle Wheel Boat](https://learn.adafruit.com/crickit-boat.md)
- [Make it Move with Crickit](https://learn.adafruit.com/make-it-move-with-crickit.md)
- [Integrating Home Assistant with Adafruit IO](https://learn.adafruit.com/integrating-adafruit-io-with-home-assistant.md)
- [Programming with Scratch 2 or 3 on Raspberry Pi](https://learn.adafruit.com/programming-with-scratch-on-raspberry-pi.md)
- [Scrambled Number Security Keypad](https://learn.adafruit.com/scrambled-number-security-keypad.md)
- [Working with Multiple Same Address I2C Devices](https://learn.adafruit.com/working-with-multiple-i2c-devices.md)
- [Desktop Dumpster Fire](https://learn.adafruit.com/desktop-dumpster-fire.md)
- [MIDI for Makers](https://learn.adafruit.com/midi-for-makers.md)
- [micro:bit Lesson 2. Controlling LEDs on Breadboard](https://learn.adafruit.com/micro-bit-lesson-2-controlling-leds-on-breadboard.md)
- [micro:bit Lesson 4. Sensing Light](https://learn.adafruit.com/micro-bit-lesson-4-sensing-light-and-temperature.md)
- [Getting Started with Raspberry Pi Pico and CircuitPython](https://learn.adafruit.com/getting-started-with-raspberry-pi-pico-circuitpython.md)
- [Raspberry Pi E-Ink Weather Station using Python](https://learn.adafruit.com/raspberry-pi-e-ink-weather-station-using-python.md)
