L298N Robot-How to drive DC motors with L298N and Arduino

Basic Code:

/*  Arduino DC Motor L298N Module H-bridge DC Motor
    By Chen The Design Maker  chenthedesignmaker.com
*/

//Define all the connections maps to the L298N
#define enA 10
#define in1 9
#define in2 8

#define in3 7
#define in4 6
#define enB 5

void setup() {
    // Setup pin mode
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  // Set initial direction and speed
  digitalWrite(enA, LOW);
  digitalWrite(enB, LOW);
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}

void loop() {
  //Drive Motor Forward
  analogWrite(enA, 255);
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);

  delay(2000);

  //Drive Motor Backward
  analogWrite(enA, 255);
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);

  delay(2000);

}

Advanced Code:

/*  Arduino DC Motor L298N Module H-bridge DC Motor
    By Chen The Design Maker  chenthedesignmaker.com
*/

//Define all the connections maps to the L298N
#define enA 10
#define in1 9
#define in2 8

#define in3 7
#define in4 6
#define enB 5

class Motor{

    int enablePin;
    int directionPin1;
    int directionPin2;
    public:
    
    //Method to define the motor pins
    Motor(int ENPin,int dPin1,int dPin2){
      enablePin = ENPin;
      directionPin1 = dPin1;
      directionPin2 = dPin2;
    };     
    
    //Method to drive the motor 0~255 driving forward. -1~-255 driving backward
    Drive(int speed){
    if(speed>=0){
        digitalWrite(directionPin1, LOW);
        digitalWrite(directionPin2, HIGH);
      }
    else{
        digitalWrite(directionPin1, HIGH);
        digitalWrite(directionPin2, LOW);
        speed = - speed;
      } 
    analogWrite(enablePin, speed);  
    }
  };
  Motor leftMotor = Motor(enA, in1, in2);
  Motor rightMotor = Motor(enB, in3, in4);
  
void setup() {
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  // Set initial direction and speed
  digitalWrite(enA, LOW);
  digitalWrite(enB, LOW);
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}


void loop() {

leftMotor.Drive(255);
rightMotor.Drive(200);

delay(2000);

leftMotor.Drive(-150);
rightMotor.Drive(-255);

delay(2000);

leftMotor.Drive(150);
rightMotor.Drive(150);

delay(2000);
 

}

Parts list:

L298N Module:

Banggood:https://www.banggood.com/Wholesale-Dual-H-Bridge-DC-Stepper-Motor-Drive-Controller-Board-Module-Arduino-L298N-p-42826.html?p=JH2514288496201309O3

Amazon: http://amzn.to/2j8DWmT

 

3-6V gear DC motor:

Banggood: https://www.banggood.com/Plastic-Tire-Wheel-With-DC-3-6v-Gear-Motor-For-Arduino-Smart-Car-p-997113.html?p=JH2514288496201309O3

Amazon:http://amzn.to/2CGGcu6

 

12V DC motor:

Banggood:https://www.banggood.com/DC-12V-356065110220rpm-Worm-Gear-Box-Reduction-Motor-p-1197020.html?p=JH2514288496201309O3

Amazon:http://amzn.to/2AZzxhu

 

Helping hand:

Banggood: https://www.banggood.com/Multifunctional-Welding-Magnifier-LED-Helping-Hand-Soldering-Iron-Stand-Magnifying-Lens-Magnifier-Clamp-Tool-p-1061332.html?p=JH2514288496201309O3

Amazon: http://amzn.to/2olH6ZK

 

4AA Battery holder

Banggood:https://www.banggood.com/4-X-AA-Battery-Holder-Case-Enclosed-Box-OFFON-Switch-With-Wires-p-925732.html?p=JH2514288496201309O3

Amazon: http://amzn.to/2CFwesR

 

Male to female cable:

Banggood:https://www.banggood.com/3-IN-1-120pcs-10cm-Male-To-Female-Female-To-Female-Male-To-Male-Jumper-Cable-Dupont-Wire-For-Arduino-p-1054670.html?p=JH2514288496201309O3

Amazon:http://amzn.to/2AXgBzP

 

DISCLAIMER: This post contains affiliate links, which means that if you click on one of the product links, I’ll receive a small commission. This help support the channel and allows us to continue to make videos like this. Thank you for the support!

Leave a Reply

Your email address will not be published. Required fields are marked *