Bird
0
0
Arduinoprogramming~10 mins

Motor direction with H-Bridge (L293D/L298N) in Arduino - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Motor direction with H-Bridge (L293D/L298N)
Set pin modes
Write HIGH/LOW to pins
Motor spins forward
Delay
Write reversed HIGH/LOW
Motor spins backward
Delay
Repeat or stop
The program sets motor control pins, then writes signals to spin the motor forward or backward by controlling the H-Bridge inputs.
Execution Sample
Arduino
const int motorPin1 = 3;
const int motorPin2 = 4;

void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
}

void loop() {
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  delay(2000);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  delay(2000);
}
This code spins the motor forward for 2 seconds, then backward for 2 seconds repeatedly.
Execution Table
StepActionmotorPin1motorPin2Motor DirectionDelay (ms)
1Set motorPin1 HIGH, motorPin2 LOWHIGHLOWForward2000
2Wait for 2000 msHIGHLOWForward2000
3Set motorPin1 LOW, motorPin2 HIGHLOWHIGHBackward2000
4Wait for 2000 msLOWHIGHBackward2000
5Loop repeats from step 1HIGHLOWForward2000
💡 Loop runs indefinitely, alternating motor direction every 2 seconds.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5 (Loop)
motorPin1LOW (default)HIGHLOWHIGH
motorPin2LOW (default)LOWHIGHLOW
Motor DirectionStoppedForwardBackwardForward
Key Moments - 3 Insights
Why does setting motorPin1 HIGH and motorPin2 LOW make the motor spin forward?
Because the H-Bridge uses these pin signals to control current flow direction through the motor, making it spin forward as shown in execution_table step 1.
What happens if both motorPin1 and motorPin2 are set HIGH or LOW at the same time?
The motor will stop or brake because no voltage difference is applied; this is not shown in the current execution but is important to avoid damage.
Why do we use delay after setting the pins?
Delay keeps the motor spinning in one direction for a set time before changing direction, as seen in execution_table steps 2 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the state of motorPin2 at step 3?
ALOW
BHIGH
CINPUT
DNot set
💡 Hint
Check the 'motorPin2' column in execution_table row for step 3.
At which step does the motor spin backward?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Motor Direction' column in execution_table.
If delay(2000) is changed to delay(1000), how does the execution_table change?
ADelay column values become 1000 instead of 2000
BMotor direction changes to stop
CPin states change to LOW
DNo change
💡 Hint
Focus on the 'Delay (ms)' column in execution_table.
Concept Snapshot
Motor direction with H-Bridge:
- Use two pins to control motor direction
- Set one pin HIGH, other LOW for forward
- Reverse pins for backward
- Use delay to keep motor spinning
- Loop to alternate direction
Full Transcript
This example shows how to control a motor's direction using an H-Bridge like L293D or L298N with Arduino. We set two pins as outputs. Writing HIGH to one pin and LOW to the other makes the motor spin forward. Reversing these signals makes it spin backward. Delays keep the motor spinning in each direction for 2 seconds. The loop repeats this forever. The execution table tracks pin states and motor direction step-by-step. Key points include how pin signals control current flow and why delays are needed. The quiz checks understanding of pin states and timing.