Arduino Nano Motor Driver Module
The Motor Driver Module project demonstrates how to interface an H‑bridge motor driver (such as the L298N module) with an Arduino Nano to control DC motors. The module takes low‑voltage logic signals from the Arduino and drives one or two DC motors at higher voltage and current, allowing you to change both motor direction and speed (via PWM).
What Is a Motor Driver Module?
A typical motor driver module (e.g. L298N) contains an H‑bridge IC that can drive up to two DC motors independently, often with a supply range of roughly 5–35 V and currents up to 2 A per channel. The Arduino sends direction commands (IN1–IN4) and PWM signals to the enable pins (EN‑A/EN‑B) to control whether each motor runs forward, backward, brakes, or stops, and at what speed.
This module protects the Arduino by handling high‑current loads and allows you to control motors that would otherwise damage the Nano’s pins if driven directly.
Components Needed
- Arduino Nano
- Motor driver module (e.g. L298N)
- DC motor(s) (1–2 pcs, 3–12 V typically)
- External power supply (battery or adapter matching motor voltage)
- Jumper Wires
- Breadboard (optional)
Circuit Setup
1. Connect Motor Driver to Arduino Nano:
For a standard L298N‑style module controlling two DC motors:
- Bridge the 5V_EN jumper (if present) so the Arduino can power the logic side.
- Connect the Arduino Nano’s 5V pin to the module’s 5V logic supply and GND to the module’s GND.
- Connect Arduino digital pins to IN1, IN2 (for motor A) and IN3, IN4 (for motor B), e.g. D2–D5.
- Connect PWM pins (e.g. D9, D10) to ENA and ENB for speed control.
- Connect the external motor‑power supply (battery or adapter) to the module’s VIN and GND screw terminals.
2. Connect DC Motors:
- Motor A: Connect its two wires to the OUT‑A terminals on the module.
- Motor B (if used): Connect its wires to the OUT‑B terminals.
How the Motor Driver Works
The Arduino sets the direction of each motor by changing the states of IN1–IN2 (and IN3–IN4). For example, when IN1 = HIGH and IN2 = LOW, motor A runs forward; when IN1 = LOW and IN2 = HIGH, it runs backward; when both are the same the motor stops or brakes depending on the driver mode. The enable pin (ENA) is driven with PWM so the average voltage to the motor changes, thus controlling its speed.
Using this logic, the Arduino Nano can move a small robot forward, backward, left, and right by independently controlling two motors, or drive a single motor with variable speed for fans, conveyors, and toy‑car projects.
/*
Motor Driver Module (e.g. L298N)
with Arduino Nano
*/
// Motor A pins
const int in1 = 2;
const int in2 = 3;
const int enA = 9;
// Motor B pins
const int in3 = 4;
const int in4 = 5;
const int enB = 10;
// Speed level (0–255, PWM duty cycle)
const int speed = 150;
void setup() {
// Motor A
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(enA, OUTPUT);
// Motor B
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(enB, OUTPUT);
// Start with motors off
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
analogWrite(enA, 0);
analogWrite(enB, 0);
Serial.begin(9600);
Serial.println("=== MOTOR DRIVER MODULE READY ===");
}
void loop() {
moveForward();
delay(2000);
moveBackward();
delay(2000);
turnLeft();
delay(2000);
turnRight();
delay(2000);
stopMotors();
delay(1000);
}
void moveForward() {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enA, speed);
analogWrite(enB, speed);
Serial.println("FORWARD");
}
void moveBackward() {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enA, speed);
analogWrite(enB, speed);
Serial.println("BACKWARD");
}
void turnLeft() {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enA, speed);
analogWrite(enB, speed);
Serial.println("LEFT");
}
void turnRight() {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enA, speed);
analogWrite(enB, speed);
Serial.println("RIGHT");
}
void stopMotors() {
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
analogWrite(enA, 0);
analogWrite(enB, 0);
Serial.println("STOP");
}
Instructions
1. Circuit Setup:
Wire the motor driver module to the Arduino Nano and connect the DC motor(s) and external power supply as described in the circuit section. Make sure the motor power and Arduino power share a common GND but do not attempt to power large motors from the Arduino’s 5V pin alone.
2. Code Upload:
Connect the Arduino Nano to your computer via USB, open the Arduino IDE, paste the motor‑driver code, and upload it to the board.
3. Testing:
After upload, the motors should repeatedly move forward, backward, turn left, turn right, and stop. Adjust the speed constant (0–255) to change how fast the motors run, and verify that the direction of each motor matches the wheel orientation on your robot‑chassis or test platform.
Applications
Robot Movement: Use two motor‑driver channels to drive a small Arduino‑based robot forward, backward, and turn left/right.
Conveyor & Automation: Control motors for small conveyor belts, toy cars, or industrial‑style actuators that push or pull objects.
Fan & Tool Control: Drive DC fans, pumps, or small drills with variable speed, ideal for DIY weather‑station vents or cooling systems.
Troubleshooting
- Motors turn in the wrong direction? Swap the motor’s two wires at the driver output or reverse the IN1/IN2 (and IN3/IN4) logic in the code.
- Motor doesn’t move at all? Check that the external power supply is connected to the driver, that the 5V_EN jumper is set when using the Arduino’s 5V, and that the enable pins receive PWM.
- Driver gets hot or resets? Ensure the motor current is within the module’s limit and avoid long stalls at maximum load; add a small heatsink if needed.
Best Practices and Notes
- Always use an external power supply for the motors; the Arduino Nano’s 5V pin alone is not enough for even small DC motors under load.
- Verify that the motor voltage and current are within the driver’s rated range and that the wiring is secure to avoid intermittent contact.
- Use diodes or flyback components if not built into the driver; these protect the circuit from inductive voltage spikes when the motor stops abruptly.
Extensions and Ideas
You can extend this project by adding an LDR or ultrasonic sensor so the robot avoids obstacles, or by using a Bluetooth module to control motor directions from a phone app, or by reading joystick inputs to control a more advanced motor‑based platform.