小编
Published2025-10-15
This article provides a comprehensive guide to controlling the speed of a DC motor using an Arduino Uno, offering a step-by-step approach for beginners and advanced enthusiasts alike. Learn how to harness the power of PWM (Pulse Width Modulation) to regulate motor speed effectively and explore the key components and techniques required for a seamless project.

DC motor, Arduino Uno, speed control, PWM, motor driver, electronic projects, DIY, Pulse Width Modulation, Arduino motor control, motor driver circuit, Arduino projects.
Introduction to DC Motor Speed Control Using Arduino Uno
In the world of electronics, controlling the speed of a DC motor is a fundamental skill that opens up doors to a wide range of projects. Whether you’re building a robot, a fan, or a conveyor system, precise motor speed control is essential to ensure smooth operation. Arduino Uno, a popular open-source microcontroller, provides an easy and efficient way to control the speed of a DC motor. In this guide, we’ll walk through the basics of speed control, delve into the technicalities of using PWM (Pulse Width Modulation) with Arduino Uno, and learn how to build a circuit to control the speed of your motor effectively.
A DC motor, or Direct Current motor, is a type of electric motor that runs on direct current electricity. These motors have two main parts: a stator (the stationary part) and a rotor (the rotating part). When current flows through the rotor, it creates a magnetic field, causing the rotor to spin and produce mechanical motion. This rotational motion is what drives many applications in everyday life, from small gadgets to industrial machinery.
Unlike AC (Alternating Current) motors, DC motors allow for easy control of speed and direction. The ability to vary the voltage supplied to the motor directly affects its speed. However, this process of controlling voltage can be complex, and that's where Arduino Uno comes in.
Understanding PWM (Pulse Width Modulation)
PWM (Pulse Width Modulation) is a technique used to control the power delivered to electrical devices, such as motors. Instead of directly varying the voltage supplied to the motor, PWM controls the voltage by rapidly switching the power on and off. The speed of the motor depends on the proportion of time the power is "on" versus "off," known as the duty cycle.
The duty cycle is expressed as a percentage:
A 0% duty cycle means the motor is off.
A 100% duty cycle means the motor is running at full speed.
Anything in between adjusts the motor speed proportionally.
By using PWM signals, Arduino can easily control the speed of a motor without the need for bulky and expensive voltage regulators.
The Role of the Motor Driver
A motor driver is an essential component when interfacing a DC motor with Arduino. The motor driver acts as an intermediary between the Arduino and the motor, allowing the microcontroller to control the motor without directly powering it.
The motor driver receives PWM signals from the Arduino and uses them to control the motor’s power. It also protects the Arduino from excessive current draw, ensuring that the microcontroller remains safe while driving the motor. A commonly used motor driver for this purpose is the L298N dual H-Bridge motor driver, which can control both the speed and direction of two motors simultaneously.
Components Needed for the Project
Before we dive into the circuit and code, here’s a quick list of the components you’ll need to set up the speed control system:
Arduino Uno – This will act as the brain of your project, generating the PWM signals for motor control.
DC Motor – A simple 12V DC motor is sufficient for this project.
Motor Driver (L298N) – A versatile motor driver to control the motor’s speed and direction.
Power Supply – A 12V DC adapter to power the motor.
Potentiometer – A variable resistor used to adjust the PWM duty cycle, thus controlling the speed.
Jumper Wires – For making all necessary connections.
Breadboard – For easy circuit assembly.
Arduino IDE – The software used to write and upload the code to the Arduino Uno.
Before starting the coding part, let's connect the components correctly. Here is how you can wire up the circuit:
DC Motor to L298N Driver:
Connect the motor terminals to the output pins of the L298N motor driver (pins 3 and 6).
Connect the IN1 and IN2 pins of the L298N to two PWM-capable pins on the Arduino (let’s use pins 9 and 10).
Connect the ENA pin to the 5V pin on the Arduino to enable the motor driver.
Potentiometer to Arduino:
Connect the middle pin of the potentiometer to the analog input pin A0 on the Arduino.
Connect the other two pins of the potentiometer to the 5V and GND pins on the Arduino.
Connect the 12V power supply to the motor driver’s 12V pin and the GND pin to the common ground.
With the circuit ready, let’s move on to the code.
Arduino Code for DC Motor Speed Control
Now that the circuit is assembled, the next step is to write the code to control the motor's speed using the potentiometer. The Arduino will read the potentiometer's value, convert it into a PWM signal, and send it to the motor driver to adjust the motor’s speed accordingly.
int motorPin1 = 9; // IN1 on L298N
int motorPin2 = 10; // IN2 on L298N
int potPin = A0; // Potentiometer connected to A0
// Initialize motor control pins
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
// Read potentiometer value (0 to 1023)
int potValue = analogRead(potPin);
// Map potentiometer value to PWM range (0 to 255)
int motorSpeed = map(potValue, 0, 1023, 0, 255);
// Control motor speed using PWM
analogWrite(motorPin1, motorSpeed); // Set speed on IN1
analogWrite(motorPin2, 0); // Motor rotates in one direction
delay(10); // Small delay to stabilize the motor speed
In the setup() function, we define two pins (motorPin1 and motorPin2) as outputs, which will control the direction of the motor via the L298N driver. The potentiometer is connected to analog pin A0.
Reading the Potentiometer Value:
In the loop(), the analogRead() function reads the value from the potentiometer. The value ranges from 0 to 1023, depending on how far you rotate the potentiometer.
Mapping the Potentiometer Value to PWM Range:
The map() function converts the potentiometer value into a range suitable for PWM control. It maps the range from 0 to 1023 to a range from 0 to 255, which is the valid range for analogWrite() to control PWM.
Adjusting the Motor Speed:
The analogWrite() function is used to send the PWM signal to the motor driver. The motorPin1 controls the speed, while motorPin2 is set to 0, meaning the motor will rotate in a single direction.
Once you’ve uploaded the code to your Arduino Uno and connected the components, turning the potentiometer will change the speed of the motor. The motor’s speed will increase as you turn the potentiometer clockwise and decrease as you turn it counterclockwise.
Enhancements and Applications
This basic setup can be expanded with additional features and functionality:
By using both IN1 and IN2 pins, you can make the motor rotate in both directions, allowing you to create forward and reverse motion for applications like robotics.
For more precise speed control, you can add sensors (e.g., encoders) to measure the actual speed of the motor and feed the data back to the Arduino to adjust the PWM dynamically.
You could implement speed profiles to control the motor's behavior in different phases of a project, such as speeding up gradually or slowing down smoothly when stopping.
Controlling the speed of a DC motor with an Arduino Uno is a straightforward yet powerful technique. By combining PWM with an Arduino and a motor driver like the L298N, you can easily regulate the motor speed for a variety of applications. Whether you're a beginner or an advanced user, this project provides an excellent foundation for exploring more complex motor control systems. Happy tinkering!
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 Kpower's product specialist to recommend suitable motor or gearbox for your product.