Bird
Raised Fist0
Arduinoprogramming~30 mins

Motor direction with H-Bridge (L293D/L298N) in Arduino - Mini Project: Build & Apply

Choose your learning style10 modes available
Motor direction with H-Bridge (L293D/L298N)
📖 Scenario: You have a small DC motor connected to an H-Bridge motor driver (L293D or L298N). You want to control the motor's direction by sending signals from an Arduino board.The motor driver has two input pins that control the motor direction: IN1 and IN2. Setting IN1 HIGH and IN2 LOW makes the motor spin forward. Setting IN1 LOW and IN2 HIGH makes the motor spin backward. Setting both LOW stops the motor.
🎯 Goal: You will write an Arduino program that sets up the motor control pins, defines a direction variable, and uses digitalWrite to control the motor direction based on that variable.
📋 What You'll Learn
Create two integer variables IN1 and IN2 for motor control pins with values 8 and 9 respectively.
Create an integer variable direction to store motor direction (1 for forward, 0 for stop, -1 for backward).
Use pinMode to set IN1 and IN2 as outputs in setup().
Use if statements in loop() to set the motor direction using digitalWrite.
Print the current motor direction to the Serial Monitor.
💡 Why This Matters
🌍 Real World
Controlling motor direction is essential in robotics, remote-controlled vehicles, and automation projects where motors need to move forward or backward.
💼 Career
Understanding how to control motors with microcontrollers and motor drivers is a key skill for embedded systems engineers, robotics developers, and hardware programmers.
Progress0 / 4 steps
1
Set up motor control pins
Create two integer variables called IN1 and IN2 and set them to 8 and 9 respectively.
Arduino
Hint

Use int to create variables for the motor pins.

2
Add motor direction variable and setup pins
Create an integer variable called direction and set it to 1 (forward). Then, write a setup() function where you use pinMode to set IN1 and IN2 as OUTPUT. Also, start serial communication with Serial.begin(9600);.
Arduino
Hint

Remember to set the motor pins as outputs and start serial communication.

3
Control motor direction in loop
Write a loop() function that uses if statements to check the value of direction. If direction is 1, set IN1 HIGH and IN2 LOW. If direction is -1, set IN1 LOW and IN2 HIGH. If direction is 0, set both IN1 and IN2 LOW.
Arduino
Hint

Use digitalWrite to set the motor pins HIGH or LOW based on the direction.

4
Print motor direction to Serial Monitor
Inside the loop() function, after setting the motor pins, add code to print the motor direction as text to the Serial Monitor. Print "Motor direction: Forward" if direction is 1, "Motor direction: Backward" if direction is -1, and "Motor direction: Stopped" if direction is 0.
Arduino
Hint

Use Serial.println() to print the motor direction text.

Practice

(1/5)
1. Which of the following correctly describes how to make a motor spin forward using an H-Bridge (L293D/L298N) with two Arduino pins pinA and pinB?
easy
A. Set both pinA and pinB HIGH
B. Set pinA LOW and pinB HIGH
C. Set both pinA and pinB LOW
D. Set pinA HIGH and pinB LOW

Solution

  1. Step 1: Understand motor direction control

    To spin the motor forward, one pin must be HIGH and the other LOW to create current flow in one direction.
  2. Step 2: Apply pin states for forward spin

    Setting pinA HIGH and pinB LOW makes the motor spin forward.
  3. Final Answer:

    Set pinA HIGH and pinB LOW -> Option D
  4. Quick Check:

    Forward spin = pinA HIGH, pinB LOW [OK]
Hint: Forward spin means one pin HIGH, other LOW [OK]
Common Mistakes:
  • Setting both pins HIGH or LOW thinking motor spins
  • Confusing which pin should be HIGH for forward
  • Assuming both pins LOW spins motor forward
2. Which Arduino code snippet correctly sets pin1 HIGH and pin2 LOW to spin a motor forward using an H-Bridge?
easy
A. digitalWrite(pin1, LOW); digitalWrite(pin2, HIGH);
B. digitalWrite(pin1, HIGH); digitalWrite(pin2, LOW);
C. digitalWrite(pin1, HIGH); digitalWrite(pin2, HIGH);
D. digitalWrite(pin1, LOW); digitalWrite(pin2, LOW);

