Arduino Tutorial: 1-Way DC Motors

Introduction: So as you probably know, I’ve recently taken the challenge of learning electronics and building a robot. So far I’ve had a couple of problems, but I’ve always found a way out. Something that bugged me for quite some time was how to correctly connect a small DC motor to the Arduino (since an expert friend of mine told me I couldn’t just directly wire it to the Arduino, something I came to understand). After many hours of searching and investigating I found out numerous things, but mainly I found that there is not a coherent and fully abrasive article on assembling a simple DC motor (there probably is, I just didn’t find it). A couple of good videos on YouTube, but they lack of good explanations and circuit schematics. That’s why I decided to write this tutorial. It doesn’t mean necessarily that it is gonna be different than the ones I found, though I’ll try very hard to make it understandable and useful.

Tutorial: What we are going to learn today is how to work a simple DC motor with the Arduino, but first of all we need to understand what is a DC motor.

A DC motor is a simple device that turns electromagnetic energy into mechanical energy. I’m not gonna describe the actual process, but you can easily find it with google (Warning: it isn’t very easy if you don’t know electromagnetism theory). To run such a motor you’re going to feed at least 5V into the circuit (or 4.5V, but the Arduino only feeds 3.3V, 5V  and 9V) and, depending of the model, a maximum of 12V (12V may be too much for the smallest ones). Additionally, it requires an average of 340 mA to keep working and about 1A to start its motion. The problem is that the digital pins of the Arduino can only feed 20mA of current, which is much less than the current required. So we can easily conclude that wiring a simple DC motor to the Arduino is not totally linear.

So what can we do to control a motor? We have to use some kind of external current to do so. Moreover, we need to be able to tell that external current how to operate. To do so we have to use some kind of device that isolates the Arduino from that current, allowing it to control the external current. So we have a couple of options. The first one is the optocoupler, which is a neat component that controls a higher current with a smaller current thanks to the photoelectric properties of certain materials (usually silicone). This basically works by turning on and off a LED in which depends the conductivity of a material on an isolated circuit. Hence, when the LED is on, current flows on the other circuit. When it is off, current doesn’t flow. Another useful device to control external currents is our friend: the transistor. This basically works like a switch depending on the current fed to him by one of its leads: the base. To actually use with a motor I recommend using a transistor because it is more common, cheaper (depending on the model), easier to use, more effective and smaller. Of course this doesn’t render the otpocoupler useless, it is usually used in other things and can be used on this particular one, but as I said, the transistor is better.

The way transistors work is very simple. The main constituents of a transistor are p-type and n-type materials which are semiconductors with funny properties. So there are two main type of transistors: NPN and PNP, which theoretically vary in their polarization, but practically they vary in current’s direction. We’ll be using NPN, which are best for the case. So the transistor works by feeding in current from a terminal called the collector to another called emitter, depending on the value of a third current on its third terminal called base. So if you have a normal circuit with, for example, an LED and you connect the collector and emitter terminals of a transistor to that circuit, you can control the brightness of the LED by feeding more or less current into the base terminal.

This works because when we feed current into the base terminal we saturate it and allow current to flow from the collector to the emitter, like a water valve.

So, what are the practical implication that we can benefit of? What if we connected a motor circuit to an external current feed and plugged a transistor in the middle? Then we’d be able to control the circuit thanks to the base terminal. Simple, hum? So lets do so!

This particular tutorial is gonna be breadboard based, but you can easily port that into a PCB or something else. Pedagogically speaking, breadboards rule. Therefore, the first step is to setup a breadboard with the bus strips (the lateral strips painted red and blue, with the +- signs) connected to the 5V and Ground pins of the Arduino. Then, grab a transistor and pluck into three different pins. The transistor choice is very important. You can choose pretty much any NPN transistor to work with this example, but you have to make sure where is the collector/emitter/base leads and wire them correctly. I’m using one from the Arduino Starter’s Kit, which is the BC547 Transistor from the TO92 Package, but I think you can use any NPN transistor from the TO92 package. Also, make sure the transistor you’re using accepts 5-9V of voltage and 20mA to 1/2A, or things can go wrong (like melting the transistor… so be careful!).

That said, it’s time to wire it up. Firstly connect the DC motor to the 5V pins and to the transistor collector lead. Then parallelly  connect a 100 uF (microfarads) which is the same as a o.1 mF and a regular rectifier diode in the same fashion between the 5V pins and the negative lead of the DC motor. Finally, wire the emitter of the transistor to the ground pins.

With this circuit we have a regular motor circuit, but we still need to be able to control it with the Arduino. So we simple connect a 1K resistor from pin 9 (or any other PWM pin) to the base lead of the transistor. Thus, by sending a pulse-width-modulated signal to the base lead, we can control the amount of current that flows from the collector to the emitter like they were connected to the Arduino!

The following images show the circuit and its schematics:

Additionally, you could replace the current feeder from the Arduino to an external power like a 9V battery or something else. That could be useful if you’re using bigger devices (like a 12V fan or motor) and need more current and more voltage. You must not forget though to always wire the ground pins to all sources, including the Arduino. This will keep the circuit at the same standards and allow the circuit to be “complete”.

The code is pretty basic: a simple output to the motor is enough. So here is it:

int motorPin = 9; // The pin connected to the transistor base
int speed = 200; // A value in the range [0..255] which represents the
// speed of the motor

void setup()
{
  pinMode(motorPin, OUTPUT);
}

void loop()
{
  analogWrite(motorPin, speed);
}

Conclusion: And this is it. You now know how to correctly connect a DC motor to your Arduino. If you have any questions or you found something wrong with the tutorial please tell me about them in the comments section. In my next tutorial I’ll be teaching you how to transform this circuit into one that allows a 2-way motor that is a motor that rotates to both directions, using a structure called an H-Bridge. We’ll first build it manually and next we’ll see how to work a IC (Integrated Circuit) one.

Finally, I leave you with a shot of a similar circuit as in the tutorial, that I did for a video in a previous post (on controlling a motor with a IR sensor), which I may talk about in a further tutorial:

About these ads

One thought on “Arduino Tutorial: 1-Way DC Motors

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s