Controlling the speed of a DC motor is a fundamental concept in electronics and robotics. With the help of an Arduino, you can control the speed and direction of a DC motor using a PWM (Pulse Width Modulation) signal. This project is ideal for beginners looking to learn about motor control, analog input reading, and interfacing with components like potentiometers and motor drivers.
π§° Components RequiredArduino Uno (or any compatible board) β 1
- Arduino Uno (or any compatible board) β 1
DC motor β 1
- DC motor β 1
L298N or L293D motor driver module β 1
- L298N or L293D motor driver module β 1
10kΞ© Potentiometer β 1
- 10kΞ© Potentiometer β 1
External power supply (e.g. 9V battery or 12V adapter) β 1
- External power supply (e.g. 9V battery or 12V adapter) β 1
Breadboard β 1
- Breadboard β 1
Jumper wires β several
- Jumper wires β several
USB cable β 1 (for programming Arduino)
- USB cable β 1 (for programming Arduino)
The Arduino generates a PWM signal, which is sent to the motor driver. PWM is a technique where the average voltage sent to a device is varied by rapidly turning the signal on and off. The duty cycle of the PWM signal determines how fast the motor runs.
A motor driver module like the L298N is used as an interface between the Arduino and the DC motor. Since the Arduino cannot supply enough current to drive the motor directly, the driver takes the low-current signal from the Arduino and uses it to control the higher-current motor supply.
The potentiometer acts as a manual speed controller. By rotating the knob, it sends an analog value (0β1023) to the Arduino, which is then mapped to a PWM value (0β255) and used to control motor speed.
π Circuit ConnectionsPotentiometer:
One outer pin to 5V
- One outer pin to 5V
The other outer pin to GND
- The other outer pin to GND
Middle (wiper) pin to A0 (analog input on Arduino)
- Middle (wiper) pin to A0 (analog input on Arduino)
- Potentiometer:One outer pin to 5VThe other outer pin to GNDMiddle (wiper) pin to A0 (analog input on Arduino)
L298N Motor Driver:
IN1 to D5 (digital pin)
- IN1 to D5 (digital pin)
IN2 to D6 (digital pin)
- IN2 to D6 (digital pin)
ENA (Enable A) to D9 (PWM pin)
- ENA (Enable A) to D9 (PWM pin)
Motor terminals connected to OUT1 and OUT2
- Motor terminals connected to OUT1 and OUT2
12V supply to VCC
- 12V supply to VCC
GND shared with Arduino
- GND shared with Arduino
- L298N Motor Driver:IN1 to D5 (digital pin)IN2 to D6 (digital pin)ENA (Enable A) to D9 (PWM pin)Motor terminals connected to OUT1 and OUT212V supply to VCCGND shared with Arduino
cpp
CopyEdit
int motorPin1 = 5;
int motorPin2 = 6;
int enablePin = 9;
int potPin = A0;
int potValue = 0;
int motorSpeed = 0;
void setup() {
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
Serial.begin(9600);
}
void loop() {
potValue = analogRead(potPin);
motorSpeed = map(potValue, 0, 1023, 0, 255);
analogWrite(enablePin, motorSpeed); // Speed control
digitalWrite(motorPin1, HIGH); // Direction control
digitalWrite(motorPin2, LOW);
Serial.print("Pot Value: ");
Serial.print(potValue);
Serial.print(" | Speed: ");
Serial.println(motorSpeed);
delay(100);
}
cpp
CopyEdit
int motorPin1 = 5;
int motorPin2 = 6;
int enablePin = 9;
int potPin = A0;
int potValue = 0;
int motorSpeed = 0;
void setup() {
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
Serial.begin(9600);
}
void loop() {
potValue = analogRead(potPin);
motorSpeed = map(potValue, 0, 1023, 0, 255);
analogWrite(enablePin, motorSpeed); // Speed control
digitalWrite(motorPin1, HIGH); // Direction control
digitalWrite(motorPin2, LOW);
Serial.print("Pot Value: ");
Serial.print(potValue);
Serial.print(" | Speed: ");
Serial.println(motorSpeed);
delay(100);
}
β
How It WorksAs you rotate the potentiometer, its analog value changes.
- As you rotate the potentiometer, its analog value changes.
The Arduino reads this value and maps it to a PWM range.
- The Arduino reads this value and maps it to a PWM range.
The PWM signal is sent to the ENA pin of the motor driver.
- The PWM signal is sent to the ENA pin of the motor driver.
This adjusts the speed of the motor.
- This adjusts the speed of the motor.
IN1 and IN2 set the direction (HIGH and LOW = forward).
- IN1 and IN2 set the direction (HIGH and LOW = forward).
You can reverse the motor by switching IN1 to LOW and IN2 to HIGH.
π ApplicationsRobotic vehicles and cars
- Robotic vehicles and cars
Conveyor belt systems
- Conveyor belt systems
Fan speed controllers
- Fan speed controllers
Smart home automation (motorized windows/curtains)
- Smart home automation (motorized windows/curtains)
Always power the motor with an external supply.
- Always power the motor with an external supply.
Use a diode or capacitor for noise suppression if needed.
- Use a diode or capacitor for noise suppression if needed.
Ensure the driver can handle your motorβs current requirements.
- Ensure the driver can handle your motorβs current requirements.
This simple Arduino project teaches the basics of DC motor control using PWM and a potentiometer. Itβs perfect for beginners and can be expanded by adding features like direction buttons, RPM feedback, or wireless control.
Comments