DC Motor Control with Arduino: A Comprehensive Guide

DC motor controlOptimizing Code for DC Motor PerformanceOptimizing Code for DC Motor PerformanceUnlock expert strategies to optimize your Arduino DC motor code with advanced PWM, precise interrupts, and non-blocking design for superior performance. is essential for robotics and automation, but Arduino’s limited current output (40mA per pin) requires external components. This guide combines practical wiring with circuit theory to safely drive motors using transistors, with code examples and advanced project variations.

Table of Contents🔗

Components Needed🔗

ComponentPurposeExample Models
Arduino BoardControl logicUno R3, Nano, Mega
DC MotorMechanical motion5-12V hobby motor
TransistorCurrent amplificationTIP120 (Darlington), IRF520 (MOSFET)
Flyback DiodeBack EMF protection1N4001, 1N4007
ResistorBase current limiting1kΩ–2.2kΩ (0.25W)
Power SupplyMotor power source9V battery, 12V adapter
BreadboardPrototypingStandard 400-point

Circuit Theory🔗

Why Use a Transistor?

ArduinoWhat is Arduino? A Comprehensive OverviewWhat is Arduino? A Comprehensive OverviewDive into the world of Arduino with our in-depth guide covering hardware, software, and community projects ideal for students, hobbyists, and educators. pins can’t supply the 200mA–2A required by DC motors. A transistor acts as a switch:

Key Equations:

1. Base ResistorYour First Hands-On Arduino ProjectYour First Hands-On Arduino ProjectEmbark on your Arduino journey with our step-by-step guide. Learn to build a simple circuit, write your first code, and troubleshoot your project easily.:

\( R = \frac{V_{Arduino} - V_{BE}}{I_{Base}} \)

For TIP120 (\( h_{FE} ≈ 1000 \)):

\( R = \frac{5V - 1.2V}{0.5A / 1000} = 7.6kΩ → 1kΩ \) (practical value).

2. Motor Current:

\( I_{Motor} = h_{FE} \times I_{Base} \).

Darlington vs. MOSFET

ParameterTIP1202N2222IRF520 (MOSFET)
Max Current5A0.8A9.7A
Voltage60V40V100V
Use CaseMedium loadsSmall motorsHigh-efficiency

Wiring Diagram🔗

TIP120 Configuration

Arduino Pin 9 → 1kΩ Resistor → TIP120 Base
TIP120 Collector → Motor → External Power (+)
TIP120 Emitter → External Power (-)
1N4007 Diode: Reverse-biased across motor (Cathode to +).
Common Ground: Connect Arduino & external supply.

Textual Diagram:

+V (External Power)
     │
   [Motor]
     │
     ├───|>|─── (Flyback Diode)
     │           (Cathode → +V, Anode → Collector)
     │
  Collector (TIP120)
     │
se (via 1kΩ resistor) ← Arduino digital pin
     │
Emitter (TIP120)
     │
  Common Ground ← Arduino & External Power

Arduino Code🔗

Basic On/Off Control

const int motorPin = 9;
void setup() {
  pinMode(motorPin, OUTPUT);
}
void loop() {
  digitalWrite(motorPin, HIGH);  // Motor ON
  delay(2000);
  digitalWrite(motorPin, LOW);   // Motor OFF
  delay(2000);
}

PWM Speed Control

const int motorPin = 9;
void setup() {
  pinMode(motorPin, OUTPUT);
}
void loop() {
  analogWrite(motorPin, 128);  // 50% speed (PWM = 0-255)
  delay(3000);
  analogWrite(motorPin, 0);    // Stop motor
  delay(1000);
}

Safety & Troubleshooting🔗

1. Heat Management:

  • Attach a heatsink to the transistor for currents >1A.
  • Use thermal paste for better dissipation.

2. Power Isolation:

3. Noise Reduction:

  • Add a 100nF capacitor across motor terminals to reduce EMI.

4. TestingYour First Hands-On Arduino ProjectYour First Hands-On Arduino ProjectEmbark on your Arduino journey with our step-by-step guide. Learn to build a simple circuit, write your first code, and troubleshoot your project easily. Tips:

5. Failure Modes:

Advanced Applications🔗

Speed Control with Potentiometer

int motorPin = 9;
int potPin = A0;
void setup() {
  pinMode(motorPin, OUTPUT);
}
void loop() {
  int speed = analogRead(potPin) / 4;  // Scale 0-1023 → 0-255
  analogWrite(motorPin, speed);
}

Bidirectional Control (H-Bridge)

Use an L298N or TB6612FNG driverIntegrating Motor Drivers in Your CircuitIntegrating Motor Drivers in Your CircuitMaster motor control with Arduino using our detailed tutorial on motor driver integration. Get expert wiring tips, coding samples, and troubleshooting advice. for forward/reverse motion:

IN1 ← Arduino Pin 7
IN2 ← Arduino Pin 8
Enable ← PWM Pin 9

Wireless Control

Pair with HC-05Setting up Bluetooth ModulesSetting up Bluetooth ModulesDiscover a detailed guide on setting up Bluetooth modules with Arduino, covering hardware, software, pairing, and troubleshooting for seamless connectivity. Bluetooth module:

if (Serial.available()) {
  char cmd = Serial.read();
  if (cmd == 'F') analogWrite(motorPin, 255);  // Full speed
  if (cmd == 'S') analogWrite(motorPin, 0);     // Stop
}

Current Monitoring

Add a 0.1Ω shunt resistorYour First Hands-On Arduino ProjectYour First Hands-On Arduino ProjectEmbark on your Arduino journey with our step-by-step guide. Learn to build a simple circuit, write your first code, and troubleshoot your project easily. in series with the motor:

\( V_{shunt} = I_{motor} \times 0.1Ω \).

Read voltage via Arduino’s analog inputHow to Choose the Right Arduino Board for Your ProjectHow to Choose the Right Arduino Board for Your ProjectLearn how to choose the perfect Arduino board. Our guide covers key project needs, essential specs, connectivity, and power efficiency tips..

By mastering transistor-based motor control, you’ll unlock robotics, automated systems, and industrial prototypes. For high-current applications, explore MOSFETs or motor driverIntegrating Motor Drivers in Your CircuitIntegrating Motor Drivers in Your CircuitMaster motor control with Arduino using our detailed tutorial on motor driver integration. Get expert wiring tips, coding samples, and troubleshooting advice. ICs like the TB6612FNG.

Author: Marcelo V. Souza - Engenheiro de Sistemas e Entusiasta em IoT e Desenvolvimento de Software, com foco em inovação tecnológica.

References🔗

Share article

Related Articles