## Monday, February 20, 2023

Single Forward and Reverse Mosfet Bi-Directional DC Motor Driver

We all know how to use a DC motor; it suffices to connect its two poles to a direct current power supply (an electric battery for example) to make it turn. If the polarity of the power supply is reversed, the motor turns in the other direction. The higher the voltage of the power supply (without exceeding what the motor can admit), the higher the speed of rotation, and vice versa. It is on this principle that our trains are controlled in analog technology, the supply transformer delivering an adjustable and reversible voltage on the two rails.

Controlling a motor therefore means being able to adjust its speed and direction of rotation. Sometimes both are not necessary: the wheel of a watermill always turns in the same direction, the gondola cable always turns at the same speed, but a blow in one direction (up from the cabin) and a blow in the opposite direction (down from the cabin). Arduino can perfectly control a DC motor and thus create animations on the network. However, the current it can deliver (maximum 20 mA on one output) is insufficient and it is therefore necessary to add some electronics. Several solutions exist and are extremely simple to implement.

Motor speed control

To vary the speed of a DC motor, the supply voltage at the motor terminals must be varied. But Arduino only knows how to deliver voltages of 5 V on its pins with a current that is much too low to power a motor. The solution is to use PWM and amplify the signal with a transistor (see the article PWM: What is it? (2)) or an equivalent integrated circuit such as the ULN2803 circuit ( see the article Amplify the output signal of an ARDUINO with an ULN 2803).

Arduino, thanks to its analogWrite function, creates a square wave signal switching between low level (LOW = 0 V) and high level (HIGH = + 5 V) of fixed frequency (on a Uno, 490 or 980 Hz approximately depending on the pins that deliver the signal), the duty cycle of which can be varied, that is to say the time when the signal is at the high level over the time of the period of the signal. A duty cycle of 50% means the signal is high 50% of the time. This is summarized in Figure 1.

This signal is generated on the Arduino pins mentioned PWM (marked with a tilde), thanks to the analogWrite function which specifies the duty cycle to adopt. The value is between 0 (for a duty cycle equal to 0%) and 255 (for a duty cycle equal to 100%), any intermediate value that can be adopted (example, 127 for a duty cycle of 50%). The signal is amplified by a transistor and as it works in switching, its power dissipation is almost zero. The motor which receives such a signal is always supplied with its nominal supply voltage, which means that its torque is maximum. On the other hand, the motor perceives this signal as a current of average voltage equal to the supply voltage multiplied by the duty cycle; it therefore rotates more slowly. To vary the speed of the motor, simply vary the duty cycle of the PWM signal delivered by Arduino.

Control of the direction of rotation with an H-bridge

The H-bridge will allow us to control the direction of rotation of the motor. Figure 2 explains the principle thanks to an assembly made up of four switches and the motor to be controlled.

All switches are open so current from the supply cannot reach the motor; this one is stopped. Let’s number our switches from A to D (clockwise) (see Figure 2). Above all, do not close switches A and D, or B and C, at the same time, as this would create a short circuit with the power supply. Let's start by closing switches A and C, B and D remaining open. The electric current reaches the motor following the path shown in yellow: the motor rotates in one direction. Open A and C: the motor stops (starting case). Let's now close switches B and D, A and C remaining open. The electric current reaches the motor following the path shown in purple: the motor turns in the other direction.

This may seem complicated to you, whereas an inverter assembly would have produced the same result, but the H bridge becomes interesting if the switches are replaced by electronic switches controlled by currents or voltages, such as transistors for example. To better understand how a transistor can become an electronic switch, refer to chapter 5 of the electronics course of the article Getting started in electronics.

H bridge with transistors

Figure 3 shows a classic assembly in electronics, called H bridge (H bridge) or double bridge (dual bridge). It consists of 4 NPN transistors called

H bridge with transistors

