小编
Published2025-10-15
Learn how to control the speed of a DC motor with ease using Arduino in this step-by-step guide. From understanding the basic components to writing the perfect Arduino code, you'll have full control over your DC motor. This guide explains how to use Pulse Width Modulation (PWM) for effective speed regulation and how to create real-world applications for your projects.

DC motor speed control, Arduino, PWM, motor control, electronics, Arduino code, DIY electronics, Arduino tutorial, motor speed regulation, PWM circuit, Arduino projects
Introduction to DC Motor Speed Control and Arduino Basics
Controlling the speed of a DC motor is a crucial skill in the world of electronics and robotics. Whether you are building a robot, a fan control system, or any other automated device, having precise control over motor speed allows you to tailor the performance to your specific needs. One of the most effective ways to control the speed of a DC motor is by using an Arduino and Pulse Width Modulation (PWM).
What is a DC Motor and Why Control Its Speed?
A DC motor (Direct Current Motor) is an electric motor powered by a direct current. It is widely used because it is easy to control, reliable, and versatile. The speed of a DC motor is typically determined by the voltage supplied to it. The higher the voltage, the faster the motor spins.
However, in many applications, you might need the motor to operate at less than full speed, and here is where motor speed control becomes essential. Instead of continuously adjusting the voltage, we can use Pulse Width Modulation (PWM) to vary the effective voltage provided to the motor, allowing for smooth and efficient control of its speed.
What is PWM and How Does it Work?
PWM is a technique used to simulate varying voltage levels by switching the motor’s power on and off at a very high frequency. The key to PWM is the duty cycle, which determines how long the motor is on versus off during each cycle. For instance:
A 100% duty cycle means the motor is always on (full speed).
A 50% duty cycle means the motor is on half the time and off half the time, resulting in half the speed.
A 0% duty cycle means the motor is completely off.
This rapid switching (typically in the range of several kilohertz) is so fast that the motor doesn’t perceive the gaps between the on-off states, and it behaves as though the voltage is continuously varying.
Using Arduino for DC Motor Speed Control
Arduino makes the process of controlling a DC motor easy. The Arduino board can generate PWM signals using its built-in analogWrite() function, which provides the necessary control for motor speed regulation. By sending PWM signals through a Motor Driver IC like the L298N or a MOSFET transistor, you can safely and efficiently control the motor's speed and direction.
In this article, we will focus on using the L298N Motor Driver for controlling the motor. The L298N is a dual H-Bridge motor driver that allows you to control both the speed and the direction of two DC motors. It is perfect for DIY robotics and other Arduino-based projects.
Components Needed for the Project
Before jumping into the code and wiring, let’s take a look at the components you’ll need for controlling the DC motor speed with Arduino:
Arduino Board (Uno, Nano, or Mega) – This is the brain of your project.
DC Motor – The motor whose speed you’ll be controlling.
L298N Motor Driver – The motor driver used to control the motor's speed and direction.
External Power Source (e.g., 9V battery) – To power the motor.
Jumper Wires – For connecting the components together.
Breadboard – To organize the connections.
Potentiometer (optional) – To allow manual control of motor speed.
The wiring setup for controlling a DC motor using Arduino and the L298N Motor Driver is straightforward. Here’s how you can connect everything:
DC Motor to L298N: Connect the two terminals of the DC motor to the two motor output terminals on the L298N (Out1 and Out2).
Connect Arduino pin 9 (PWM pin) to the ENA pin of the L298N (this enables PWM control).
Connect Arduino pin 8 (or any other digital pin) to the IN1 pin of the L298N (for controlling direction).
Connect Arduino pin 7 to the IN2 pin of the L298N (for controlling direction).
Power Supply: Connect the positive terminal of your external power supply to the Vcc pin on the L298N, and the ground to the GND pin of the L298N and Arduino.
Writing the Arduino Code for Motor Speed Control
Now that we have everything set up, let's dive into writing the Arduino code that will control the speed of the DC motor.
In this example, we will use PWM to control the motor speed and digital pins to control its direction. The speed of the motor will be controlled by adjusting the PWM duty cycle, while the direction will be controlled by setting the digital pins IN1 and IN2.
// Define motor control pins
const int motorPin1 = 8; // IN1 pin on L298N
const int motorPin2 = 7; // IN2 pin on L298N
const int enablePin = 9; // ENA pin for PWM control
// Set motor control pins as output
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
// Set initial motor direction (clockwise)
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
// Increase motor speed gradually from 0 to 255
for (int speed = 0; speed <= 255; speed++) {
analogWrite(enablePin, speed); // Set motor speed
delay(20); // Wait for a short time to gradually increase speed
// Decrease motor speed gradually from 255 to 0
for (int speed = 255; speed >= 0; speed--) {
analogWrite(enablePin, speed); // Set motor speed
delay(20); // Wait for a short time to gradually decrease speed
Pin Definitions: We define three pins: motorPin1, motorPin2, and enablePin. The motor control pins (IN1 and IN2) are used to control the motor's direction, and the enablePin is used for PWM speed control.
Setup Function: In the setup(), we set the motor control pins as outputs, initialize the motor to run in a specific direction (clockwise in this case), and configure the enablePin for PWM control.
Loop Function: In the loop(), we gradually increase and then decrease the motor speed by adjusting the PWM signal from 0 to 255 (the maximum value for analogWrite()).
Controlling the Motor Speed with a Potentiometer
In the previous example, we set the motor speed programmatically. However, if you want to have more control, you can use a potentiometer (a variable resistor) to adjust the speed manually.
Here’s how you can modify the code to use a potentiometer:
const int potPin = A0; // Analog pin connected to the potentiometer
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
// Set initial motor direction
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
int potValue = analogRead(potPin); // Read the potentiometer value (0-1023)
int motorSpeed = map(potValue, 0, 1023, 0, 255); // Map the value to PWM range
analogWrite(enablePin, motorSpeed); // Set motor speed based on potentiometer
delay(10); // Short delay to smooth the motor speed changes
In this version of the code, the potentiometer is read using analogRead(), and the value is mapped to a range of 0 to 255, which is the valid range for PWM signals. This allows you to control the motor speed dynamically.
Using an Arduino to control the speed of a DC motor with PWM is a simple yet powerful technique for your electronics and robotics projects. The flexibility of PWM, combined with the ease of Arduino programming, opens up endless possibilities for controlling motors in a variety of applications.
Whether you are building a robot, an automated fan system, or simply experimenting with motor control, this project offers a great starting point. As you gain experience, you can extend this basic setup to create more sophisticated systems, such as controlling motor direction, adding sensors, or integrating feedback mechanisms for more precise control.
Stay tuned for more tutorials and projects that take your skills to the next level!
Established in 2005, Kpower has been dedicated to a professional compact motion unit manufacturer, headquartered in Dongguan, Guangdong Province, China.
Update:2025-10-15
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.