Werktags vor 14:00 Uhr bestellt = am selben Tag versendet 30 Tage Widerrufsrecht 1 Jahr Garantie
← Zurück zur Übersicht

Schaltplan und Codebeispiel

So schließt du den L298N Motor Driver Board rot an einen Arduino UNO oder ESP32 an, mit einem Beispiel-Sketch, den du direkt hochladen kannst.

L298N-Motortreiber-Steuerung mit Arduino Uno

Dieses Beispiel zeigt, wie man DC-Motoren mit einer L298N-Motortreiberplatine steuert. Der Arduino legt die Drehrichtung der Motoren über digitale Logik-Pins fest und passt die Motordrehzahl mit PWM am Enable-Pin an. Das Programm lässt die Motoren zuerst vorwärts laufen, stoppt sie und fährt sie dann rückwärts.

Anschlussplan: L298N Motor Driver Board rot an einem Arduino UNO
Anschlussplan · Zum Vergrößern klicken
Anschlusstabelle: L298N Motor Driver Board rot an einem Arduino UNO
Anschlusstabelle · Zum Vergrößern klicken
L298N_Motor_Driver_Control_with_Arduino_.ino · kompiliert und getestet
/*
 * ==================================================================
 *  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-Motorsteuerung mit L298N

Dieses Projekt steuert zwei DC-Motoren mit einem ESP32 und einem L298N H-Brücken-Treiber. Der Sketch lässt beide Motoren vorwärts laufen, stoppt sie, bewegt sie rückwärts und demonstriert die PWM-Drehzahlregelung. Schließe eine externe Stromversorgung an den 12V-Anschluss des L298N an, um ausreichend Strom für größere Motoren bereitzustellen.

Anschlussplan: L298N Motor Driver Board rot an einem ESP32 DevKit V1
Anschlussplan · Zum Vergrößern klicken
Anschlusstabelle: L298N Motor Driver Board rot an einem ESP32 DevKit V1
Anschlusstabelle · Zum Vergrößern klicken
ESP32_Dual_DC_Motor_Control_with_L298N.ino · kompiliert und getestet
/*
 * ==================================================================
 *  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

Zuletzt angesehen

Arduino Starterkit UNO R3 Niederländisch inkl. Codey Ai Vibe Coding

Arduino Starterkit UNO R3 Niederländisch inkl. Codey Ai Vibe Coding

€ 39,95
Raspberry Pi Pico 2WH mit WiFi und vorverlöteten Headers

Raspberry Pi Pico 2WH mit WiFi und vorverlöteten Headers

€ 11,95
Löt-Reinigungsset 6-teilig

Löt-Reinigungsset 6-teilig

€ 8,95
Ansehen
MAX7219CNG LED-Anzeigetreiber DIP24 IC

MAX7219CNG LED-Anzeigetreiber DIP24 IC

€ 2,10
Mini OLED-Display weiß 0.91 inch 128x32 I2C

Mini OLED-Display weiß 0.91 inch 128x32 I2C

€ 2,99
Mini OLED-Display gelb/blau 0.96 inch 128x64 I2C

Mini OLED-Display gelb/blau 0.96 inch 128x64 I2C

€ 5,99
Flexible Silikon-Lötmatte 30x20cm blau

Flexible Silikon-Lötmatte 30x20cm blau

€ 4,70
Ansehen
CAN-Bus-Modul MCP2515 SPI 5V

CAN-Bus-Modul MCP2515 SPI 5V

€ 2,25
Ansehen
5V 3A Netzteiladapter mit USB-C-Anschluss 15W

5V 3A Netzteiladapter mit USB-C-Anschluss 15W

€ 7,49
5V 4-Phasen-Schrittmotor 28BYJ-48

5V 4-Phasen-Schrittmotor 28BYJ-48

€ 2,95