Pedido antes de las 14:00 en día laborable = enviado el mismo día 30 días de desistimiento 1 año de garantía
← Volver al resumen

Esquema de conexión y ejemplo de código

Así conectas el Placa controladora de motor L298N roja a un Arduino UNO o ESP32, con un sketch de ejemplo que puedes subir directamente.

Control del driver de motor L298N con Arduino Uno

Este ejemplo muestra cómo controlar motores DC usando una placa driver de motor L298N. El Arduino establece la dirección de giro del motor mediante pines lógicos digitales y ajusta la velocidad del motor usando PWM en el pin enable. El programa hace que los motores avancen, se detengan y luego giren en sentido inverso.

Esquema de conexión: Placa controladora de motor L298N roja en un Arduino UNO
Esquema de conexión · Haz clic para ampliar
Tabla de conexión: Placa controladora de motor L298N roja en un Arduino UNO
Tabla de conexión · Haz clic para ampliar
L298N_Motor_Driver_Control_with_Arduino_.ino · compilado y probado
/*
 * ==================================================================
 *  Generated by Codey.online  —  https://www.codey.online
 * ==================================================================
 *  Project   : L298N Motor Driver Control with Arduino Uno
 *  Board     : Arduino UNO (arduino:avr:uno)
 *  Parts     : L298N Motor Driver Board
 *  Libraries : none (built-in)
 *
 *  Codey Online is an AI-powered browser IDE for Arduino and ESP32.
 *  Describe your project and Codey writes the code, draws the wiring
 *  diagram and uploads it to your board, straight from the browser.
 *  This code is free to use, modify and share, without warranty.
 * ==================================================================
 */

// L298N Motor Driver Control Demo

const int ENA_PIN = 5;   // PWM speed control
const int IN1_PIN = 8;   // Motor A direction 1
const int IN2_PIN = 9;   // Motor A direction 2
const int IN3_PIN = 10;  // Motor B direction 1
const int IN4_PIN = 11;  // Motor B direction 2

void stopMotors() {
  digitalWrite(IN1_PIN, LOW);
  digitalWrite(IN2_PIN, LOW);
  digitalWrite(IN3_PIN, LOW);
  digitalWrite(IN4_PIN, LOW);
  analogWrite(ENA_PIN, 0);
}

void driveForward(int speedVal) {
  digitalWrite(IN1_PIN, HIGH);
  digitalWrite(IN2_PIN, LOW);
  digitalWrite(IN3_PIN, HIGH);
  digitalWrite(IN4_PIN, LOW);
  analogWrite(ENA_PIN, speedVal);
}

void driveReverse(int speedVal) {
  digitalWrite(IN1_PIN, LOW);
  digitalWrite(IN2_PIN, HIGH);
  digitalWrite(IN3_PIN, LOW);
  digitalWrite(IN4_PIN, HIGH);
  analogWrite(ENA_PIN, speedVal);
}

void setup() {
  Serial.begin(115200);
  pinMode(ENA_PIN, OUTPUT);
  pinMode(IN1_PIN, OUTPUT);
  pinMode(IN2_PIN, OUTPUT);
  pinMode(IN3_PIN, OUTPUT);
  pinMode(IN4_PIN, OUTPUT);

  stopMotors();
  Serial.println(F("L298N Motor Driver initialized."));
}

void loop() {
  Serial.println(F("Driving forward"));
  driveForward(200);
  delay(2000);

  Serial.println(F("Stopping"));
  stopMotors();
  delay(1000);

  Serial.println(F("Driving reverse"));
  driveReverse(200);
  delay(2000);

  Serial.println(F("Stopping"));
  stopMotors();
  delay(1000);
}

Wiring diagram and example code generated by Codey.online — https://www.codey.online

Control dual de motores DC con ESP32 y L298N

Este proyecto controla dos motores DC usando un ESP32 y un driver de puente H L298N. El sketch hace girar ambos motores hacia adelante, los detiene, los hace girar en reverse y demuestra el control de velocidad por PWM. Conecta alimentación externa al terminal 12V del L298N para proporcionar suficiente corriente a motores más grandes.

Esquema de conexión: Placa controladora de motor L298N roja en un ESP32 DevKit V1
Esquema de conexión · Haz clic para ampliar
Tabla de conexión: Placa controladora de motor L298N roja en un ESP32 DevKit V1
Tabla de conexión · Haz clic para ampliar
ESP32_Dual_DC_Motor_Control_with_L298N.ino · compilado y probado
/*
 * ==================================================================
 *  Generated by Codey.online  —  https://www.codey.online
 * ==================================================================
 *  Project   : ESP32 Dual DC Motor Control with L298N
 *  Board     : ESP32 DEVKIT V1 (esp32:esp32:esp32doit-devkit-v1)
 *  Parts     : L298N Motor Driver Board
 *  Libraries : none (built-in)
 *
 *  Codey Online is an AI-powered browser IDE for Arduino and ESP32.
 *  Describe your project and Codey writes the code, draws the wiring
 *  diagram and uploads it to your board, straight from the browser.
 *  This code is free to use, modify and share, without warranty.
 * ==================================================================
 */

const int ENA_PIN = 14;
const int IN1_PIN = 27;
const int IN2_PIN = 26;
const int IN3_PIN = 25;
const int IN4_PIN = 33;
const int ENB_PIN = 32;

void setMotors(int speedA, int dirA, int speedB, int dirB) {
  // Motor A direction
  if (dirA > 0) {
    digitalWrite(IN1_PIN, HIGH);
    digitalWrite(IN2_PIN, LOW);
  } else if (dirA < 0) {
    digitalWrite(IN1_PIN, LOW);
    digitalWrite(IN2_PIN, HIGH);
  } else {
    digitalWrite(IN1_PIN, LOW);
    digitalWrite(IN2_PIN, LOW);
  }
  analogWrite(ENA_PIN, speedA);

  // Motor B direction
  if (dirB > 0) {
    digitalWrite(IN3_PIN, HIGH);
    digitalWrite(IN4_PIN, LOW);
  } else if (dirB < 0) {
    digitalWrite(IN3_PIN, LOW);
    digitalWrite(IN4_PIN, HIGH);
  } else {
    digitalWrite(IN3_PIN, LOW);
    digitalWrite(IN4_PIN, LOW);
  }
  analogWrite(ENB_PIN, speedB);
}

void setup() {
  Serial.begin(115200);
  pinMode(ENA_PIN, OUTPUT);
  pinMode(IN1_PIN, OUTPUT);
  pinMode(IN2_PIN, OUTPUT);
  pinMode(IN3_PIN, OUTPUT);
  pinMode(IN4_PIN, OUTPUT);
  pinMode(ENB_PIN, OUTPUT);
  Serial.println("L298N Motor Driver Initialized");
}

void loop() {
  // Drive forward
  Serial.println("Moving Forward");
  setMotors(200, 1, 200, 1);
  delay(2000);

  // Stop
  Serial.println("Stopping");
  setMotors(0, 0, 0, 0);
  delay(1000);

  // Drive backward
  Serial.println("Moving Backward");
  setMotors(200, -1, 200, -1);
  delay(2000);

  // Stop
  Serial.println("Stopping");
  setMotors(0, 0, 0, 0);
  delay(1000);

  // Speed ramp up
  Serial.println("Ramping Speed Forward");
  for (int spd = 0; spd <= 255; spd += 15) {
    setMotors(spd, 1, spd, 1);
    delay(100);
  }
  delay(500);
  setMotors(0, 0, 0, 0);
  delay(1500);
}

Wiring diagram and example code generated by Codey.online — https://www.codey.online