Tutorial 14 – Using a Motor Driver with Arduino
Welcome back to the CraftedTech Engineering Arduino tutorial series! In this tutorial, we’ll show you how to use an H-Bridge motor driver with Arduino to control motors in both directions. This opens up a world of possibilities, such as controlling robotic vehicles, automated systems, and much more.
🔌 What You’ll Need
- 1 Arduino Board (e.g., Arduino UNO)
- 1 H-Bridge Motor Driver (e.g., L298N)
- 1 DC Motor
- External power supply for the motor
- Breadboard
- Jumper wires
- USB cable for Arduino
You can order the required components here:
🧠 What Is an H-Bridge Motor Driver?
An H-Bridge motor driver is a circuit that allows you to control the direction and speed of a DC motor. By switching the current’s direction through the motor, the motor can rotate forward or backward, making it ideal for controlling robotics or any system that requires bidirectional movement.
🧾 Step-by-Step Instructions
🔧 Step 1: Wiring the Motor Driver
- Connect the VCC pin of the L298N motor driver to the 5V pin on the Arduino.
- Connect the GND pin of the L298N to the GND pin on the Arduino.
- Connect the IN1 and IN2 pins of the L298N to digital pins 9 and 10 on the Arduino (for controlling direction).
- Connect the ENA pin to 5V (for enabling the motor driver).
- Connect the OUT1 and OUT2 pins to the motor terminals.
- Connect an external power supply (e.g., 9V battery) to the motor's power input on the L298N (if needed for higher voltage motors).
💻 Step 2: Arduino Code to Control the Motor
const int motorPin1 = 9; // IN1 pin connected to digital pin 9
const int motorPin2 = 10; // IN2 pin connected to digital pin 10
void setup() {
pinMode(motorPin1, OUTPUT); // Set motor pins as outputs
pinMode(motorPin2, OUTPUT);
}
void loop() {
// Motor forward
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
delay(2000); // Motor runs forward for 2 seconds
// Motor backward
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
delay(2000); // Motor runs backward for 2 seconds
}
🔍 Code Breakdown
- pinMode(): Configures the motor pins as outputs.
- digitalWrite(): Controls the direction of the motor by setting the IN1 and IN2 pins.
- delay(): Pauses the program for the specified amount of time, allowing the motor to run in one direction for 2 seconds before reversing.
🧪 Try This!
- Change the delay() times to make the motor run for different durations in each direction.
- Experiment with using PWM pins for controlling motor speed.
🛠️ Troubleshooting Tips
- Ensure the motor power supply is sufficient for your motor.
- Double-check the wiring, especially the connections to IN1 and IN2 pins.
- If the motor doesn't move, check if the ENA pin is connected to 5V to enable the motor driver.
🧭 What You Learned in This Tutorial
✔️ How to wire and control a motor driver with Arduino
✔️ How to control the direction of a DC motor using an H-Bridge
✔️ How to reverse the motor direction programmatically
✔️ How to use digitalWrite() to control motor movement
📌 Next Up: Tutorial 15 – Controlling a Servo Motor with Arduino
In the next tutorial, we’ll show you how to control a servo motor using Arduino. Servos are great for precise angular movements, such as in robotic arms or automated doors.
📣 Don’t forget to follow CraftedTech Engineering for more hands-on tutorials and project ideas!
🔗 Website: craftedtechengineering.com
📘 Facebook: @CraftedTechEngineering
📺 YouTube: @CraftedTechEngineering