How to Get Started with L298N Motor Driver module using Arduino

Introduction

The L298N motor driver module is a cheap solution when you need to drive a 2 DC motors or 1 stepper motor. It is a popular motor driver module to the DIYers and hobbyist due to ease of use and availability.

Features

  • It has a dual H-bridge motor driver.
  • Onboard 5V linear voltage regulator so it means you can supply the voltage requirements of the other circuit.

Below is the pin assignments and its configuration.

Power Terminal Block: +12V: - is the positive motor supply voltage

  • if motor supply voltage is more than 12V, make sure the jumper pin in CON5 is removed. This is to protect the onboard 5V linear voltage regulator from damage. GND: - is the common ground for the L298N and the interface microcontroller +5V: - it is an input voltage supply for the L298N IC when the onboard 5V linear voltage regulator is disabled (jumper pin in CON5 is removed).
  • it is an output voltage supply for the interface microcontroller when the linear voltage regulator is enabled (jumper pin in CON5 is in placed).

Bidirectional Motor Driver Output: MotorA: motor driver output for MotorA MotorB: motor driver output for MotorB

Enable and Speed Controller: enA: enable pin A

  • Pull down (connect to GND) to disable motor driver output for MotorA
  • Pull up (connect to VCC) to enable motor driver output for MotorB (similar results is achieve using jumper pin)
  • PWM to control speed of the motor

enB: enable pin B

  • Pull down (connect to GND) to disable motor driver output for MotorA
  • Pull up (connect to VCC) to enable motor driver output for MotorB (similar results is achieve using jumper pin)
  • PWM to control speed of the motor

Direction Logic Controller: IN1 and IN2: - direction logic controller for MotorA

IN3 and IN4: - direction logic controller for MotorB

PWR jumper pin: - this is to enable onboard voltage regulator

Circuit Diagram

NOTES:

  1. Referring to the circuit diagram, I am supplying the L298N with 6V battery. +6V is connected to the +12V pin motor supply voltage.
  2. Battery GND pin is connected to the GND pin of the L298N.
  3. Arduino GND is also tied to the L298N GND pin, this is to create a common reference ground.
  4. 5V pin of Arduino is tied to the L298N +5V pin to supply voltage for the L298N IC. Please notice also that the jumper pin in the CON5 is removed to disable the onboard 5V voltage regulator of the L298N.
  5. DC motor is connected to the terminal block MotorB in the right side.
  6. IN3 and IN4 is tied to Arduino Uno digital pin 13 and 12 to control the direction of motor rotation.
  7. EnB is tied to Arduino Uno digital pin 11. This pin has a capability of outputting a Pulse Width Modulation (PWM) signal. This is to control the speed of rotation of the motor.

Video Demonstration

Source Code

 1#define IN3B 13
 2#define IN4B 12
 3#define PWMB 11
 4
 5String cmd="";
 6bool isBEnabled = false;
 7byte PWMB_val = 100;
 8
 9void setup() {
10  Serial.begin(9600);
11  pinMode(IN3B, OUTPUT);
12  pinMode(IN4B, OUTPUT);
13  pinMode(PWMB, OUTPUT);
14  digitalWrite(IN3B, LOW);
15  digitalWrite(IN4B, LOW);
16  digitalWrite(PWMB, LOW);
17}
18
19void loop() {
20  checkSerial();
21}
22
23
24void checkSerial() {
25 //Read data from Serial
26 while(Serial.available()>0){
27   cmd+=(char)Serial.read();
28 }
29 
30 //Select function with cmd
31 if(cmd!=""){
32    cmd.trim();  // Remove added LF in transmit
33    // We expect Command from bluetooth
34    if (cmd.equals("E")) {
35      Serial.println("Enabling motor");
36      enableB();
37    } else if(cmd.equals("R")) {
38       Serial.println("CW Rotation");
39       forward();
40    }else if (cmd.equals("L")){
41       Serial.println("CCW Rotation");
42       backward();
43    }else if(cmd.equals("U")){
44       Serial.println("Speed up");   
45       speedUp();
46    }else if(cmd.equals("D")){
47       Serial.println("Speed down");
48       speedDown();
49    }
50    cmd=""; //reset cmd
51  }
52
53void enableB() {
54  if (isBEnabled) {
55    // motor is running, now disable it
56    isBEnabled = false;
57    digitalWrite(PWMB, 0);
58  } else {
59    // motor is disabled, now run it
60    isBEnabled = true;
61    analogWrite(PWMB, PWMB_val);
62  }
63}
64
65void forward() {
66  digitalWrite(IN3B, LOW);
67  digitalWrite(IN4B, HIGH);
68}
69
70void backward() {
71  digitalWrite(IN3B, HIGH);
72  digitalWrite(IN4B, LOW);
73}
74
75void speedUp() {
76  if (PWMB_val < 245) {
77    PWMB_val = PWMB_val + 10;
78    analogWrite(PWMB, PWMB_val);
79  }
80}
81
82void speedDown () {
83  if (PWMB_val > 9) {
84    PWMB_val = PWMB_val - 10;
85    analogWrite(PWMB, PWMB_val);
86  }
87}

Call To Action

That’s all for now. If you have any questions or clarification, please feel free to leave your comments and suggestions in the comment box. Thank you and have a good day. Happy tinkering.



Posts in this series



No comments yet!

GitHub-flavored Markdown & a sane subset of HTML is supported.