Tijekom pandemije COVID-19, e-radionica.com nastavlja s normalnim radom. Sve mjere opreza se redovno poduzimaju te su naručeni paketi sigurni. Opcija dostave uslugama Hrvatske Pošte i osobno preuzimanje privremeno su onemogućeni iz sigurnosnih razloga.

• Worldwide Shipping

• FAQ
• Worldwide Shipping

• FAQ

# HUM: The simple H-Bridge

You are a beginner with Croduino. Or electronics? A specific module caught your eye, but you do not know how to use it? Do not worry, HUM is here for you! How to Use Module (HUM) is a blog tutorials series by e-radionica where you will find all you need in order to begin working with your favorite module. Tutorials include: technical characteristics, work principle, instructions on how to connect module with Croduino and the basic code. Everything else is left to your imagination.

## Introduction

You have certainly already encountered some type of a DC motor. You know that connecting the DC motor's pins to a source of certain voltage results in rotating the motor to one side. Also, if we switch pins on the motor and connect the power source the opposite way, we will see that the motor rotates to the opposite side. This feature of the motor is very useful, but it is complicated to constantly switch pins of the motor manually. There must be an electronic circuit which can do it for us? Of course there is. The answer is in the H-bridge circuit. Using it, we can easily and safely change the motor rotation direction. In continuation, we will explain the H-bridge work principle in more detail.

Characteristics:
• Maximum current: -
• Maximum voltage: -
• Control via and control pins

## How does it work?

H-bridge is a circuit that, in theory, consists of four switches connected to some load (e.g. DC motor). Various configurations of these switches can allow us to control current flow in the circuit. Using the H-bridge, it is very simple to switch the polarity on the load. Even though the load can theoretically be whatever you want, by far, the most widely used H-bridge application is for DC and stepper motors. It is most commonly used to control and replace the DC motor rotation direction. H-bridge is also used in many other applications such as DC/AC, AC/AC or DC/DC converters. The H-bridge circuit reminds of the letter H, and that is how it has gotten its name.
The image above represents a scheme display of the H-bridge. We can see that the H-bridge consists of four MOSFETs connected in a certain way. Because of the inability to simultaneously switch between the two possible states of the bridge, diodes were added to the circuit (mostly Schottky's diodes) in order for it to ensure simultaneous switch of states and limit the current's flow power during these short periods of imbedding, without the load voltage being too high. The upper end of the bridge is connected to a power source (e.g. battery), while the bottom end is grounded. The basic work principle of the H-bridge is very simple: if Q1 and Q4 are turned on, the left cable of the motor will be connected to the power supply, and the right to the ground. The current flows through the motor (so to say) in the forward direction, and the engine shaft starts rotating.

If Q2 and Q3 are on, what will happen is quite the opposite, the motor is powered in the reversed direction, and the shaft will rotate in the opposite direction.

You must never close Q1 and Q2 (or Q3 and Q4) at the same time! If that happens, a direct path between the power supply voltage and ground which is of very low resistance is created, which ultimately results in a short circuit. A short circuit can lead to destruction of the H-bridge, or some other component in the circuit.

The table below shows every possible combination of switch states and their outcome.

It is necessary to note that this module has only two control pins (A and B) which are used to control the H-bridge operation. A pin is connected to 2 MOSFETs (Q1 and Q2) which are actually one N-channel and one P-channel MOSFET. The reason for it is the elimination of states in which the Q1 and Q2 are simultaneously "turned on". When the N-channel MOSFET conducts current, P-channel MOSFET does not, and it ensures that, at any given time, Q1 and Q2 are not in the same state (more about MOSFETs here). B pin is connected to the other 2 MOSFETs (Q3 and Q4) in the same way. This makes it easier to work with the module where we do not have to look out for accidentaly making the short circuit or destroying the module.

## How to connect it?

Below, we will show the connection of the H-bridge module with the DC motor whom we will control using Croduino. H-bridge contains several pins:

VCC - Input for the external power supply.
GND - Serves for connecting minuses of the power supply.
A - Control pin for controlling the H-bridge operation.
B - Another control pin for controlling the H-bridge operation.
OUTPUT x2 - Two pins to which we connect the load (e.g. DC motor).

The image below shows an example of the correct connection of the H-bridge with Croduino. It is necessary to note that the A and B module pins are connected to Croduino, while the DC motor is connected to the OUTPUT pins. It is not important which OUTPUT pin is connected to which DC motor connector, as the rotation direction will be determined in the program.

## Arduino code

There is no need to use any library for motor control using the H-bridge module. By simply switching the A and B connectors on/off using digitalWrite() function, we start the motor. The motor will rotate only when the A pin's state is different from B pin's state. When their states are the same, A=B, it means we have brought the same voltage to both motor connectors (e.g. +5V or -5V). Therefore, when the A pin is in HIGH state, and B in LOW, the motor will rotate in one direction. When the A pin is in LOW state, and B pin in HIGH, the motor will rotate to the opposite direction.

```//This simple program runs the motor for 2 sec in one direction, after which the motor stops for 2 sec
//After that, the direction changes and it rotates to the opposite direction for 2 sec and then stops for 2 sec
int pinA = 4; // defining pins for motor rotation control
int pinB = 3;
void setup() {
pinMode(pinA, OUTPUT); // Setting pins as outputs
pinMode(pinB, OUTPUT);
}
void loop() {
pokreniMotor (1); //the motor rotates in one direction
delay(2000); //wait 2 sec
pokreniMotor (0); //the motor stops
delay(2000); //wait 2 sec
pokreniMotor (2); //the motor rotates in the opposite direction
delay(2000); //wait 2 sec
pokreniMotor (0); //the motor stops
delay(2000); //wait 2 sec
}
void pokreniMotor (int stanje) { //we use this function to start the motor
switch (stanje) {
case 0: //if the state=0, the motor will not start, breaking mode
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
break;
case 1: //if the state=1, the motor will rotate in one direction
digitalWrite(pinA, HIGH);
digitalWrite(pinB, LOW);
break;
case 2: //if the state=2, the motor will rotate in the opposite direction
digitalWrite(pinA, LOW);
digitalWrite(pinB, HIGH);
break;
default: //not necessary, if the state is different than the above mentioned values, turn the engine off, for safety.
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
break;
}
}
```