Die H-Brücke / Motor Controller

If you want to implement projects with motors that need more power or should be variable in speed, you usually use modules with a so-called H-bridge. Such modules can be found under the heading Motor Driver or DC stepper.

In many cases, especially if you look for them in online shops, you will find them as so-called double H-bridge or dual H-bridge. But what exactly is an H-bridge and how do you use it, this basic question and all other questions about the use of these components should be clarified by this blog post.

The hardware and software for this blog

The components from Table 1 serve as reference hardware for this blog, but in principle any H-bridge or micro controller can be used.

Pos. Number Component
1 1 Nano V3.0 with Atmega328 CH340
or
Nano V3.0 with FT232RL chip and ATmega328
2 1 ESP32 Dev Kit C V4 unsoldered
or
ESP32 Dev Kit C unverlötet
3 1 MB 102 Breadboard Kit - 830 Breadboard, power supply adapter 3.3V 5V, 65pcs jumpers for Arduino
4 2 Power supply 12V
5 1 Dual H-Bridge
6 1 2 sets of DC gear motor and tire wheel for DC 3V-6V Arduino intelligent car robot projects

Table 1: Test hardware

So that you can replicate this blog, you need the following software and libraries:

The H-bridge or the four-quadrant regulator

If you start looking for the H-bridge on the Internet, you will usually find two definitions. One is the so-called bridge circuit, the other the four-quadrant controller. Both are H-bridges, but differ in a few details. Strictly speaking, a motor driver or DC stepper is not an H-bridge, but a four-quadrant controller. Why this is so should be clarified in comparison.

The H-bridge - bridge circuit

The first H-bridge on this blog will be the bridge circuit. This is also known in specialist circles under the terms H circuit or quarter, half or full bridge. It is also incorrectly referred to in some forums as a Wheatstone measuring bridge, but this is not correct. There are, too, but something else is meant by that.

The H-bridge, referred to below as the bridge circuit, got its name from the arrangement of the electrical components, mostly resistors, see illustration 1.

Illustration 1: Basic structure of the bridge circuit, from Wikipedia MovGP0

If you take the circuit shown above as a basis, a bridge circuit is nothing more than a parallel connection of two voltage dividers. However, there is a branch between the two voltage dividers, which is why the basic circuit looks like an "H" and the name H-bridge was derived from this.

This bridge can be used for both DC and AC voltage, but it depends on whether you want to measure resistors, capacitors or coils. Another property of the bridge circuit is that, depending on the setting of the resistors, you can not only vary the current and voltage, but also change the polarity! Depending on how many resistors are variable, the bridge circuit is also called quarter (one resistor), half (two resistors) or full bridge (all resistors).

The problem at this point is, it is not that easy to change the resistances for a motor that quickly.

The H-bridge - four-quadrant regulator

As for the above problem, we come straight to the next H-Bridge on our blog. This is also called the four-quadrant controller and takes on exactly the task that is needed to control a motor. This H-bridge, hereinafter referred to as the four-quadrant controller, can be found in many devices with a motor. This is used from the smallest vehicle to, for example, the ICE train of Deutsche Bahn.

If you look closely at the four-quadrant regulator, see Figure 2, the basic structure is similar to the bridge circuit.

Illustration 2: Basic structure of the four-quadrant regulator, from Wikipedia Biezl

The difference is that the resistors of the bridge circuit have been replaced by MOSFETs and blocking diodes (self-induction recuperation diode). The motor is located in the interconnection of the two parallel circuits. Similar to changing the resistances of the bridge circuit, this can change the polarity. The voltage and current must be set correctly at the connection terminals.

This already clarifies how the four-quadrant controller works roughly. Now it has to be clarified how the four-quadrant regulator got its name. Depending on how the MOSFETs (T1 to T4) are switched, one of the four states, the quadrants, can be used for the motor. Table 2 lists all four quadrants, with the MOSFETs for T1 to T4 Figure 2 are meant.

