Motor speed control with Arduino
Controlling motor speed with Arduino is a fundamental project for enthusiasts and professionals in robotics and electronics. This tutorial explains how to control motor speed with Arduino, providing practical examples, a deep dive into Pulse Width Modulation (PWM), and an advanced implementation using PID control. By the end, you’ll have a comprehensive understanding of Arduino motor speed control with DC motors, enabling you to apply this knowledge to your projects.
Why Control Motor Speed withArduino?
Motor speed control is essential in robotics, automation, and other applications where precision is required. Using Arduino, you can easily manage the speed of DC motors using PWM signals, making it an ideal choice for beginners and professionals.
Key benefits include:
- Precision speed control.
- Integration with sensors and other electronics.
- Cost-effective and scalable solutions.
Tools and Components Required
( To buy any of the parts introduced below, just click on its name to be taken to the store )
To implement motor speed control with Arduino, gather the following:
- Arduino Board (e.g., Arduino Uno, Nano, or Mega).
- DC Motor.
- L298N Motor Driver or an N-channel MOSFET (e.g., IRFZ44N).
- Power Source (battery or DC adapter).
- Potentiometer (for manual control).
- Jump Wires and Breadboard.
- Multimeter (optional for testing).
Pulse Width Modulation (PWM) Basics
PWM is a technique where the duty cycle of a signal controls the power delivered to a device. In Arduino, PWM signals are generated using the analogWrite()
function on specific pins.
- A 0% duty cycle results in no motor movement.
- A 50% duty cycle provides half the speed.
- A 100% duty cycle delivers full speed.
Basic Motor Speed Control with Arduino
Circuit Diagram
Connect your components as shown:
- Connect the DC motor to the L298N driver (OUT1 and OUT2 pins).
- Connect the enable pin (
ENA
) of the motor driver to a PWM pin on the Arduino (e.g.,D3
). - Connect a potentiometer’s middle pin to the Arduino analog input pin (
A0
).
Code
Here’s the Arduino code for basic motor speed control:
#define POT_PIN A0 // Potentiometer pin
#define PWM_PIN 3 // PWM pin connected to motor driver ENA
void setup() {
pinMode(POT_PIN, INPUT); // Set potentiometer pin as input
pinMode(PWM_PIN, OUTPUT); // Set PWM pin as output
}
void loop() {
int potValue = analogRead(POT_PIN); // Read potentiometer value (0-1023)
int pwmValue = map(potValue, 0, 1023, 0, 255); // Map to PWM range (0-255)
analogWrite(PWM_PIN, pwmValue); // Write PWM to motor
delay(10); // Small delay for stability
}
Download the code |
Explanation
- The potentiometer adjusts the PWM signal.
- The PWM signal controls the motor driver, varying the motor speed.
Advanced: PID Control for Arduino Motor Speed
PID (Proportional-Integral-Derivative) control offers precise motor speed control by adjusting the PWM signal dynamically based on feedback. This is crucial in applications like robotics and automation.
Components
Add a rotary encoder or a tachometer to measure motor speed.
Code
Below is an example of implementing PID control for Arduino motor speed:
#define POT_PIN A0
#define PWM_PIN 3
#define ENCODER_PIN 2
volatile int encoderTicks = 0;
float targetSpeed, currentSpeed;
float kp = 1.0, ki = 0.5, kd = 0.1; // PID coefficients
float error, prevError = 0, integral = 0, derivative;
void setup() {
pinMode(POT_PIN, INPUT);
pinMode(PWM_PIN, OUTPUT);
pinMode(ENCODER_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(ENCODER_PIN), countTicks, RISING);
Serial.begin(9600);
}
void loop() {
targetSpeed = map(analogRead(POT_PIN), 0, 1023, 0, 255);
currentSpeed = calculateSpeed();
error = targetSpeed - currentSpeed;
integral += error;
derivative = error - prevError;
float output = kp * error + ki * integral + kd * derivative;
output = constrain(output, 0, 255);
analogWrite(PWM_PIN, output);
prevError = error;
delay(50);
}
void countTicks() {
encoderTicks++;
}
float calculateSpeed() {
static unsigned long lastTime = 0;
unsigned long currentTime = millis();
float speed = (encoderTicks * 1000.0) / (currentTime - lastTime);
encoderTicks = 0;
lastTime = currentTime;
return speed;
}
Download the code |
Explanation
- Encoder Feedback: Measures motor speed in real time.
- PID Algorithm: Adjusts the PWM signal dynamically based on error, integral, and derivative values.
- Fine-Tuning: Adjust
kp
,ki
, andkd
for optimal performance.
We recommend that you watch the following educational video
Conclusion
This article explained how to control motor speed with Arduino, from basic PWM techniques to advanced PID control. By understanding these methods, you can implement precise and efficient Arduino motor speed control with DC motors. Whether you’re a beginner or a seasoned developer, these techniques will empower you to create more advanced robotics projects.