[tutorial] How to drive a DC Motor with Transistor


motor and transistor

To drive a DC motor you need a larger amount of current than Arduino board can give. For that reason you must use a transistor. Transistors have limits and maximum specs, just be sure those values are enough for your use.

The transistor we are using for this tutorial is P2N2222A and is rated at 40V and 200mA, it just perfect for one toy dc motor.

Note: If your motor needs more current than 200mA you can just buy another transistor (ask the staff in the electronics store). The connections below are the same ;-)

In this tutorial we will spin a dc motor from one direction, with different speed. You will be able to control motor speed from serial monitor!

So, let's get started!

What you will need


For this tutorial you will need:

  • Arduino uno
  • Breadboard
  • 220 Ohm resistor
  • Transistor P2N2222A
  • Diode 1N4148
  • DC Motor

The Circuit


The connections are easy, see the image above with the breadboard circuit schematic.

Diode in circuit:

It is possible, when motor is spinning, suddenly turn off because the magnetic field inside it collapses, generating a voltage spike. This can damage the transistor, to prevent this, we use a diode which diverts the voltage spike around the transistor.

Transistor Pinout:

PNP type - front side view:

  • Collector
  • Base
  • Emitter

The Code


Here's the code, embedded using codebender!

Try downloading the codebender plugin and clicking on the Run on Arduino button to program your Arduino board with this sketch. And that's it, you've programmed your Arduino with this sketch.

Press connect button below to start serial communication, try to send number from 50 to 255.

Well done!


You have successfully completed one more Arduino "How to" tutorial and you learned how to drive a DC Motor with Transistor.