How to Control DC Motor with Arduino: Simple Guide
To control a DC motor with
Arduino, use a motor driver like the L298N or a transistor circuit connected to Arduino pins. Write code to set the motor direction and speed using digitalWrite() for direction and analogWrite() for speed control via PWM signals.Syntax
To control a DC motor, you typically use two Arduino pins: one for direction and one for speed (PWM). The main functions are:
pinMode(pin, OUTPUT);- sets the pin as output.digitalWrite(pin, HIGH/LOW);- sets motor direction.analogWrite(pin, value);- controls motor speed with PWM (0-255).
You connect the motor driver inputs to these pins and power the motor separately.
arduino
pinMode(directionPin, OUTPUT); pinMode(speedPin, OUTPUT); digitalWrite(directionPin, HIGH); // Motor forward analogWrite(speedPin, 200); // Speed control (0-255)
Example
This example shows how to control a DC motor's direction and speed using an Arduino and an L298N motor driver. The motor runs forward at half speed for 3 seconds, then backward at full speed for 3 seconds.
arduino
const int directionPin = 8; const int speedPin = 9; void setup() { pinMode(directionPin, OUTPUT); pinMode(speedPin, OUTPUT); } void loop() { // Motor forward at half speed digitalWrite(directionPin, HIGH); analogWrite(speedPin, 128); // 50% speed delay(3000); // Motor backward at full speed digitalWrite(directionPin, LOW); analogWrite(speedPin, 255); // 100% speed delay(3000); }
Output
Motor runs forward at half speed for 3 seconds, then backward at full speed for 3 seconds, repeating.
Common Pitfalls
- Not using a motor driver or transistor can damage the Arduino because motors draw more current than Arduino pins can supply.
- Forgetting to connect the motor power supply ground to Arduino ground causes erratic behavior.
- Using
digitalWrite()instead ofanalogWrite()for speed control means the motor runs only full on or off. - Not adding a flyback diode when using transistors can damage components from voltage spikes.
arduino
/* Wrong way: Directly powering motor from Arduino pin (can damage Arduino) */ pinMode(9, OUTPUT); digitalWrite(9, HIGH); // Not recommended /* Right way: Use motor driver or transistor with PWM */ pinMode(9, OUTPUT); analogWrite(9, 128); // Safe speed control
Quick Reference
| Function | Purpose | Usage Example |
|---|---|---|
| pinMode(pin, OUTPUT) | Set pin as output | pinMode(8, OUTPUT); |
| digitalWrite(pin, HIGH/LOW) | Set motor direction | digitalWrite(8, HIGH); |
| analogWrite(pin, 0-255) | Control motor speed with PWM | analogWrite(9, 128); |
| delay(milliseconds) | Pause program | delay(3000); |
Key Takeaways
Always use a motor driver or transistor to protect the Arduino from high current.
Use digitalWrite to set motor direction and analogWrite for speed control with PWM.
Connect the motor power supply ground to Arduino ground to avoid erratic motor behavior.
Add a flyback diode when using transistors to protect from voltage spikes.
Test motor control with simple code before adding complexity.