L298N Motor Driver with Arduino Leave a comment


The L298N is a popular dual H-bridge motor driver integrated circuit commonly used in robotics and other projects to control DC motors or stepper motors. It allows you to control the direction and speed of two DC motors independently.

Features and Connections:

  1. Dual H-Bridge: The L298N has two H-bridges, allowing it to control two motors independently.
  2. Motor Power Supply: It requires an external power supply for the motors, typically 7V to 35V. Ensure the voltage matches your motor specifications.
  3. Logic Power Supply: It also needs a separate power supply (usually 5V) for its logic circuitry.
  4. Motor Connections: Connect your motors to the OUT1/OUT2 and OUT3/OUT4 pins for Motor 1 and Motor 2, respectively.
  5. Control Pins: The control pins are used to control the direction and speed of the motors. The logic inputs IN1, IN2, IN3, and IN4 control the direction, while PWM (Pulse Width Modulation) signals applied to the ENA and ENB pins control the motor speed.
  6. Enable Pins (ENA and ENB): To control the speed of the motors, you can apply a PWM signal to these pins. The higher the PWM value, the faster the motor spins.
  7. Direction Pins (IN1, IN2, IN3, IN4): These pins determine the direction of the motor. For instance, setting IN1=HIGH and IN2=LOW might make the motor turn in one direction, while reversing those values will change the motor’s direction.

Remember to use appropriate heat sinks if the motor driver gets hot during prolonged usage, and always handle the connections carefully to prevent short circuits.

***************************************************************************************************************

***************************************************************************************************************

Replace the PINs (enA, in1, and in2) with the actual pins you’ve connected the L298N to on your Arduino board. The code demonstrates how to control the direction and speed of the motor. Adjust the analogWrite(enA, 200) value to change the motor speed (values between 0-255).

Make sure to power the L298N separately with an appropriate voltage source, connect the motor to the outputs of the L298N, and connect the control pins (IN1, IN2, ENA) to the Arduino as specified in the code.

This code is a simple demonstration. For a more sophisticated control scheme or for incorporating inputs like potentiometers or sensors, you’d modify the loop() function accordingly.

Arduino Code

* Company Name:LK Tronics
* Author: Research & Development Department
EXPERT GUIDANCE TO BRING YOUR DREAM PROJECT TO LIFE
* Date: 2022/ 01/20
* https://lk-tronics.com/
***********************************************
* All rights reserved. No part of this code may be reproduced, distributed,
* or transmitted in any form or by any means, including photocopying, recording,
* or other electronic or mechanical methods, without the prior written permission
* of LK Tronics, except in the case of brief quotations embodied in
* critical reviews and certain other noncommercial uses permitted by copyright law.

************************************************/
int enA = 9;
int in1 = 8;
int in2 = 7;

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);

  // Set initial rotation direction
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);

  // Initialize serial communication
  Serial.begin(9600);
}

void loop() {
  // Motor speed control using PWM on enable pin (enA)
  analogWrite(enA, 200); // Change this value (0-255) for speed control
  
  // Move the motor in one direction for some time
  Serial.println("Motor rotating in one direction");
  delay(2000); // 2 seconds delay

  // Change rotation direction
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);

  // Move the motor in the opposite direction for some time
  Serial.println("Motor rotating in the opposite direction");
  delay(2000); // 2 seconds delay

  // Stop the motor
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  
  Serial.println("Motor stopped");
  delay(2000); // 2 seconds delay
}

L298N Motor Driver Module (lk-tronics.com)

VISIT OUR FACEBOOK PAGE FOR MORE INFO | MORE PRODUCT IN OUR SHOP

Leave a Reply