quadrant

T1

T2

T3

T4

Engine running

1

Switched

Open

Open

Switched

Accelerate forward

2

Open

Switched

Open

Switched

Braking forward

3

Open

Switched

Switched

Open

Accelerate backwards

4

Switched

Open

Switched

Open

Brake backwards

Table 2: The four quadrants

Depending on the switched MOSFETs, you can choose any direction of rotation and the type of motor acceleration. As already written above, the voltage and the current must come from the voltage source and cannot be changed in the four-quadrant controller itself.

The motor driver in practice

Now that the theory has been explained, the motor driver should now be assembled and used in practice. The components for this can be found in Table 1. In principle, all of our micro controllers with the exception of the ESP-01 are suitable for this task.

The first step is the Nano V3.0. It does not matter whether you are now using the USB cable, see the Nano Figure 3, or via an external power supply, e.g. also via the available 5V of the Motor Driver used here, see Figure 4, operate. The motors must always be powered by a battery or a rechargeable battery.



Illustration 3: Nano V3.0 with USB supply

Illustration 4: Nano with 5V supply of the H-bridge

Please note that if you use voltages above 12V on the motor driver, the 5V jumper on the motor driver must be removed and a separate voltage source for the micro controller is required.

For a simple demonstration of how to drive a small motor, you can use Code 1.

//-----------------------------------------------------
// Controlling a DC motor with Nano V3.0
// Author: Joern Weise
// License: GNU GPl 3.0
// Created: Feb 23, 2021
// Update: Feb 23, 2021
//-----------------------------------------------------

// PWM and motor configuration
// Motor A
const int motor1Pin1 = 9;
const int motor1Pin2 = 8;
const int enable1Pin = 10;

void setup () {
Serial.begin (115200);
while (! Serial) {
; // Wait until serial is available
  }
Serial.println ("Motor control with NANO V3.0");
Serial.println ("(c) Joern way for AZ-Delivery");
Serial.println ("--------------------");
Serial.println ("Set outputs");

// Set pins as outputs
pinMode (motor1Pin1, OUTPUT);
pinMode (motor1Pin2, OUTPUT);
pinMode (enable1Pin, OUTPUT);

Serial.println ("Setup finished");
}

void loop () {
Serial.println ("----- Motor clockwise speedup -----");
digitalWrite (motor1Pin1, HIGH);
// A = HIGH and B = LOW means the motor will turn right
digitalWrite (motor1Pin2, LOW);
for (int i = 0; i <256; i + = 5)
  {
Serial.println ("Speed:" + String (i));
analogWrite (enable1Pin, i);
delay (100);
  }

Serial.println ("----- Motor clockwise break -----");
for (int i = 255; i> 0; i- = 5)
  {
Serial.println ("Speed:" + String (i));
analogWrite (enable1Pin, i);
delay (100);
  }

Serial.println ("----- Motor counterclockwise speedup -----");
digitalWrite (motor1Pin1, LOW);
// A = HIGH and B = LOW means the motor will turn right
digitalWrite (motor1Pin2, HIGH);
for (int i = 0; i <256; i + = 5)
  {
Serial.println ("Speed:" + String (i));
analogWrite (enable1Pin, i);
delay (100);
  }
Serial.println ("----- Motor counterclockwise break -----");
for (int i = 255; i> 0; i- = 5)
  {
Serial.println ("Speed:" + String (i));
analogWrite (enable1Pin, i);
delay (100);
  }
}

Code 1: Demo code for Nano V3.0

In principle, the code doesn't do much that is spectacular. It starts the motor once in a clockwise direction and accelerates it up to maximum speed. Immediately afterwards, the motor is braked again to a standstill. To show that this does not only work in one direction, the same control is carried out again counterclockwise.

Once the loop function has been run through, the process starts all over again. So that you can quickly understand what is happening where, plenty of comments have been added to the source code.

