• Worldwide Shipping

• FAQ
• Worldwide Shipping

• FAQ

# HUM: DC motor driver dual H-bridge

You are a beginner with Croduino. Or electronics? A specific module caught your eye, but you do not know how to use it? Do not worry, HUM is here for you! How to Use Module (HUM) is a blog tutorials series by e-radionica where you will find all you need in order to begin working with your favorite module. Tutorials include: technical characteristics, work principle, instructions on how to connect module with Croduino and the basic code. Everything else is left to your imagination.

## Introduction

In the last tutorial we have encountered stepper motors and explained their work principles. You must have noticed that the motor was not directly connected to Croduino, but rather via driver. You probably wonder why do we even have to use the driver for motor? Why can't we only connect the sole outputs of the motor to Croduino pins and in the program code change directions and speed of motor rotations? The answer is because of Croduino's limited output power. Most motors require greater power than the one provided by Croduino's outputs. In order to protect Croduino, and ensure reliable operation of the motor, we use external power supply. Drivers are electronic circuits which combine every single component necessary for reliable motor operation into one circuit. Using drivers, it is possible to control speed and direction of motor's rotations. One of them is the L298N driver whom we will further explain in this tutorial.

Characteristics:
• Dual Motor Controller
• Max. power: 25W
• Voltage: 5 - 35V (for motor), 5V for logic
• Current: 2A
• Dimensions: 43 x 43 x 23 mm

## How does it work?

L298N driver for motor uses the so-called H-bridge integrated chip. H-bridge is a circuit which consists of 4 switches connected to the motor in a way shown in the image below. If we close S2 and S3 switches, and open S1 and S4, the current will flow in a certain direction. If we shift states of switches so that the S2 and S3 are open, and S1 and S4 closed, the current will flow through the motor in the opposite direction. Besides, we must not close all four switches at once or both switches on one side of the H-bridge (e.g. S1 and S2) because it would induce a short circuit.

The table below shows every possible combination of switch states and their outcome.

With the H-bridge, it is very easy to shift polarity on the load. Most commonly, they are used for changing DC motor's rotation directions. The H-bridge is also used in other applications such as DC/AC, AC/AC or DC/DC converters. The L298N driver is designed for motors but can be used for other devices which require a shift of polarities on their outputs, for certain uses. With the possibility of shifting polarities, L298N driver has the capacity to control the motor's rotation speed using PWM (eng. Puls-Width Modulation) signal. We must note that L298N driver also contains two outputs which can be used to control the operation of two motors.

## How to connect it to Croduino?

L298N driver contains several connectors that are listed and explained below:

OUT1 – +/- Output for the motor A
OUT2 - +/- Output for the motor A
OUT3 - +/- Output for the motor B
OUT4 – +/- Output for the motor B
CON5 – Serves to activate a 5V voltage regulator. It is turned on by setting up the jumper (if the input voltage is less than 12V).
EnA – Serves to control PWM for the motor A (it is necessary to place the jumper if we want to control motor rotation speed)
EnB - Serves to control PWM for the motor B (it is necessary to place the jumper if we want to control motor rotation speed)
In1 - Serves for controlling motor A rotation direction
In2 - Serves for controlling motor A rotation direction
In3 - Serves for controlling motor B rotation direction
In4v - Serves for controlling motor B rotation direction
+5V – The input where an external power supply 5V source is placed (if the jumper is not set onto the CON5). The output provides 5V (if the jumper is placed on the CON5).
GND – Ground. It is necessary to connect all circuit's minuses with this GND
+12V – The input of the external power supply for motors. You must remove the jumper on CON5 if voltage is greater than 12V. Max 35V DC.

## Arduino code

For controlling motors using L298N drivers, it is not necessary to use any library. We run the motors by turning the output on/off using digitalWrite() function. For motor rotation speed, we use analogWrite() function.

```// this program rotates motors 2 seconds in one, then 2 seconds in the other direction
// defining pins for the first motor
int enA = 10;
int in1 = 9;
int in2 = 8;
// defining pins for the second motor
int enB = 5;
int in3 = 7;
int in4 = 6;

void setup()
{
// set all connected pins as output
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}

void loop()
{
// turn the first motor on
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// set the rotation speed of the first motor to 200 (max 255)
analogWrite(enA, 200);
// turn the second motor on
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// set the rotation speed of the second motor to 200 (max 255)
analogWrite(enB, 200);
delay(2000);

// Replace motor rotation directions
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(2000);

// turn both motors off
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
delay(2000);
}
```