小编
Published2025-10-15
Discover how to efficiently control motors using Arduino and PWM (Pulse Width Modulation) signals, paired with a potentiometer for fine-tuning the motor’s speed. This article walks you through the setup, the basics of PWM, and how to achieve smooth motor control in projects ranging from robotics to home automation.

Arduino, PWM motor control, potentiometer, motor control, electronics project, Arduino motor control, potentiometer circuit, pulse width modulation, robotics, DIY electronics
Getting Started with Arduino PWM Motor Control
In the world of electronics and robotics, controlling motors with precision is crucial for a wide range of applications, from basic hobby projects to complex industrial machinery. One of the most effective ways to control a motor's speed is through Pulse Width Modulation (PWM). Coupled with a potentiometer, this method allows users to fine-tune motor behavior in a way that is both simple and highly effective. This article will guide you through how to implement PWM motor control on an Arduino board using a potentiometer, ensuring that your projects run smoothly and precisely.
What is PWM and Why is it Used for Motor Control?
PWM stands for Pulse Width Modulation, a technique used to control the amount of power delivered to an electronic device, such as a motor. By rapidly switching the motor's power supply on and off at a high frequency, the motor receives an average voltage that is less than the supply voltage. The amount of time the signal is “on” compared to “off” is known as the duty cycle. The higher the duty cycle, the more power the motor receives, which translates to higher speed.
PWM is popular in motor control because it allows for efficient control of motor speed without the need for complex analog components. It’s a great way to reduce energy consumption while maintaining full control over the motor’s performance.
What You’ll Need for the Project
Before jumping into the wiring and coding, it’s important to gather all the necessary components. Here's what you’ll need to control a motor with Arduino using PWM:
Arduino board (e.g., Arduino Uno)
Motor driver (e.g., L298N, L293D) to safely control the motor
Potentiometer (10kΩ recommended)
Breadboard and jumper wires
Power supply for the motor
External power source for Arduino (optional)
With these components ready, you'll have everything you need to create a simple but powerful motor control system.
Wiring the circuit correctly is crucial to ensure everything works as expected. Start by connecting the potentiometer to the Arduino. The potentiometer has three pins: one for voltage (VCC), one for ground (GND), and one for the output signal (Vout).
Connect the potentiometer's VCC pin to the 5V pin on the Arduino.
Connect the GND pin of the potentiometer to the ground (GND) on the Arduino.
The Vout pin goes to an analog input pin on the Arduino (let’s say A0).
Next, you need to connect the motor to the motor driver. The motor driver acts as an intermediary, allowing you to safely control the power to the motor. Here’s how to wire it:
Connect the motor’s terminals to the motor output pins on the motor driver (OUT1 and OUT2 for most drivers).
Connect the motor driver’s IN1 and IN2 pins to two PWM-capable pins on the Arduino (pins 9 and 10, for example).
Don’t forget to connect the motor driver’s ground pin to the Arduino’s ground pin.
Finally, provide the motor driver with an external power supply that matches your motor’s voltage requirement.
Now your circuit is ready to be programmed.
Writing the Code for PWM Motor Control
With the hardware in place, it’s time to write the Arduino code to control the motor speed using the potentiometer. The potentiometer will act as a variable resistor, sending an analog value to the Arduino. This value will then be used to adjust the PWM signal sent to the motor, effectively controlling its speed.
Here's a basic example of the code:
// Define the motor control pins
// Define the potentiometer input pin
// Set motor control pins as output
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
// Read the potentiometer value (0 to 1023)
int potValue = analogRead(potPin);
// Map the potentiometer value to a PWM range (0 to 255)
int motorSpeed = map(potValue, 0, 1023, 0, 255);
// Set the motor speed using PWM
analogWrite(motorPin1, motorSpeed); // Motor speed control
analogWrite(motorPin2, 0); // Ensure the motor runs in the correct direction
delay(10); // Small delay for stability
analogRead(potPin): This reads the value from the potentiometer. The value is between 0 and 1023, representing the full range of the potentiometer's turn.
map(potValue, 0, 1023, 0, 255): This line converts the potentiometer value to a PWM range between 0 and 255, which is suitable for controlling the motor’s speed.
analogWrite(motorPin1, motorSpeed): This line generates a PWM signal on motorPin1 to control the motor’s speed.
delay(10): This small delay helps in stabilizing the motor’s behavior and prevents abrupt changes.
Now that you’ve set up the basic motor control with Arduino, let’s delve into fine-tuning the system, adding more functionality, and enhancing the project in Part 2.
Enhancing the Motor Control System and Adding Features
While the basic motor control system with PWM and a potentiometer is a fantastic start, there are several ways to enhance and expand the project to make it even more powerful. This section will introduce new features such as reversing the motor direction, implementing smooth acceleration and deceleration, and adding a safety mechanism for your motor system.
Reversing the Motor Direction
One of the common features in motor control systems is the ability to reverse the direction of the motor. Using a motor driver like the L298N or L293D makes this task straightforward. The motor driver has two input pins that control the direction of the motor (IN1 and IN2). By switching these inputs, you can easily reverse the motor’s rotation.
Here’s how you can modify the code to include a direction control feature using the potentiometer:
// Define the motor control pins
int dirPin = A1; // Analog pin for direction control
// Define the potentiometer input pin
// Set motor control pins as output
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(dirPin, INPUT); // Set direction pin as input
// Read the potentiometer value (0 to 1023)
int potValue = analogRead(potPin);
// Map the potentiometer value to a PWM range (0 to 255)
int motorSpeed = map(potValue, 0, 1023, 0, 255);
// Read direction control (0 or 1)
int dirValue = analogRead(dirPin);
// Motor runs forward
analogWrite(motorPin1, motorSpeed);
analogWrite(motorPin2, 0);
// Motor runs backward
analogWrite(motorPin1, 0);
analogWrite(motorPin2, motorSpeed);
delay(10); // Small delay for stability
In this updated code, we introduce a new pin, dirPin, to control the motor’s direction. The potentiometer controls speed, while the dirPin changes the direction.
Smooth Acceleration and Deceleration
In some applications, abrupt changes in speed can lead to mechanical stress or an undesirable user experience. To make the motor speed change smoothly, you can implement acceleration and deceleration ramps. Instead of instantly changing the motor speed, you can gradually increase or decrease the PWM value over time.
Here’s how to modify the code to introduce smooth acceleration and deceleration:
// Define motor control pins and potentiometer pin
// Variables for smooth acceleration
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
// Read potentiometer value
targetSpeed = analogRead(potPin) / 4; // Map to 0-255 range
// Smoothly accelerate or decelerate the motor speed
if (motorSpeed < targetSpeed) {
} else if (motorSpeed > targetSpeed) {
analogWrite(motorPin1, motorSpeed);
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.