Control motors with the L298N H-bridge

FunduinoPublished on

Zwei Gleichstrommotoren in beide Richtungen drehen lassen

Übersicht

In dieser Anleitung steuerst du zwei Gleichstrommotoren über die L298N H-Brücke an. Eine H-Brücke besteht aus vier Schaltern (oft Transistoren), die in einer H-förmigen Anordnung verbunden sind. Diese vier Schalter lassen sich so stellen, dass die sogenannte Last (zum Beispiel ein Motor) in beiden möglichen Polaritäten betrieben werden kann. Zudem lässt sich die Stromversorgung ganz trennen. Du kannst einen Gleichstrommotor also in beide Richtungen rotieren lassen.

You need this

L298N motor driver - H-bridge for 2 DC motors, 5-35 V L298N motor driver - H-bridge for 2 DC motors, 5-35 V 3,22 € *Item no. F23107161About the product — L298N motor driver - H-bridge for 2 DC motors, 5-35 V
TT gear motor - 3-6 V, 1:90, 200 rpm, all-metal gearbox, single shaft TT gear motor - 3-6 V, 1:90, 200 rpm, all-metal gearbox, single shaft 3,80 € *Item no. F23109195About the product — TT gear motor - 3-6 V, 1:90, 200 rpm, all-metal gearbox, single shaft
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
830-point Breadboard - solderless breadboard for Arduino 830-point Breadboard - solderless breadboard for Arduino 2,90 € *Item no. FUN-1010553About the product — 830-point Breadboard - solderless breadboard for Arduino
Breadboard cables male-female - 40 pieces, 20 cm Breadboard cables male-female - 40 pieces, 20 cm 1,74 € *Item no. FUN-1010554About the product — Breadboard cables male-female - 40 pieces, 20 cm

All 5 parts together 22,78 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

Pinout

Since the L298N H-bridge has a relatively large number of inputs and outputs, we have numbered them.

  • 1 = Connection for DC motor 1 – “+”
  • 2 = Connection for DC motor 1 – “–”
  • 3 = 12V jumper. This can be removed if you are using a supply voltage greater than 12V DC. Removing the jumper activates the H-bridge’s 5V regulator.
  • 4 = Power supply input for the H-bridge (max. 35V DC)
  • 5 = GND input
  • 6 = 5V output, e.g. for powering the microcontroller
  • 7 = Pin used to control DC motor 1. If this pin is connected to a PWM pin on the microcontroller, the motor speed can also be controlled precisely.
  • 8 = Pin IN1 – Controls DC motor 1
  • 9 = Pin IN2 – Controls DC motor 1
  • 10 = Pin IN3 – Controls DC motor 2
  • 11 = Pin IN4 – Controls DC motor 2
  • 12 = Pin used to control DC motor 2. If this pin is connected to a PWM pin on the microcontroller, the motor speed can also be controlled precisely.
  • 13 = Connection for DC motor 2 – “+”
  • 14 = Connection for DC motor 2 – “–”
L298N H-bridge from above with numbered terminals
The L298N H-bridge from above, with numbered connections

Circuit diagram

Wiring diagram with Arduino, L298N, and two DC motors
Wiring diagram: two DC motors connected to the L298N

Example sketch

Example sketch
// Gleichstrommotor 1
int GSM1 = 10;
int in1 = 9;
int in2 = 8;
// Gleichstrommotor 2
int GSM2 = 5;
int in3 = 7;
int in4 = 6;
void setup()
{
  pinMode(GSM1, OUTPUT);
  pinMode(GSM2, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
}
void loop()
{
  digitalWrite(in1, HIGH);  // Motor 1 beginnt zu rotieren
  digitalWrite(in2, LOW);
  analogWrite(GSM1, 200);   // Motor 1 soll mit der Geschwindigkeit "200" (max. 255) rotieren
  digitalWrite(in3, HIGH);  // Motor 2 beginnt zu rotieren
  digitalWrite(in4, LOW);
  analogWrite(GSM2, 200);   // Motor 2 soll ebenfalls mit der Geschwindigkeit "200" (max. 255) rotieren
  delay(2000);
  digitalWrite(in1, LOW);   // Durch die Veränderung von HIGH auf LOW (bzw. LOW auf HIGH) wird die Richtung der Rotation verändert.
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  delay(2000);
  digitalWrite(in1, LOW);   // Anschließend sollen die Motoren 2 Sekunden ruhen.
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  delay(2000);
}
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.