Bird
0
0
Arduinoprogramming~30 mins

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

Choose your learning style9 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.