Solution

  1. Step 1: Recall correct pin states for forward

    Forward spin requires first pin HIGH and second pin LOW.
  2. Step 2: Match code to pin states

    Code in digitalWrite(pin1, HIGH); digitalWrite(pin2, LOW); sets pin1 HIGH and pin2 LOW correctly.
  3. Final Answer:

    digitalWrite(pin1, HIGH); digitalWrite(pin2, LOW); -> Option B
  4. Quick Check:

    Pin1 HIGH, Pin2 LOW = forward spin [OK]
Hint: HIGH then LOW means forward spin [OK]
Common Mistakes:
  • Swapping HIGH and LOW values
  • Setting both pins HIGH or LOW
  • Using analogWrite instead of digitalWrite
3. What will be the motor behavior when the following Arduino code runs?
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
medium
A. Motor spins backward
B. Motor spins forward
C. Motor stops
D. Motor speed changes but direction stays same

Solution

  1. Step 1: Identify pin states and their meaning

    Pin 3 is LOW and pin 4 is HIGH, which reverses the current flow compared to forward spin.
  2. Step 2: Determine motor direction

    This pin combination makes the motor spin backward.
  3. Final Answer:

    Motor spins backward -> Option A
  4. Quick Check:

    LOW then HIGH = backward spin [OK]
Hint: LOW then HIGH means backward spin [OK]
Common Mistakes:
  • Assuming LOW then HIGH is forward spin
  • Thinking motor stops when pins differ
  • Confusing speed control with direction
4. The following code is intended to stop the motor, but it does not. What is the error?
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
medium
A. Pins must be set to LOW and HIGH to stop
B. Pins should be set to INPUT to stop motor
C. Both pins HIGH causes motor to brake, not stop
D. digitalWrite cannot be used to stop motor

Solution

  1. Step 1: Understand stopping motor with H-Bridge

    To stop the motor, both pins should be LOW to cut power and stop current flow.
  2. Step 2: Analyze given pin states

    Both pins are set HIGH, which causes the motor to brake but not coast to a stop.
  3. Final Answer:

    Both pins HIGH causes motor to brake, not stop -> Option C
  4. Quick Check:

    Stop motor = both pins LOW, not HIGH [OK]
Hint: Stop motor by setting both pins LOW [OK]
Common Mistakes:
  • Setting both pins HIGH thinking motor stops
  • Using INPUT mode instead of LOW
  • Confusing brake with stop
5. You want to write Arduino code to spin a motor forward for 3 seconds, then stop for 2 seconds, then spin backward for 3 seconds using pins 7 and 8 with an L298N. Which code snippet correctly implements this?
hard
A. digitalWrite(7, HIGH); digitalWrite(8, LOW); delay(3000); digitalWrite(7, LOW); digitalWrite(8, LOW); delay(2000); digitalWrite(7, LOW); digitalWrite(8, HIGH); delay(3000);
B. digitalWrite(7, LOW); digitalWrite(8, HIGH); delay(3000); digitalWrite(7, LOW); digitalWrite(8, LOW); delay(2000); digitalWrite(7, HIGH); digitalWrite(8, LOW); delay(3000);
C. digitalWrite(7, HIGH); digitalWrite(8, HIGH); delay(3000); digitalWrite(7, LOW); digitalWrite(8, LOW); delay(2000); digitalWrite(7, LOW); digitalWrite(8, LOW); delay(3000);
D. digitalWrite(7, LOW); digitalWrite(8, LOW); delay(3000); digitalWrite(7, HIGH); digitalWrite(8, LOW); delay(2000); digitalWrite(7, LOW); digitalWrite(8, HIGH); delay(3000);

Solution

  1. Step 1: Set pins for forward spin and delay

    Forward spin requires pin 7 HIGH and pin 8 LOW, then delay 3000 ms (3 seconds).
  2. Step 2: Stop motor by setting both pins LOW and delay

    Both pins LOW stops motor, delay 2000 ms (2 seconds).
  3. Step 3: Set pins for backward spin and delay

    Backward spin requires pin 7 LOW and pin 8 HIGH, delay 3000 ms.
  4. Final Answer:

    digitalWrite(7, HIGH); digitalWrite(8, LOW); delay(3000); digitalWrite(7, LOW); digitalWrite(8, LOW); delay(2000); digitalWrite(7, LOW); digitalWrite(8, HIGH); delay(3000); -> Option A
  5. Quick Check:

    Forward, stop, backward with correct pins and delays [OK]
Hint: Forward=HIGH-LOW, Stop=LOW-LOW, Backward=LOW-HIGH [OK]
Common Mistakes:
  • Swapping forward and backward pin states
  • Not stopping motor between directions
  • Using HIGH-HIGH which brakes but doesn't stop
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard