Motor control with a transistor

Analog outputs can be very useful sometimes and not just to change the brightness of an LED. There are plenty of devices that operate on an analog signal; motors, for example, where you can change its speed by varying the voltage you apply to them.

However, motors can be sometimes tricky to operate, mainly due to the fact that they are big current consumers. A typical toy DC motor can easily consume more than 200 mA when running without a load and up to 1 Amp when stalled.

We mentioned in the previous chapter that an Arduino pin can't give more than 40 mA, or it could burn. So, how can we deal with a motor using an Arduino? Well, usually when dealing with high-current devices, we use a driver circuit that allows the device to be powered from an external power source and use the Arduino pin just as a regulator.

This way, we can avoid the need to power the device directly from the Arduino pin, like we have done till now when dealing with LEDs that operate with under 30 mA and can be directly fed from Arduino pins.

Motor driver

This kind of circuit usually uses a transistor as an intermediate device that receives the Arduino signal and provides a proportional current coming from an external power source to the device.

In the following schematic, you can see a typical assembly when operating a DC motor from Arduino:

Motor driver

A motor driver circuit

The key element of this circuit is the transistor component, the one with the arrow pointing out that has three legs:

  • Base: This is the leg that we connect to the Arduino board through a resistor and that acts as the control element
  • Collector: This, simply said, is the leg where current comes into the transistor
  • Emitter: This is the leg through which current flows out of the transistor

Regarding its operation, I like to compare a transistor with a water tap, where we turn the handle and, more or less, water comes out of it. In the transistor case, we set a variable voltage on the base, which allows more or less current to flow from the collector to the emitter and consequently, through all components we connect in between.

Keeping this in mind and taking a closer look at the previous schematic, we can realize that by connecting the base to an Arduino analog output, we can make a varying current flow through the motor, thus making its speed vary.

When dealing with transistors, it is important that you know which of the three legs is the base, collector, and emitter of your transistor. You should refer to its datasheet, which you can easily find on sites such as http://octopart.com.

There are two more components on the circuit that deserve a short explanation:

  • The resistor: This allows the connection between the Arduino pin and the transistor base. Without going into too much detail, we will use a 1K resistance here.
  • The diode: Without going into too much detail, you should know that a motor acts much like a big electromagnet and thus, when turned off, generates what is called a back Electromagnetic Force (EMF), which can even be 100 volts and with an opposite direction from the main voltage applied to the motor, which could damage your circuit. By placing a diode in the opposite direction of the main voltage, you help suppress this dangerous back EMF. Any protection diode will do, such as the 1N4007, but if you don't have one to hand, you could even use an LED as long as you connect it correctly.

Power source considerations

There is a last question to consider before going hands on with our motor control circuit, and that is the powering of the motor.

For the purposes of this example, I'll suppose that you are going to connect a small DC motor that consumes no more than 250 mA when operating without a load. This way, we could take current directly from the 5V pin of the Arduino board.

It's important to keep this in mind, because the power regulator present in your Arduino board takes current in the last instance from your USB port, which can't typically support more than 500 mA.

You should take some precautions before connecting a bigger motor to your Arduino board by measuring its typical power consumption. You can do this by placing an ammeter in the serial with a battery or another external power source and take note of the current it needs to run.

If the motor doesn't need more than the maximum 500 mA that your USB port and Arduino voltage regulator can give, then you can safely power it from the 5V Arduino pin. In other cases, you will have to provide another power source for the motor and connect both grounds: the one for your external power source and the one for your Arduino board.

The complete circuit

Well, that is enough theory for the moment; let's go into the real nuts and bolts of our circuit. Here, you have the complete schematic of the motor speed control circuit we are going to assemble for this example:

The complete circuit

A motor speed control circuit

Connections diagram

For the breadboard connections, we can save some wires by placing the diode just in contact with the transistor collector.

Notice that for the transistor used in this diagram, the legs are looking at it with the plain side in front from the left to the right, collector, base, and emitter, or, as you will usually find, CBE.

Connections diagram

A breadboard connections diagram for the motor speed control circuit

Motor varying speed code

The code for this circuit is very similar to the LED fading one, but instead of using the for loop to go all over the range of values of the analog output, we will just use it to generate a three-iterations loop that will give us just three different speeds for the motor, which will make its changing state more evident.

Further, I couldn't make my motor spin with a voltage lower than 1V, so I will configure the loop to take the values 150, 200, and 250, which made my transistor deliver voltages of 1.5V, 2.5V, and 3.5V.

Here, you have the complete code I used for this example:

/*
 Chapter 04 - Controlling outputs softly with analog outputs
 Motor speed control
 By Francis Perea for Packt Publishing
*/

// Global variables we will use
int base = 6;
int speed = 0;

// Configuration of the board
void setup() {   
  // Set the pin that we will connect to the transistor base as an output
  pinMode(base,OUTPUT);
}

// Main loop
void loop() {
  // Increment the speed of the motor in three steps, each for 3 seconds
  for (speed=150; speed<=250; speed=speed+50){
   analogWrite(base, speed);
   delay(3000);
  }
  // Stop the motor for 1 second and begin again
  analogWrite(base, 0);
  delay(1000);
}

Notice that every change in speed lasts three seconds and that once the three steps' loop ends, we stop the motor for one second before beginning all over again.

The assembled circuit

I'd like to show you a picture of the complete assembly that could perhaps clarify even more what we are doing:

The assembled circuit

A real assembly for the motor speed control circuit

As you can see, I've used a little piece of tape around the motor axle in order to make it clear when it changes its speed.

Bigger power motors

As I told you previously, when dealing with motors, this can consume more current than the maximum 500 mA your USB port can give. You will have to provide another power source just for the motor, such as an external battery or DC transformer.

I have used an excellent and totally recommendable free software called EAGLE (http://www.cadsoftusa.com) to prepare a schematic for this kind of circuit that can perhaps help you understand what we pretend.

Bigger power motors

A circuit to power the motor from an external power source

Here, you can see how the motor gets its current from the big battery and the only component between both circuits is the transistor, which is connected through its base to the Arduino board, its collector to the external power source, and the emitter to both grounds: the battery one and the Arduino one.

This way, you can not only provide a bigger current but also a different voltage to the motor that could, for example, operate at 12V as opposed to the 5V of the Arduino board.

One final consideration with respect to this circuit is that you should look for an appropriate transistor, because every transistor has a maximum current that it can support, so be careful if you don't want to see a little fireworks show on your desktop.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.249.104