Figure 3 shows a classic assembly in electronics, called H bridge (H bridge) or double bridge (dual bridge). It consists of 4 NPN transistors called A, B, C and D (like our switches in the previous paragraph). To make the motor turn in direction 1, the current must follow the path shown in yellow. Transistors A and C must be on while transistors B and D must be off. It is the opposite to make the motor turn in direction 2, the current must follow the path shown in purple; transistors B and D must be on and transistors A and C must be off.

In the electronics course mentioned above, we learned how to use switching transistors by applying a signal to its base. If we connect the bases of the transistors to the digital outputs 7, 8, 9 and 10 of Arduino (for example), it is then sufficient to apply HIGH or LOW signals to these outputs so that our transistors are on or off. The following table summarizes the situation.

Arduino outputs 9 and 10 can provide a PWM type signal; the assembly is then used to reverse the direction of rotation of a motor while controlling its rotation speed.

Of course, each time a load made up of windings is used (in the case of a motor), it is necessary to protect the control electronics (transistors or Arduino) with freewheel diodes. These have not been shown in Figure 3 (so as not to overload the figure) but should be mounted as shown in the following paragraphs, especially in Figures 5 and 6. For more information on wheel diodes free, consult the electronics course of the article Getting started in electronics, in the chapter "Diodes".

IC-based H-bridge

There are integrated circuits equivalent to the diagram that we have just assembled, such as for example the L298 which makes it possible to control two DC motors. If you look at the manufacturer's instructions for the L298 (figure 4), you will find similarities there with figure 3; there are two H-bridges since the circuit can drive two DC motors. Still, the L298 IC is more sophisticated than we realized, because it has built-in control logic.

