How to Drive a DC Motor With Transistor - Arduino Tutorial

53,714

115

20

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

About: Arduino Tutorials by Codebender.cc 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!

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!

Recommendations

  • Battery Powered Contest

    Battery Powered Contest
  • Plywood Challenge

    Plywood Challenge
  • Plastic Contest

    Plastic Contest

20 Discussions

0
Nathanjf95
Nathanjf95

2 months ago

I'm still trying to wrap my head around the PNP, NPN differences but if I wanted to use an NPN transistor here instead, could I just swap the flyback diode to the other leg of the transistor and wire into the grounded side?

0
pathaklaukik
pathaklaukik

5 months ago

2n2222 is a npn transistor, u have connected it as a pnp transistor??

0
Alex Schroeder
Alex Schroeder

Reply 4 months ago

While 2n2222 is a npn transistor, p2n2222 is a pnp transistor. They are pretty similar but the collector and emitter are switched around.

0
Bemur22
Bemur22

Reply 5 years ago on Introduction

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

0
UserInstru
UserInstru

Reply 6 months ago

there is not reason to 'tune' the components with the same chosen by the author of the instructables. There is not a thin range where chose the components. the 2N2222 is a general purpose good transistor and the 1N4007 is an honest rectifier, hard to die. In this example the motor is a little motor and just for this the 2N2222 it's enough for this use, while the 1N4007 is much more than enough. You can chose another available transistor, preferably more hard in current than the 2N2222, and for the diode can be enough a similar starting from 1N4001 to up. To finish: the choose of all the components are depending from the motor that everyone of use want use for this useful lesson.

0
Saiyam
Saiyam

5 years ago

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

0
UserInstru
UserInstru

Reply 6 months ago

To run a motor in both directions, it's not obligatory buy a 'DRIVER' in the 'transistor contest' of this instructables. I think that the author want show us "how to" work with the basic components. So, a driver is just an IC containing four transistors connected as an 'H bridge'. H, is a figure that well describe the way the transistors are connected into the driver IC. We can use four transistors to build an 'H bridge' without buy any 'driver'.
The resulting circuit will not a beauty to see, it will be a forest of colored wires on a white breadboard, but it will be useful to learn how an 'H bridge' works. This still adherent with the scope of this instructables.

0
codebender_cc
codebender_cc

Reply 5 years ago on Introduction

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

Thank you

1
wbarrows1
wbarrows1

4 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.

0
NeilRG
NeilRG

Reply 7 months ago

It has more to with the back EMF which generates very short duration voltage spikes in a brush type motor as it self commutates, not only as the motor comes to a stop.

0
MRUDUL18
MRUDUL18

2 years ago

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

0
NeilRG
NeilRG

Reply 7 months ago

Why risk it? Second guess the instructable here at your own risk. Diodes cost a fraction of an arduino.

0
paulfecht
paulfecht

3 years ago

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

0
paulfecht
paulfecht

Reply 3 years 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?

0
Ryan Wright
Ryan Wright

3 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?

0
dmwatkins
dmwatkins

5 years ago

Very well done how-to!