Why a Motor Driver?
Arduino GPIO pins can only source/sink ~40 mA — not enough to drive a motor directly. Motor drivers (H-bridges) use transistors to switch higher currents from an external power supply while keeping the Arduino safely isolated.
DC Motor with L298N
The L298N drives two DC motors. For one motor:
Arduino pin 6 (PWM) → L298N ENA (speed)
Arduino pin 7 → L298N IN1 (direction)
Arduino pin 8 → L298N IN2 (direction)
External 6–12V → L298N VIN
GND shared between Arduino and L298N
const int ENA = 6, IN1 = 7, IN2 = 8;
void setup() {
pinMode(ENA, OUTPUT); pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT);
}
void forward(int speed) {
analogWrite(ENA, speed); // 0–255
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
}
void backward(int speed) {
analogWrite(ENA, speed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
}
void stopMotor() { analogWrite(ENA, 0); }
Servo Control
Servos receive a PWM signal (50 Hz, 1–2 ms pulse width) to hold a specific angle.
Arduino’s Servo library handles the timing:
#include <Servo.h>
Servo myServo;
void setup() {
myServo.attach(9); // any digital pin
}
void loop() {
for (int angle = 0; angle <= 180; angle += 5) {
myServo.write(angle);
delay(20);
}
for (int angle = 180; angle >= 0; angle -= 5) {
myServo.write(angle);
delay(20);
}
}
Stepper Motor (28BYJ-48 + ULN2003)
The 28BYJ-48 is an inexpensive 5V geared stepper (2048 steps/rev) driven by a ULN2003 board.
#include <AccelStepper.h> // install: "AccelStepper" by Mike McCauley
// ULN2003 driver pins: IN1 IN2 IN3 IN4
AccelStepper stepper(AccelStepper::HALF4WIRE, 8, 10, 9, 11);
void setup() {
stepper.setMaxSpeed(500); // steps/sec
stepper.setAcceleration(200);
stepper.moveTo(2048); // one full revolution
}
void loop() {
if (stepper.distanceToGo() == 0) {
stepper.moveTo(-stepper.currentPosition()); // reverse
}
stepper.run(); // call as often as possible — non-blocking
}
Exercises
- Control DC motor speed with a potentiometer; reverse direction with a button
- Use a servo as a “gauge needle” that maps to ADC reading (0 → 0°, 1023 → 180°)
- Combine stepper + button: press to advance exactly 512 steps (quarter turn)