That brings us to the ESP family at this point. Regardless of whether a controller from the ESP32 or ESP8266 family, the connection is almost identical here too. The cabling for the power supply with USB cable, see Figure 5, or with the 5V connection of the Motor Driver, see Figure 6, is also done quickly.

One more word about the L298N Motor Driver Board used here. Compared to the L293D boards, which are also suitable, a heat sink for higher currents and thus larger motors is already installed here. After we initially used a logic level converter for the Espressif micro controllers to adjust the output voltage at the 3.3V GPIOs of the ESPs to 5V, we found out in further experiments that the voltage at the enable pin of the Motor Driver is a kind of reference voltage for the PWM signals is; So a duty cycle of 100% delivers the full output voltage for the motors even at 3.3V. The LLC can therefore be dispensed with.

Illustration 5: Micro Controller of the ESP32 family with USB supply

 

Illustration 6: Micro Controller of the ESP32 family with 5V supply

At this point, too, the 5V jumper must be removed for voltages above 12V on the motor driver and a separate voltage source for the micro controller is required.

Unfortunately, we cannot completely take over the example from Nano V3.0, see Code 2, because the PWM signal has to be programmed differently and we also use different pins.

//-----------------------------------------------------
// Controlling a DC motor with ESP
// Author: Joern Weise
// License: GNU GPl 3.0
// Created: Feb 23, 2021
// Update: Feb 23, 2021
//-----------------------------------------------------

// PWM and motor configuration
// Motor A
const int motor1Pin1 = 27;
const int motor1Pin2 = 26;
const int enable1Pin = 14;
const int motor1channel = 0;

// Setting PWM properties
const int freq = 30000;
const int resolution = 8;

void setup () {
Serial.begin (115200);
while (! Serial) {
; // Wait until serial is available
  }
Serial.println ("Motor control with ESP");
Serial.println ("(c) Joern way for AZ-Delivery");
Serial.println ("--------------------");
Serial.println ("Set outputs");

// Set pins as outputs
pinMode (motor1Pin1, OUTPUT);
pinMode (motor1Pin2, OUTPUT);
pinMode (enable1Pin, OUTPUT);

Serial.println ("Configurate motorchannel");
ledcSetup (motor1channel, freq, resolution);
// Configure PWM for motor 1
ledcAttachPin (enable1Pin, motor1channel);
// Attach channel 1 to motor 1
Serial.println ("Setup finished");
}

void loop () {
Serial.println ("----- Motor clockwise speedup -----");
digitalWrite (motor1Pin1, HIGH);
// A = HIGH and B = LOW means the motor will turn right
digitalWrite (motor1Pin2, LOW);
for (int i=0; i<256; i+=5)
  {
Serial.println("Speed:" + String(i));
ledcWrite(motor1channel, i);
delay(100);
  }
Serial.println("----- Motor clockwise break-----");
for (int i=255; i>0; i-=5)
  {
Serial.println("Speed:" + String(i));
ledcWrite(motor1channel, i);
delay(100);
  }

Serial.println("----- Motor counterclockwise speedup-----");
digitalWrite(motor1Pin1,LOW);
// A = HIGH and B = LOW means the motor will turn right
digitalWrite(motor1Pin2,HIGH);
for (int i=0; i<256; i+=5)
  {
Serial.println("Speed:" + String(i));
ledcWrite(motor1channel, i);
delay(100);
  }
Serial.println("----- Motor counterclockwise break-----");
for (int i=255; i>0; i-=5)
  {
Serial.println("Speed:" + String(i));
ledcWrite(motor1channel, i);
delay(100);
  }
}


Code 2: Demo code for Micro Controller of the ESP32-family

As with the Nano V3.0, many comments have been added to this code so that you can quickly see what is happening where. Pay particular attention to the setup function, in which the PWM is declared. Through "ledcSetup (motor1channel, freq, resolution);" you declare a PWM channel with an 8-bit resolution and a frequency of 30 kHz. Immediately afterwards, "ledcAttachPin (iPWMPin, motor1channel);" The PWM channel is linked to the output pin.

