A DC motor needs more voltage and current than an Arduino Mega pin can supply, so this build adds an L298N driver between the two. You send it a PWM signal to set speed and flip two digital pins to set direction, which is a nice pattern to know before you move on to steppers or servos.
The full tutorial, with every detail and the line-by-line code explanation, is on our site: Arduino Mega - DC Motor
What you need
- Arduino MEGA
- DIYables MEGA Development Board
- USB 2.0 cable type A/B (for USB-A PC)
- USB 2.0 cable type C/B (for USB-C PC)
- L298N Motor Driver Module
- 5V DC Motor
- 5V Power Adapter for 5V DC motor
- DC Power Jack
- Jumper Wires
- Screw Terminal Block Shield for Arduino Uno/Mega
- Sensors/Servo Expansion Shield for Arduino Mega
- Breadboard Shield for Arduino Mega
- Enclosure for Arduino Mega
- DIYables Sensor Kit (18 sensors/displays)
Wiring
The motor's two wires go to the L298N's OUT1 and OUT2 pins. The driver's VCC pin takes the motor's own power supply (5 to 35V), GND is common ground shared with the Arduino, and the 5V pin can be powered straight from the Arduino's 5V. ENA gets a PWM-capable pin from the Mega to control speed, while IN1 and IN2 go to two digital pins to control direction. Before wiring anything, remove all three jumpers on the L298N module so ENA, IN1 and IN2 are free to be driven by the Arduino instead of staying tied high.
How it works
A DC motor just has a positive and a negative wire, usually red and black. Feed it the rated voltage one way and it spins clockwise at full speed; swap the wires and it spins counterclockwise. Giving it less voltage spins it slower, but changing voltage directly is impractical, so the driver instead uses PWM: it switches the full voltage on and off fast, and a higher duty cycle means more average power and more speed. The L298N takes a weak PWM signal from the Arduino and uses it to switch the real motor voltage, and it uses IN1/IN2 to swap polarity for direction. One pin high and one low spins the motor one way; both the same stops it.
Code
The sketch sets ENA, IN1 and IN2 as outputs, then uses analogWrite() on the ENA pin with a value from 0 to 255 to set speed, where 0 stops the motor and 255 is full speed. Direction comes from digitalWrite() on IN1 and IN2: one high and one low spins the motor, and matching values (both low or both high) stops it. The demo sketch ramps the motor up, reverses direction, slows it down, and stops it, showing all the control pieces together.
arduino-mega-dc-motor.ino
/*
* This Arduino Mega code was developed by newbiely.com
*
* This Arduino Mega code is made available for public use without any restriction
*
* For comprehensive instructions and wiring diagrams, please visit:
* https://newbiely.com/tutorials/arduino-mega/arduino-mega-dc-motor
*/
#define ENA_PIN 9 // The Arduino Mega pin connected to the EN1 pin L298N
#define IN1_PIN 6 // The Arduino Mega pin connected to the IN1 pin L298N
#define IN2_PIN 3 // The Arduino Mega pin connected to the IN2 pin L298N
void setup() {
// initialize digital pins as outputs.
pinMode(ENA_PIN, OUTPUT);
pinMode(IN1_PIN, OUTPUT);
pinMode(IN2_PIN, OUTPUT);
}
void loop() {
digitalWrite(IN1_PIN, HIGH); // control motor A spins clockwise
digitalWrite(IN2_PIN, LOW); // control motor A spins clockwise
for (int speed = 0; speed <= 255; speed++) {
analogWrite(ENA_PIN, speed); // control the speed
delay(10);
}
delay(1000); // rotate at maximum speed 1 seconds in in clockwise direction
// change direction
digitalWrite(IN1_PIN, LOW); // control motor A spins anti-clockwise
digitalWrite(IN2_PIN, HIGH); // control motor A spins anti-clockwise
delay(1000); // rotate at maximum speed 1 seconds in in anti-clockwise direction
for (int speed = 255; speed >= 0; speed--) {
analogWrite(ENA_PIN, speed); // control the speed
delay(10);
}
delay(1000); // stop motor 1 second
}
Things that go wrong
If the motor doesn't move at all, check that you removed the ENA, IN1 and IN2 jumpers on the L298N; leaving them in place stops the Arduino pins from having any effect. If the motor runs weak or not at full strength, confirm the driver's VCC is wired to a supply matching the motor's rated voltage, not just the Arduino's 5V. If the motor spins the wrong way, just swap the two motor wires or flip the IN1/IN2 logic in code.
Full tutorial
The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/arduino-mega/arduino-mega-dc-motor

Top comments (0)