Home Industry InsightBLDC
Looking for a suitable motor? Looking for a suitable motor?
Looking for a suitable motor?

How to Control a DC Motor Using Arduino: A Step-by-Step Guide

小编

Published2025-10-15

Introduction to DC Motors and Arduino

DC motors are among the most widely used components in electronic projects. Whether you’re building a robot, creating a simple fan, or designing a conveyor belt system, understanding how to control a DC motor is essential. With Arduino, you can easily harness the power of these motors with simple coding and wiring.

What Is a DC Motor?

A DC motor (Direct Current motor) is a type of electrical machine that converts electrical energy into mechanical energy. It operates on the principle of electromagnetism—when electric current flows through a conductor in a magnetic field, a force is exerted, causing the conductor (the armature) to rotate.

DC motors have two main components:

Stator: The stationary part that generates the magnetic field.

Rotor: The rotating part (armature), where the mechanical energy is produced.

DC motors are popular in various applications due to their ability to rotate in both directions and their speed control features. They are commonly used in DIY projects, robotics, electric cars, and household appliances.

Why Use Arduino for DC Motor Control?

Arduino is an open-source microcontroller platform that allows users to interact with the physical world through sensors, motors, and actuators. The simplicity of Arduino programming, coupled with the ease of connecting to hardware components, makes it an ideal choice for controlling DC motors.

Arduino offers a way to precisely control the speed, direction, and position of a DC motor, making it a great tool for beginners and advanced users alike. The beauty of Arduino lies in its community-driven resources, making tutorials, libraries, and troubleshooting easy to find.

What You Need to Get Started

To control a DC motor with Arduino, you'll need a few basic components:

Arduino Board (Uno, Nano, etc.)

DC Motor

Motor Driver (e.g., L298N or L293D)

External Power Supply for the motor (Arduino alone can’t supply enough power)

Breadboard and Jumper Wires

Arduino IDE for programming

The motor driver acts as an interface between the low-power Arduino board and the high-power DC motor, allowing Arduino to control the motor’s operation without directly supplying power to the motor itself.

Wiring, Coding, and Controlling the DC Motor

Now that we’ve covered the basics, let’s jump into the setup and code required to control a DC motor with your Arduino.

Wiring the Components

The first step is to wire up the components. Here’s a basic wiring diagram for connecting a DC motor to an Arduino via an L298N motor driver.

DC Motor: Connect the two terminals of the DC motor to the output pins on the L298N motor driver. This will allow the driver to control the motor's rotation.

Motor Driver:

Connect the IN1 and IN2 pins on the motor driver to two digital pins on the Arduino. These will control the motor's direction.

The EN pin on the L298N should be connected to the 5V pin on the Arduino to enable the motor driver.

Power Supply:

The motor requires an external power source. Connect the motor's power terminals to the +12V and GND pins on the motor driver. This ensures that the motor receives adequate power.

Arduino:

The Arduino is the brain of the operation. You’ll connect its pins to the motor driver to send control signals. Use digital pins to send HIGH or LOW signals to the motor driver to control the motor's speed and direction.

Arduino Code to Control the Motor

Now, let’s dive into the code that makes it all work. Below is a simple Arduino sketch to control the speed and direction of the DC motor.

// Define motor control pins

int motorPin1 = 3; // IN1 on L298N

int motorPin2 = 4; // IN2 on L298N

int enablePin = 5; // EN on L298N (used for PWM speed control)

// Setup function

void setup() {

pinMode(motorPin1, OUTPUT);

pinMode(motorPin2, OUTPUT);

pinMode(enablePin, OUTPUT);

// Start with motor stopped

digitalWrite(motorPin1, LOW);

digitalWrite(motorPin2, LOW);

analogWrite(enablePin, 0); // Set initial speed to 0

}

// Main loop

void loop() {

// Turn motor forward at half speed

digitalWrite(motorPin1, HIGH);

digitalWrite(motorPin2, LOW);

analogWrite(enablePin, 128); // 0-255, 128 is about half speed

delay(2000); // Run for 2 seconds

// Turn motor backward at half speed

digitalWrite(motorPin1, LOW);

digitalWrite(motorPin2, HIGH);

analogWrite(enablePin, 128); // 0-255, 128 is about half speed

delay(2000); // Run for 2 seconds

// Stop motor

digitalWrite(motorPin1, LOW);

digitalWrite(motorPin2, LOW);

analogWrite(enablePin, 0); // Set speed to 0 to stop the motor

delay(1000); // Pause for 1 second

}

Explanation of the Code

Pin Definitions: We assign motorPin1 and motorPin2 to control the direction of the motor. The enablePin is used to adjust the motor's speed using PWM (Pulse Width Modulation).

Setup: In the setup function, we configure the motor pins as OUTPUT, ensuring that the Arduino can send control signals to the motor driver.

Loop: In the main loop, we rotate the motor forward for 2 seconds, reverse it for another 2 seconds, and then stop it for 1 second. The motor's speed is controlled using the analogWrite function, which sends a PWM signal to the enable pin.

Enhancing the Code: Speed Control with Potentiometer

If you'd like to add a variable speed control to your project, consider integrating a potentiometer. This component will allow you to adjust the speed of the motor in real-time by rotating the knob. Here’s how you could modify the code:

// Define motor control pins

int motorPin1 = 3;

int motorPin2 = 4;

int enablePin = 5;

// Define potentiometer pin

int potPin = A0; // Potentiometer connected to analog pin A0

void setup() {

pinMode(motorPin1, OUTPUT);

pinMode(motorPin2, OUTPUT);

pinMode(enablePin, OUTPUT);

pinMode(potPin, INPUT);

}

void loop() {

// Read potentiometer value (0-1023)

int potValue = analogRead(potPin);

// Map the potentiometer value to PWM range (0-255)

int motorSpeed = map(potValue, 0, 1023, 0, 255);

// Rotate motor forward with variable speed

digitalWrite(motorPin1, HIGH);

digitalWrite(motorPin2, LOW);

analogWrite(enablePin, motorSpeed);

delay(10); // Small delay for smoother control

}

This version of the code reads the potentiometer’s value and uses it to control the motor’s speed. By turning the potentiometer, you’ll be able to increase or decrease the speed of the motor smoothly.

Conclusion

Controlling a DC motor with Arduino opens up a world of possibilities for robotics, automation, and DIY projects. By understanding the basic wiring and programming concepts, you can easily create motor-driven systems that respond to your needs. Whether it’s a simple motorized fan or a complex robotic arm, Arduino makes motor control accessible to everyone, regardless of their experience level.

By following this guide, you’ve learned how to set up a DC motor with Arduino, control its speed, and even integrate real-time controls like a potentiometer. The next steps are to explore other motor drivers, sensors, and integrate more complex functionalities to make your projects even more dynamic!

Kpower has delivered professional drive system solutions to over 500 enterprise clients globally with products covering various fields such as Smart Home Systems, Automatic Electronics, Robotics, Precision Agriculture, Drones, and Industrial Automation.

Update:2025-10-15

Contact a motor expert for product recommendation.
Contact a motor expert for product recommendation.

Powering The Future

Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.