Introduction: How to Drive a DC Motor With Transistor - Arduino Tutorial

Picture of How to Drive a DC Motor With Transistor - Arduino Tutorial

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!

Step 1: What You Will Need

Picture of 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

Picture of 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

Picture of 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!

Picture of 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!

Comments

Ryan Wright (author)2017-01-02

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?

wbarrows1 (author)2016-04-05

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.

Saiyam (author)2015-07-13

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

codebender_cc (author)Saiyam2015-08-15

Hello, here you can find two "How to" tutorials, with the L293D and L298 module IC motor drivers.

https://www.instructables.com/id/How-to-use-the-L29...

https://www.instructables.com/id/How-to-use-the-L29...

codebender_cc (author)Saiyam2015-07-13

Hi! We will post a "How to" tutorial with motor driver IC soon!

Thank you

Bemur22 (author)2015-08-06

Hi, I love this tutorial, but the problem for me is I don't have those exact transistors and diodes. I have Mosfet transistors [IRF520], Diodes [1N4007]. Could I use these parts instead of the ones in the tutorial??

Bemur22 (author)Bemur222015-08-06

The dc motor I have is a 6/9 volt motor

codebender_cc (author)Bemur222015-08-15

Hi! I just read the datasheet of IRF520 and I saw that it has a built-in protection diode. Try to use it as it.

Also, if you want to drive dc motors take a look on those how to tutorials:

https://www.instructables.com/id/How-to-use-the-L29...

https://www.instructables.com/id/How-to-use-the-L29...

Famagroup made it! (author)2015-08-10

It Works. Very cool. Now I need to discover how to spin the motor to the other direction, or it just invert the + and ground?

codebender_cc (author)Famagroup2015-08-15

Hello, here you can find two "How to" tutorials, with the L293D and L298 module IC motor drivers.

https://www.instructables.com/id/How-to-use-the-L29...

https://www.instructables.com/id/How-to-use-the-L29...

cheekid (author)Famagroup2015-08-12

Hi, search for h bridge. I think that's what you're looking for .

codebender_cc (author)2015-07-13

Thats true ;)

dmwatkins (author)2015-07-13

Very well done how-to!

About This Instructable

24,757views

102favorites

License:

Bio: Arduino Tutorials by Codebender.cc Team
More by codebender_cc:How to use a magnetic door switch / sensor with ArduinoHow to Measure LUX With ArduinoHow to Control an RGB LED Strip - Arduino Tutorial
Add instructable to: