Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Control DC Motors with Arduino Using the L298N Driver

The L298N motor driver is a popular choice for controlling DC motors with Arduino due to its ability to handle high currents and its dual H-bridge configuration, which allows for controlling two motors simultaneously. This article will guide you through the process of setting up and controlling DC motors using an Arduino and an L298N motor driver.

Understanding the L298N Motor Driver

The L298N motor driver module is based on the L298N IC, which is capable of driving two DC motors independently. It can control the direction and speed of the motors by adjusting the voltage and current supplied to them. The module typically includes:

  • Two H-bridges for controlling two motors.
  • A 5V regulator to power the logic circuitry.
  • Terminal blocks for motor and power connections.
  • Pin headers for connecting to a microcontroller like Arduino.

Components Required

  • Arduino Uno (or any compatible board)
  • L298N motor driver module
  • DC motors (up to 2)
  • Power supply (suitable for your motors, typically 7-12V)
  • Jumper wires
  • Breadboard (optional)

Wiring the Components

  1. Connect the L298N module to the Arduino:

    • IN1 to Arduino pin 9
    • IN2 to Arduino pin 8
    • IN3 to Arduino pin 7
    • IN4 to Arduino pin 6
    • ENA to Arduino pin 10 (for PWM speed control of Motor A)
    • ENB to Arduino pin 11 (for PWM speed control of Motor B)
  2. Connect the motors to the L298N module:

    • Motor A to the OUT1 and OUT2 terminals
    • Motor B to the OUT3 and OUT4 terminals
  3. Connect the power supply to the L298N module:

    • Positive to the 12V terminal
    • Negative to the GND terminal
  4. Connect the 5V and GND pins of the L298N module to the Arduino.

Arduino Code Example

The following code demonstrates how to control the speed and direction of two DC motors using the L298N motor driver and Arduino:

// Define motor control pins
const int ENA = 10;
const int IN1 = 9;
const int IN2 = 8;
const int ENB = 11;
const int IN3 = 7;
const int IN4 = 6;

void setup() {
  // Set all the motor control pins as outputs
  pinMode(ENA, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(ENB, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
}

void loop() {
  // Move Motor A forward
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  analogWrite(ENA, 255); // Full speed

  // Move Motor B backward
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  analogWrite(ENB, 255); // Full speed

  delay(2000); // Run for 2 seconds

  // Stop both motors
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);

  delay(1000); // Stop for 1 second

  // Move Motor A backward
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  analogWrite(ENA, 128); // Half speed

  // Move Motor B forward
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  analogWrite(ENB, 128); // Half speed

  delay(2000); // Run for 2 seconds

  // Stop both motors
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);

  delay(1000); // Stop for 1 second
}

This code sets up the pins for controlling two motors and uses PWM to adjust their speed. It demonstrates moving the motors in different directions and at different speeds.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.