DC Motor Drives, Voltage, Direction, H-Bridge, PWM

Arduino Tut#16 – DC Motor Drives, Voltage, Direction, H-Bridge, PWM

DC Motor Drives – Voltage, Frequency, PWM, H-Bridges and more…

DC Motors are inexpensive, small and can be ran from very low voltages through a Motor Drive. This makes them attractive options for makers who are interested in robotics. You can run a DC motor by supplying a voltage difference across its leads. However, you need to overcome certain challenges in order to drive them effectively. The most common goals are variable speed and direction. In other words, you want to be able to vary the RPM and direction of your motor. Both of these can be achieved with a motor driver, but before getting into those, it is important to understand the basics.

DC Motors – Variable Speeds

motor driver DC motorThe speed of a DC Motor can be varied by applying different voltage differences to its leads. For example, a motor will run faster if 12V instead of 3V is applied to it. You can leverage this property in several ways. If you are looking to have two speeds, slow and fast for example, you can drive the same motor through two different transistors. One would supply a lower voltage for a low speed setting and the other a higher voltage for the high speed. You can obviously supply as many different voltages in the same fashion. Your second, and more viable option, is to have a variable voltage controller. Depending on your circuit requirements, you can either step up or step down your voltage. As an example, I’ve built an Arduino circuit in my Tutorial where I have access to a 12V power supply. For those reasons, I chose the step down method (Step Down Converter) and can comfortably supply 3V-12V to my motor leads.

I know what you’re thinking; I can send a PWM signal from my Arduino and drive the motor. DO NOT wire a motor directly to your control board. From the Atmega328 Datasheet (Arduino’s micro controller) you will see that you can only supply 40mA per pin. This is way below the rated current of your motor.

DC Motors – H-Bridge Motor Drivers

We have looked at power drivers in one of my previous tutorials. You can connect a motor to a relay or transistor. However, you will limit yourself in one crucial way: direction switching. Unless you want your motor to rotate in one way only (such as a fan), you need circuitry capable of switching directions. Here is where H-Bridges come into the picture. Through the use of 4 semiconductors, the H-Bridge will drive the current in both directions.

Required Hardware

For this tutorial, you will require the following:

or any other H-Bridge

  • Jumper Wires

Hardware Setup

If you are using the L298 Module, the connections are self explanatory. You will have to connect the Arduino to In1, In2 and EN1. The PWM signal will be sent to the EN1 pin in order to vary the speed. If you have chosen an H-bridge chip, you will have to refer to its datasheet in order to complete the connections.

Arduino DC-Motor Driver

You need to worry about two things: direction and PWM. The direction is set through two pins: In1/In2. If In1 = HIGH / In2 = LOW your motor will spin in one direction. If In1 = LOW / In2 = HIGH, it will spin in the other. Obviously, it will depend on which lead you’ve connected to the output pins of the motor drive. The PWM is applied to the EN pin and is easily implemented as discussed in one of my earlier tutorials (Arduino PWM Tutorial).


int set1 = 3;
int set2 = 4;
int enable = 5;
void setup()
{
pinMode(set1, OUTPUT);
pinMode(set2, OUTPUT);
pinMode(enable, OUTPUT);
}
void loop()
{
digitalWrite(set1, LOW); // Forward motion
digitalWrite(set2, HIGH);
for (int m_speed = 70; m_speed < 255; m_speed++)
{
analogWrite(enable, m_speed);
delay(50);
}
digitalWrite(set1, HIGH); // Reverse motion
digitalWrite(set2, LOW);
for (int m_speed = 70; m_speed < 255; m_speed++)
{
analogWrite(enable, m_speed);
delay(50);
}
}

The above code is self explanatory. What I’m doing is essentially setting the direction of the motor through set1/set2 pins. I’m using the PWM pin enable to vary the speed. Lastly, I’m sweeping from 70 to 255 PWM. The reason behind the 70 came from the fact that the motor stalls below that setting.

As always, thank you guys for watching/reading! If you’ve enjoyed my tutorial, make sure to subscribe, leave me a comment and stay tuned for more.
– EEEnthusiast