Other Arduino Codes and Videos by Robojax

Using 2 L298N Modules to control 4 DC Motors with library

دروس آردوینو به فارسی

Control 4 DC Motors with 2 L298N Motor Driver modules

This this code shows how to use 2 L298N Modules to control 4 DC Motor fully with Robojax Library. Please watch video for instruction for 2 motors Here. Download library.

L298N Motor Controller MOdule

Resources for this sketch

Code to control 2 DC motor using L298N Module


 /*
 * Library Example for L298N Module to control DC motors
 * 
 * This code is to control 4 DC motors by using 2 L298N Modules
 * 
 * Written by Ahmad Shamshiri on Jan 02, 2021 at 08:12
 (Alex M. requested from YouTube)
 * in Ajax, Ontario, Canada. www.robojax.com
 * 
 Visit the main code to control 2 DC motors https://robojax.com/learn/arduino/?vid=robojax_L298N-DC-Motor
 
 Need wiring diagram from this code: Purchase My course on Udemy.com http://robojax.com/L/?id=62
 * 
 * Watch video instruction for this code: https://youtu.be/wn5D7j1ybxY
 * 
 * Get this code and other Arduino codes from Robojax.com
Learn Arduino step by step in structured course with all material, wiring diagram and library
all in once place. Purchase My course on Udemy.com http://robojax.com/L/?id=62

If you found this tutorial helpful, please support me so I can continue creating 
content like this. You can support me on Patreon http://robojax.com/L/?id=63

or make donation using PayPal http://robojax.com/L/?id=64

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <Robojax_L298N_DC_module1.h>
// motor 1 settings
#define IN1a 2
#define IN2a 4
#define ENAa 3 // this pin must be PWM enabled pin

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

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

// motor 3 settings
#define IN3b 13
#define IN4b 12
#define ENBb 11 // this pin must be PWM enabled pin


const int CCW = 2; // do not change
const int CW  = 1; // do not change

#define motor1 1 // do not change
#define motor2 2 // do not change
#define motor3 1 // do not change
#define motor4 2 // do not change

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

//define module 2 objects
Robojax_L298N_DC_motor module2(IN1b, IN2b, ENAb, IN2b, IN3b, ENBb, true);

void setup() {
  Serial.begin(115200);
  module1.begin();//module 1 starts
  module1.begin(); //module 2 starts 
  //L298N DC Motor by Robojax.com

}

void loop() {
  
  ///////////////////////////// module 1 tests
  module1.demo(1);
  module1.demo(2);  
  module1.rotate(motor1, 60, CW);//run motor1 at 60% speed in CW direction
  module1.rotate(motor2, 60, CCW);//run motor2 at 60% speed in CCW direction  
  delay(3000);

  module1.brake(1);
  module1.brake(2);
  delay(2000);

  ///////////////////////////// module 2 tests
  module2.demo(1);
  module2.demo(2);  
  module2.rotate(motor3, 60, CW);//run motor3 at 60% speed in CW direction
  module2.rotate(motor4, 60, CCW);//run motor4 at 60% speed in CCW direction  
  delay(3000);

  module2.brake(1);
  module2.brake(2);
  delay(2000);
 
  
}

   

If you found this tutorial helpful, please support me so I can continue creating content like this. support me via PayPal