Using an Arduino to control motors is one of the most common and beginner-friendly projects. Arduino is often better suited for motor control than a Raspberry Pi for simple applications because it's designed for real-time I/O and can handle the electrical noise from motors more robustly.
Here’s a practical Arduino playbook for the most common motor types—pick yours, wire it safely, then drop in the sample code.
Power & safety (read first)
-
Do not power motors from the Arduino 5V pin. Use a separate supply sized for the motor’s stall current; tie grounds together (motor supply GND ↔ Arduino GND).
-
Arduino pins source only a few mA—they drive a driver, not the motor.
-
Add a flyback diode across DC motors/solenoids (stripe to +V) unless your driver has them built-in.
-
Keep high-current wires short; add bulk caps near drivers (e.g., 100–470 µF + 0.1 µF).
1) Brushed DC motor
A) One direction (simple, cheap): N-MOSFET + diode
Parts: Logic-level N-MOSFET (e.g., IRLZ44N/IRLZ34N/AOZ128x), 1N5819/1N4007 diode, 100 Ω gate resistor, 10 kΩ gate pulldown.
Wiring (low-side switch):
-
Motor + → external supply +
-
Motor − → MOSFET D
-
MOSFET S → GND
-
Arduino PWM pin (e.g., D9) → 100 Ω → MOSFET G; 10 kΩ G→GND
-
Diode across motor (stripe to +)
Code (speed control with PWM):
(Default PWM is ~490 Hz on most pins; ~980 Hz on 5/6. Audible whine is normal at low kHz.)
B) Both directions: H-bridge (TB6612FNG/DRV8833 preferred)
Wiring (one channel): PWMA → D5
, AIN1 → D7
, AIN2 → D6
, STBY → D8 (HIGH)
, motor across A01/A02
, VM to motor supply, VCC to 5 V, GND common.
Code:
2) Hobby servo (SG90, MG996R, etc.)
-
Provide 5–6 V from a separate supply; connect GNDs together.
-
Signal wire goes to any digital pin (Servo library handles timing).
Code (position control):
3) Stepper motor
A) Bipolar stepper + A4988/DRV8825/TMC (STEP/DIR)
Basics: Set current limit (Vref), choose microstep mode (MS1–MS3), add 100–220 µF near VMOT.
Wiring: STEP → D2
, DIR → D3
, EN → D4 (LOW=enable)
. Coils to A1/A2/B1/B2, VMOT to motor supply, GND common.
Code:
Tip: For smooth moves, use acceleration profiles (e.g., AccelStepper library).
B) 28BYJ-48 + ULN2003 (unipolar)
Use the built-in Stepper library or AccelStepper’s FULL4WIRE
. Connect IN1..IN4
to four Arduino pins.
4) BLDC via ESC (RC motors/fans)
ESCs accept servo-style pulses (≈1000–2000 µs). Many need an arming sequence.
Code:
Common pitfalls (and fixes)
-
Motor won’t spin / resets Arduino: separate motor supply, add bulk cap, confirm common ground.
-
Driver overheating: check current limit (steppers), add heatsink/airflow, ensure proper VMOT voltage.
-
Noisy sensors / random behavior: twist motor leads, keep power/logic grounds star-connected, route motor wires away from signal lines, add snubbers/ferrites if needed.
-
Servo jitter: dedicated 5–6 V rail with enough current; avoid long, thin wires.
Quick parts cheat sheet
-
DC one-way: IRLZ44N + 1N5819 + 100 Ω gate + 10 k pulldown
-
DC two-way: TB6612FNG (efficient) or DRV8833
-
Stepper: A4988/DRV8825/TMC2209 + AccelStepper
-
Servo/ESC control:
Servo.h
(use external 5–6 V supply)