Using Two L298N Modules to Control Four DC Motors with a Library

Using Two L298N Modules to Control Four DC Motors with a Library

This project guide demonstrates how to control up to four DC motors using two L298N motor driver modules and a custom Arduino library. The L298N is a popular and cost-effective dual full-bridge driver capable of controlling two DC motors per module with full speed and direction control. By utilizing two modules, you can independently manage four motors, making this setup ideal for complex robotics projects like 4-wheel drive vehicles, robotic arms with multiple joints, or any application requiring coordinated movement from several motors.

The custom "Robojax_L298N_DC_motor" library simplifies the control process, allowing you to command motors with intuitive functions for setting speed (as a percentage) and direction (clockwise or counterclockwise). This guide will walk you through the hardware explanation, wiring for both standard and high-power parallel modes, and how to use the library effectively.

Hardware Explained

The L298N module is based on the L298N dual full-bridge driver IC. Each module can control two DC motors independently. A full bridge consists of two half-bridges, which are necessary for bidirectional control (in video at 03:44). For each motor, you use three control pins: an Enable pin (ENA or ENB) for speed control via PWM, and two Input pins (IN1/IN2 or IN3/IN4) for direction control (in video at 05:00).

The module can accept a motor power supply voltage from 5V up to 35V and can deliver up to 2A per bridge (in video at 03:08). It also includes a 5V regulator, which can be used to power your Arduino board if the input voltage is 7V or higher, eliminating the need for a separate USB connection during operation (in video at 03:21). The module comes with built-in protection diodes to handle voltage spikes from the inductive loads of the motors (in video at 08:14).

Wiring Guide

Wiring the modules correctly is crucial. For each motor on a module, you must connect the two motor wires to the two output terminals (OUT1/OUT2 or OUT3/OUT4). The module's power input terminals (often labeled 12V and GND) are connected to your external motor power supply (e.g., a 12V battery). The 5V output from the module can be connected to the Arduino's 5V pin to power it, but remember to disconnect this when programming the Arduino via USB (in video at 11:29).

The control pins must be connected to PWM-capable pins on the Arduino for the Enable pins (ENA, ENB). These pins are marked with a tilde (~) on the Arduino board (e.g., pins 3, 5, 6, 9, 10, 11 on an Uno). The direction control pins (IN1, IN2, IN3, IN4) can be connected to any digital pin. For two modules controlling four motors, you will need a total of 12 digital pins from your Arduino.

Code Explanation

The provided code uses a custom library to simplify motor control. The user-configurable section is at the top, where you define the connection pins for each motor on each module. You do not need to modify the logic in the loop() function; it is provided as a demonstration.

The first step is to define the pins for each motor. For each module, you define pins for two motors. It is critical that the Enable (ENA/ENB) pins are connected to Arduino pins that support PWM (indicated by a ~ symbol).

// motor 1 settings for Module 1
#define IN1a 2
#define IN2a 4
#define ENAa 3 // this pin must be PWM enabled pin

// motor 2 settings for Module 1
#define IN3a 5
#define IN4a 7
#define ENBa 6 // this pin must be PWM enabled pin

// motor 3 settings for Module 2
#define IN1b 8
#define IN2b 9
#define ENAb 10 // this pin must be PWM enabled pin

// motor 4 settings for Module 2
#define IN3b 13
#define IN4b 12
#define ENBb 11 // this pin must be PWM enabled pin

Next, you create objects for each L298N module. The library constructor takes the pin definitions for both motors on that module. The final true parameter enables debug messages over the serial monitor, which can be set to false once your project is working correctly.

//define module one object
Robojax_L298N_DC_motor module1(IN1a, IN2a, ENAa, IN3a, IN4a, ENBa, true);

//define module two object
Robojax_L298N_DC_motor module2(IN1b, IN2b, ENAb, IN3b, IN4b, ENBb, true);

To control a motor, you use the rotate(motorNumber, speed, direction) function. The motorNumber is 1 or 2, referring to the motors on that specific module object. The speed is an integer from 0 (stop) to 100 (full speed). The direction is either CW for clockwise or CCW for counterclockwise. To stop a motor abruptly, use the brake(motorNumber) function.

module1.rotate(motor1, 60, CW);//run motor1 on module1 at 60% speed CW
module2.rotate(motor2, 40, CCW);//run motor2 on module2 at 40% speed CCW

Live Project Demonstration

After uploading the code and completing the wiring, the demonstration shows all four motors operating (in video at 18:38). The code runs each motor through a sequence of speeds and directions. The serial monitor outputs debug information, showing the commanded speed and direction for each motor. This visual feedback is helpful for troubleshooting and verifying that each motor responds correctly to its commands. For larger motors that require more current, the guide also explains how to wire two bridges on a single module in parallel to double the current capacity (in video at 22:06).

Chapters

  • [00:01] Introduction and Project Preview
  • [01:12] Tutorial Overview and Library Introduction
  • [01:57] Hardware Explained: L298N Module
  • [09:02] Wiring Explained for Two Motors
  • [11:39] Code Explanation and Library Usage
  • [18:38] Demonstration with Two DC Motors
  • [20:32] Demonstration with a Larger Motor
  • [22:06] Wiring for Parallel (High-Power) Mode
  • [25:09] Demonstration of Parallel Mode

Images

L298N Motor Controller Module
L298N Motor Controller MOdule
250-Code to control 4 DC motors by using 2 L298N Modules
Language: C++
Copied!

Resources & references

No resources yet.

Files📁

No files available.