What is needed is a circuit to interface the motor and Arduino. Such a device is a transistor. A transistor can take a small signal at its base and provide a larger signal at its collector.
Rather than just switching the signal on and off, we will use pulse width modulation (PWM). When controlling motors, PWM provides a number of advantages:
- Motors can work at their maximum torque, even when running at lower speeds.
- Quick response time
Using PWM we will control the speed of the motor by adjusting the average voltage that is applied to the motor – the duty cycle. The Arduino has PWM pins and we can connect one of these to the base of our transistors. The motor is connected between the collector and positive rail of the power supply. The emitter is connected to ground. We can then control the speed of our motor by providing a low power PWM signal from the Arduino to the base of the transistor and a larger signal can flow through the collector, emitter and motor.
We have controlled the speed of the motor but what if we wish to change its direction? To change the direction of the motor we need to change the direction of the current. One solution is to use a H-bridge circuit. The H-bridge uses 4 transistors, with the motor in the center, which looks like a H. As well as building our own H-bridge, you can also use a driver chip, such as the L298N. You can go a step further and buy a module that contains this chip and the necessary circuitry to connect to your Arduino and transistor. This module contains 2 h-bridges, which means that you can control 2 DC motors, each with a voltage from 5 – 35V, with a peak current up to 2A.
The way that this works is to control the speed of the motor by sending a number between 0 and 255.
To make sure that you can supply enough current, use a mains adapter instead of USB.
A DC motor is likely to use more power than a digital output pin of an Arduino can handle. Doing so could damage the Arduino. A transistor can be used as a switch, by using a small current from the Arduino’s digital output pin to control the bigger current of the motor. Current will flow through the collector and emitter when a small current flows into the base. This small current will be supplied by the Arduino’s digital output pin.
The digital output pin of the Arduino is connected to the base of the transistor via a resistor. This is to limit the current. There is a diode in parallel to the motor because when the motor is turned off, there is a negative voltage spike. This could damage the transistor and/or Arduino. Because a diode is only conductive in one direction, this will protect the transistor and Arduino by shorting out this current.
Now swap the connections of the motor. You will see that the motor is now spinning in the opposite direction. It would be useful if we could reverse the motor connections electronically, so that we can control the direction that the motor spins. One way that we can achieve this is by using a H-bridge. A H-bridge is a circuit that switches the polarity of the voltage that is applied to a load. When we manually swapped the motor’s connection, we were changing the polarity of the voltage that was applied to the motor.
Figure 1 shows an H-bridge that uses physical switches. If you look at the shape of the connections to the motor, you will see that it looks like a H. This is where the name is derived from.
Figure 2 shows an H-bridge that uses transistors as the switches.
Figure 3 shows an H-bridge using an L293, quadruple high-current half-H driver.
Please take note that there are 2 versions of the L293. There is the L293 and L293D. I am using the L293D but you can use either. However, if you use the L293 then you will need to add a diode across each motor. This is to protect the L293 and Arduino from being damaged from a discharge from the motor when it is turned off. The L293 has internal diodes and therefore no external ones are required. There is also a further difference, the L293 can be used for a continuour current of 1A and the L293D for 600ma. None of the circuits here will exceed these specifications but it should be noted for any circuits that you may wish to design.