As you can see, controlling a motor using an H-bridge or, better still, a motor driver is not much rocket science. The principle behind the control is simple, purely the logic of when the motor should do what must be properly processed in your code. A simple example of this can be found in the blog post Controlling a Robot with MQTT - [Part 3]. Such an H-bridge is necessary to let motors turn in any speed and direction.

You can already look forward to the other articles on the main topic of robot cars, where you will most likely find an H-bridge or motor driver again

This and other projects can be found on GitHub at

https://github.com/M3taKn1ght/Blog-Repo.

 

 

Esp-32Für arduinoGrundlagen elektronikStromversorgung

4 comments

Kistner, Gert

Kistner, Gert

Ich hoffe, dass das “Motor break” nicht wörtlich gemeint ist (brechen, zerstören), sondern brake (bremsen).

Jacky

Jacky

@Christian Paulsen:
Theoretisch lässt sich natürlich auch eine H-Brücke als reiner Ein-/Ausschalter für einen Laser verwenden. Nur wäre das ein ziemliche Resourcenverschwendung: vier Schalter anstatt Einer!
Der Hauptaspekt bei der H-Brücke ist, dass ich die Stromflussrichtung über die vier Schalter umkehren/umpolen kann, ohne die Last abklemmen und wieder anschließen zu müssen (also im laufenden Betrieb). Früher hat man hierzu mechanische “Polwendeschalter” benötigt. D.h. das Ganze macht eigentlich nur für Motoren (oder z.B. Peltier-Elemente) Sinn, sprich bei Verbrauchern, bei denen die Stromrichtung eine funktionale Eigenschaft bestimmt (wie Rechtslauf/Linkslauf oder Heizen/Kühlen).
Da der Laser nur einen Stromfluss in eine Richtung erfordert, reicht hier, wie von Jörn erwähnt, ein einfacher (MOSFET-/Bipolarer) Transistor als Schalter um die Zustände “Laser Ein/Aus” abzubilden.

Jörn Weise

Jörn Weise

Hallo Benno,
Hallo Herr Planert,

ja da hat sich bei mir ein kleiner Fehler eingeschlichen, ich bitte das zu entschuldigen. Normalerweise prüfe ich das noch einmal ganz genau, anscheinend habe ich an dieser Stelle nicht genau aufgepasst. Ich werde in Zukunft versuchen die Steckpläne noch genauer zu kontrollieren, damit ihrer Hardware keinen Schaden passiert.

Hallo Herr Paulsen,
leider verstehe ich die Frage nicht. Einen Laser können Sie im regelfall nur ein oder ausschalten. Dazu wird entsprechend Spannung angelegt, eine H-Brücke eignet sich da weniger. Um zu verhindern, dass der Laser Schaden versursacht, sollte (aus meiner Sicht) immer ein MOSFET oder Transistor verbaut werden. Damit zerstören Sie nur einen kleinen Teil ihrer Schaltung und nicht gleich den ganzen Controller.
Das selbe wird auch bei 3D-Druckern gemacht. beim Heatbed und Hotend werden MOSFET’s eingesetzt. Das verhindert, dass die Platine einen Defekt erleidet, wenn z.B. das Heatbed einen Schaden hat. Sie müssen dann “nur” das Heatbed und MOSFET austauschen (wobei dieses meist auf der Platine verlötet ist). Kleiner Spoiler am Rande, aktuell arbeite ich an einer kleinen Serie, die einigen Makern hier gefallen wird, ich sage nur 3D-Drucker ^^

Gruß und schönes Wochenende
Jörn Weise

Christian Paulsen

Christian Paulsen

Interessant!Kann man eine H-Bridge eigentlich auch für die Anbindung eines Laser-mouls an ein Arduino-CNC-shield benutzen? Oder ist da ein Mosfet besser geeignet?

Leave a comment

All comments are moderated before being published