In one of my last posts I have shown how to drive a servo motor. Servo motors comes in two flavours: the standard servo motors can be set such that they reach a given position and maintain it; the continuous rotation servo can rotate continuously at a given speed.
DC motors are very similar to continuous rotation servo motors, in fact. With respect to the latter, DC motor technology is much simpler and, as a consequence, these motors are cheaper than servos. As suggested by their name, DC motors need just a DC voltage source to run, so there is no need to use any PWM pin. They just have two wires to be connected to a voltage source. The direction of rotation depend on how you connect the wires to the voltage source. Reversing the polarity of the power makes the motor run in the opposite direction. The speed of a DC motor depends on the applied voltage: the higher the voltage, the higher the speed. Motor specifications tell you in which voltage range it operates: below the minimum voltage the motor will probably do not move at all or they move with lot of difficulty; above the maximum voltage you seriously risk to damage it.
Some DC motor, as the one we used for our tests, comes with a gear embedded in their body: gears make the motors more powerful, but slower.
If, for any reason, you need to control the rotation of a DC motor using Arduino, in principle it would be enough to connect one of the motor leads to the GND pin and the other to any digital pin. Setting the pin to LOW the motor doesn't move. Setting the pin to HIGH the motor runs if 5V is in its range. Given the fact that the direction of rotation depends on the polarity of the voltage, inverting the wires makes the motor rotate in the opposite direction.
In some cases, however, this very simple schema cannot work: manifestly it does not work if 5V are not enough to run the motor; moreover, the maximum current that an Arduino pin can provide is 40 mA (but it is recommended to not exceed 20 mA) and it may not be sufficient to run the motor. In those cases you need an external power source. You cannot use Arduino as the power source, but you can use it as the driver of the motor, controlling an external power source used to feed the motor. Suppose, for example, that you need at least 9V to run your motor. You can use Arduino to realise a sort of switch that stays open until a given digital pin is set to HIGH. As soon as that pin flips from LOW to HIGH, the switch closes and the motor is connected to the power source.
Of course you can use a relays to this purpose, but we are going to illustrate the use of a transistor to obtain the same result. Understanding how to use transistors is always a good idea, even if you are not going to use them: knowledge is power!
Transistors can be thought as a sort of switches. In fact transistors may act both as amplifiers as well as switches, depending on how they are polarised. If you want to understand how transistors work in a non technical manner you can read my article published on 'The Physics Teacher' (Giovanni Organtini, "A flush toilet model for the transistor", Phys. Teach. 50, 221 (2012); http://dx.doi.org/10.1119/1.3694073). In order to understand this post you don't need to understand how transistor work: it is enough to know that they behave like a switch. The switch connects the transistor emitter with its collector and its operation is driven by its base. Consider the following circuit.
The Arduino board is used just to provide a digital signal on a given pin (pin 9 in the example). This pin is connected to a resistor (whose value is not very important: its purpose is just to limit the current flowing out of the pin) that, in turn, is connected to the base of a transistor. If no current flows to the transistor base, the transistor is said to be in interdiction and acts as an open switch between the emitter and the collector. In our schema the motor is connected to a 9V battery trough the transistor, but you can imagine the transistor being substituted by an open switch. The Arduino ground is in common with the 9V minus lead.
As soon as you make enough current flowing into the transistor base, the transistor goes in saturation and acts as a closed switch. In that case the transistor emitter (the lower lead with the arrow in the schema) appears to be short circuited with its collector (the top lead, connected to the motor). In this case the current can flow from the battery and feed the motor that starts running.
Below you can see a Fritzing model of the device where you can see that the base of the transistor is its central lead; the emitter is on the left, while the collector is on the right
Below you can see our actual setup.
In order to identify the collector, the base and the emitter of a transistor you must refer to its data sheet. There are common rules among producers, allowing you to identify the leads looking at the transistor shape (the one shown in the picture above is called TO-92). However, it is always a good idea to check the pinout on the specific transistor data sheet, because not all the manufacturers may use the same pinout for the same enclosure. The actual pinout, in fact, depends also on the transistor type.
There are two types of transistors: NPN and PNP. Transistors are made out of three semiconductor slices and semiconductors comes in two flavours: N-type and P-type semiconductors. In N-type semiconductors the current carriers are electrons, while in P-type semiconductors the current is due to holes (lack of electrons). In NPN transistors the base is made of a P-type semiconductor, while in PNP ones the base is an N-type semiconductor.
In the above example we showed how to use an NPN transistor. If you are going to use a PNP one, you should connect the collector to the positive lead of the battery and the emitter to the motor, while the second lead of the motor goes directly to ground.