Introduction: Use of the L298 H-Bridge With Arduino
General Overview of an H-Bridge
An H-Bridge is a series of transistors which allows you to control both the direction and speed of a motor. The L298 H-Bridge makes use of three pins per motor in order to control the motor. On the L298 H-Bridge these pins are as follows: EnB, InC and InD. Every H-Bridge also has two power inputs, one for the motor and one for the H-Bridge itself. On the L298 VCC is the power supply to the motors and +5 is the power supply to the H-Bridge.
Step 1: Use of the L298 H-Bridge
The EnB pin is referred to as the enable pin. It controls when the motor is on and off and is also used to control the speed of the motor.
The InC and InD pins control the direction of the motor. If one is powered(HIGH) and the other is unpowered(LOW) then the motor will turn. If both are in the same state(HIGH, HIGH or LOW,LOW) then the motor will not turn. Thus, these pins can also be used to turn the motor on or off although it is easier to use the enable pin.
Step 2: Wiring for One Motor
The wiring is as follows
Note that the enable pin(EnB) must be connected to a PWM pin on the arduino
<p>L298 H-Bridge Arduino External Power Supply <br>+5 5V GND GND GND GND VCC Positive terminal EnB 5 InC 7 InD 8</p>
Step 3: Coding
Here is the code that works with the wiring above
<p>const int EnB = 5; //Defining pins as variables-good practice<br>const int InC = 7;
const int InD = 8;
int motorSpeed = 255; //variable for motor speed. Could be linked to an input to control motor</p><p>void setup() {
// put your setup code here, to run once:
pinMode(EnB, OUTPUT); //telling Arduino which pins are sending out signals
pinMode(InC, OUTPUT);
pinMode(InD, OUTPUT);
}</p><p>void loop() {
// put your main code here, to run repeatedly:
digitalWrite(InC, HIGH); //Changing InC to LOW and InD to HIGH will rotate the motor in the oppsite direction
digitalWrite(InD, LOW);
analogWrite(EnB, motorSpeed); //How fast the motor will turn. Set this to anything between and including 0 and 255
}</p>Step 4: Wiring and Coding for a Second Motor
The wiring and coding for a second motor is exactly the same aside from using different pins.
On the L298 H-Bridge these pins are EnA, InA and InB. Remember to connect all three to digital pins and EnA to its own PWM pin.





