Op werkdagen vóór 14:00 uur besteld = dezelfde dag verzonden en in 98% van de gevallen de volgende dag in huis. Gratis retourneren 30 dagen bedenktijd 1 jaar garantie
← Tillbaka till översikten

Kopplingsschema och kodexempel

Så här kopplar du L298N Motor Driver Board röd till en Arduino UNO eller ESP32, med ett exempel­sketch som du kan ladda upp direkt.

L298N Motor Driver Control with Arduino Uno

This example demonstrates how to control DC motors using an L298N motor driver board. The Arduino sets the motor rotation direction via digital logic pins and adjusts motor speed using PWM on the enable pin. The program cycles the motors forward, stops them, and then drives them in reverse.

Kopplingsschema: L298N Motor Driver Board röd på en Arduino UNO
Kopplingsschema · Klicka för att förstora
Kopplingstabell: L298N Motor Driver Board röd på en Arduino UNO
Kopplingstabell · Klicka för att förstora
L298N_Motor_Driver_Control_with_Arduino_.ino · kompilerad och testad
/*
 * ==================================================================
 *  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

ESP32 Dual DC Motor Control with L298N

This project controls two DC motors using an ESP32 and an L298N H-bridge driver. The sketch rotates both motors forward, stops them, drives them in reverse, and demonstrates PWM speed control. Connect external power to the L298N 12V terminal to provide enough current for larger motors.

Kopplingsschema: L298N Motor Driver Board röd på en ESP32 DevKit V1
Kopplingsschema · Klicka för att förstora
Kopplingstabell: L298N Motor Driver Board röd på en ESP32 DevKit V1
Kopplingstabell · Klicka för att förstora
ESP32_Dual_DC_Motor_Control_with_L298N.ino · kompilerad och testad
/*
 * ==================================================================
 *  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

Tidigare visade

Lindtråd Koppar Ø0.2mm - 178m - 97 ohm

Lindtråd Koppar Ø0.2mm - 178m - 97 ohm

€ 9,70
Visa
Tryckknapp | moment - puls | 12x12x7 | Gul

Tryckknapp | moment - puls | 12x12x7 | Gul

€ 0,75
Hölje för 0.96 inch OLED-display

Hölje för 0.96 inch OLED-display

€ 5,90
Visa
A7670 SIM 4G LTE Cat 1 och GPS-modul

A7670 SIM 4G LTE Cat 1 och GPS-modul

€ 28,99
3x4 matris-knappsats svart

3x4 matris-knappsats svart

€ 3,40