How to Drive a DC Motor With Transistor - Arduino Tutorial




About: Arduino Tutorials by Team

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!

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: What You Will Need

For this tutorial you will need:

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

Step 2: 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

Step 3: 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.

Step 4: Well Done!

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

I hope you liked this, let me know in the comments.

There will be more of them, so make sure to click Follow button!

1 Person Made This Project!


  • Made with Math Contest

    Made with Math Contest
  • Multi-Discipline Contest

    Multi-Discipline Contest
  • Robotics Contest

    Robotics Contest

13 Discussions


1 year ago

Will my arduino board get damaged if i dont use the diode


1 year ago

I built the circuit using a 330 Ohm resistor and the diode gets really hot.... thoughts as to why?
Great tutorial regardless.

1 reply

Reply 1 year ago

I'm getting heat in the transistor. I've experimented with 330-ohm, 1K-ohm, and 10K ohm... with 10K ohm doesn't drive the motor. The heating of the diode and/or transistor is confusing to me.... how to make it so the motor can run a while and not overheat the transistor?

Ryan Wright

2 years ago

Well done guide just one quick question. You mention that in order to source more than 200 mA a larger transistor is required, however the datasheet appears to say it can continuously source 600 mA. Can you clarify?


3 years ago

Diode in circuit: Explanation might be clearer if re-written. May I suggest:

When the motor is spinning and then turned off it is possible that the motor will generate some electric current before it stops spinning. We use a diode to prevent the current from damaging the other circuitry.


4 years ago

Good one. Maybe you should post a tutorial to turn a motor clockwise and anticlockwise using a motor driver.

2 replies

4 years ago

Very well done how-to!