Figure 5 (from the manufacturer's datasheet) shows how to interface half of an L298 circuit to control a motor; the assembly must be done in duplicate from the same L298 circuit to control two motors. Note the four freewheeling diodes that we mentioned in the previous paragraph to protect the L298 circuit from the currents induced by the DC motor windings. The small table in figure 5 summarizes the behavior of the circuit according to the logic levels (H for HIGH and L for LOW) on pins C and D (numbered) of the circuit. We have a forward direction (Forward in English), a reverse direction (Reverse in English) and even the possibility of a fast stop (Fast motor stop in English) if the logic levels are identical on the inputs C and D of the circuit. This requires that the logic level on the "En" pin (Enable) is H. If the level is L, the motor runs in freewheel mode until it stops by itself (Free running motor stop) and in which case, it doesn't matter (Don't care) the levels on C and D. It is precisely on the "En" input that a PWM signal can be applied to regulate the speed of the motor.

Notice in figure 5 how the free wheel diodes D1 to D4 are mounted.

This control logic can of course be connected to the Arduino outputs and Figure 6 shows us how to connect the L298 circuit to the Arduino outputs. Again, we notice the four freewheeling diodes at the motor terminals.

The signals applied to inputs In1 (5) and In2 (7) determine the direction of rotation of the motor (or its rapid stop). The signal applied to the EnA input (6) controls the rotation speed if it is of the PWM type. Again, if you want to control two DC motors, the mounting must be done twice.

There are other integrated circuits performing the same thing as the L298, with superior performance, such as the LMD18200 circuit which is preferred by the Arduino staff, but other circuits are also possible depending on the characteristics of the motors to be controlled. .

It is sometimes easier and cheaper to use this type of card when you have to control several motors, but it is also the ideal solution for those who are afraid of electronics. Simply plug the shield card onto the Uno or Mega module, connect the motor(s) and their power supply to the connector provided for this purpose, then develop a small program or find a ready-made one on the many forums and adapt it according to their needs.

Each channel uses 4 pins of the Arduino module: 3 (or 11) for the PWM, 12 (or 13) to determine the direction of rotation, 9 (or 8) for the brake function (Brake in English) and the analog inputs A0 ( or A1) to read what each motor consumes (Current sensing function). If you don't need the Brake and Current Sensing functions, you can get the pins for something else by cutting a jumper on the back of the board.

Single Forward and Reverse Mosfet Bi-Directional DC Motor Driver

We all know how to use a DC motor; it suffices to connect its two poles to a direct current power supply (an electric battery for example) to make it turn. If the polarity of the power supply is reversed, the motor turns in the other direction. The higher the voltage of the power supply (without exceeding what the motor can admit), the higher the speed of rotation, and vice versa. It is on this principle that our trains are controlled in analog technology, the supply transformer delivering an adjustable and reversible voltage on the two rails.

Controlling a motor therefore means being able to adjust its speed and direction of rotation. Sometimes both are not necessary: the wheel of a watermill always turns in the same direction, the gondola cable always turns at the same speed, but a blow in one direction (up from the cabin) and a blow in the opposite direction (down from the cabin). Arduino can perfectly control a DC motor and thus create animations on the network. However, the current it can deliver (maximum 20 mA on one output) is insufficient and it is therefore necessary to add some electronics. Several solutions exist and are extremely simple to implement.

Motor speed control

To vary the speed of a DC motor, the supply voltage at the motor terminals must be varied. But Arduino only knows how to deliver voltages of 5 V on its pins with a current that is much too low to power a motor. The solution is to use PWM and amplify the signal with a transistor (see the article PWM: What is it? (2)) or an equivalent integrated circuit such as the ULN2803 circuit ( see the article Amplify the output signal of an ARDUINO with an ULN 2803).

Arduino, thanks to its analogWrite function, creates a square wave signal switching between low level (LOW = 0 V) and high level (HIGH = + 5 V) of fixed frequency (on a Uno, 490 or 980 Hz approximately depending on the pins that deliver the signal), the duty cycle of which can be varied, that is to say the time when the signal is at the high level over the time of the period of the signal. A duty cycle of 50% means the signal is high 50% of the time. This is summarized in Figure 1.

This signal is generated on the Arduino pins mentioned PWM (marked with a tilde), thanks to the analogWrite function which specifies the duty cycle to adopt. The value is between 0 (for a duty cycle equal to 0%) and 255 (for a duty cycle equal to 100%), any intermediate value that can be adopted (example, 127 for a duty cycle of 50%). The signal is amplified by a transistor and as it works in switching, its power dissipation is almost zero. The motor which receives such a signal is always supplied with its nominal supply voltage, which means that its torque is maximum. On the other hand, the motor perceives this signal as a current of average voltage equal to the supply voltage multiplied by the duty cycle; it therefore rotates more slowly. To vary the speed of the motor, simply vary the duty cycle of the PWM signal delivered by Arduino.

Control of the direction of rotation with an H-bridge

The H-bridge will allow us to control the direction of rotation of the motor. Figure 2 explains the principle thanks to an assembly made up of four switches and the motor to be controlled.

All switches are open so current from the supply cannot reach the motor; this one is stopped. Let’s number our switches from A to D (clockwise) (see Figure 2). Above all, do not close switches A and D, or B and C, at the same time, as this would create a short circuit with the power supply. Let's start by closing switches A and C, B and D remaining open. The electric current reaches the motor following the path shown in yellow: the motor rotates in one direction. Open A and C: the motor stops (starting case). Let's now close switches B and D, A and C remaining open. The electric current reaches the motor following the path shown in purple: the motor turns in the other direction.

This may seem complicated to you, whereas an inverter assembly would have produced the same result, but the H bridge becomes interesting if the switches are replaced by electronic switches controlled by currents or voltages, such as transistors for example. To better understand how a transistor can become an electronic switch, refer to chapter 5 of the electronics course of the article Getting started in electronics.

H bridge with transistors

Figure 3 shows a classic assembly in electronics, called H bridge (H bridge) or double bridge (dual bridge). It consists of 4 NPN transistors called

H bridge with transistors

Figure 3 shows a classic assembly in electronics, called H bridge (H bridge) or double bridge (dual bridge). It consists of 4 NPN transistors called A, B, C and D (like our switches in the previous paragraph). To make the motor turn in direction 1, the current must follow the path shown in yellow. Transistors A and C must be on while transistors B and D must be off. It is the opposite to make the motor turn in direction 2, the current must follow the path shown in purple; transistors B and D must be on and transistors A and C must be off.

In the electronics course mentioned above, we learned how to use switching transistors by applying a signal to its base. If we connect the bases of the transistors to the digital outputs 7, 8, 9 and 10 of Arduino (for example), it is then sufficient to apply HIGH or LOW signals to these outputs so that our transistors are on or off. The following table summarizes the situation.

Arduino outputs 9 and 10 can provide a PWM type signal; the assembly is then used to reverse the direction of rotation of a motor while controlling its rotation speed.

Of course, each time a load made up of windings is used (in the case of a motor), it is necessary to protect the control electronics (transistors or Arduino) with freewheel diodes. These have not been shown in Figure 3 (so as not to overload the figure) but should be mounted as shown in the following paragraphs, especially in Figures 5 and 6. For more information on wheel diodes free, consult the electronics course of the article Getting started in electronics, in the chapter "Diodes".

IC-based H-bridge

There are integrated circuits equivalent to the diagram that we have just assembled, such as for example the L298 which makes it possible to control two DC motors. If you look at the manufacturer's instructions for the L298 (figure 4), you will find similarities there with figure 3; there are two H-bridges since the circuit can drive two DC motors. Still, the L298 IC is more sophisticated than we realized, because it has built-in control logic.

Figure 5 (from the manufacturer's datasheet) shows how to interface half of an L298 circuit to control a motor; the assembly must be done in duplicate from the same L298 circuit to control two motors. Note the four freewheeling diodes that we mentioned in the previous paragraph to protect the L298 circuit from the currents induced by the DC motor windings. The small table in figure 5 summarizes the behavior of the circuit according to the logic levels (H for HIGH and L for LOW) on pins C and D (numbered) of the circuit. We have a forward direction (Forward in English), a reverse direction (Reverse in English) and even the possibility of a fast stop (Fast motor stop in English) if the logic levels are identical on the inputs C and D of the circuit. This requires that the logic level on the "En" pin (Enable) is H. If the level is L, the motor runs in freewheel mode until it stops by itself (Free running motor stop) and in which case, it doesn't matter (Don't care) the levels on C and D. It is precisely on the "En" input that a PWM signal can be applied to regulate the speed of the motor.

Notice in figure 5 how the free wheel diodes D1 to D4 are mounted.

This control logic can of course be connected to the Arduino outputs and Figure 6 shows us how to connect the L298 circuit to the Arduino outputs. Again, we notice the four freewheeling diodes at the motor terminals.

The signals applied to inputs In1 (5) and In2 (7) determine the direction of rotation of the motor (or its rapid stop). The signal applied to the EnA input (6) controls the rotation speed if it is of the PWM type. Again, if you want to control two DC motors, the mounting must be done twice.

There are other integrated circuits performing the same thing as the L298, with superior performance, such as the LMD18200 circuit which is preferred by the Arduino staff, but other circuits are also possible depending on the characteristics of the motors to be controlled. .

It is sometimes easier and cheaper to use this type of card when you have to control several motors, but it is also the ideal solution for those who are afraid of electronics. Simply plug the shield card onto the Uno or Mega module, connect the motor(s) and their power supply to the connector provided for this purpose, then develop a small program or find a ready-made one on the many forums and adapt it according to their needs.

Each channel uses 4 pins of the Arduino module: 3 (or 11) for the PWM, 12 (or 13) to determine the direction of rotation, 9 (or 8) for the brake function (Brake in English) and the analog inputs A0 ( or A1) to read what each motor consumes (Current sensing function). If you don't need the Brake and Current Sensing functions, you can get the pins for something else by cutting a jumper on the back of the board.