Controlling the speed of a motor using an Arduino is a relatively straightforward project that can be completed for under $20. With just a few basic electronic components, you can make your own PWM motor speed controller and adjust the rotation rate of DC motors with ease.
What You'll Need
Building a DIY Arduino motor controller only requires a few parts:
-
Arduino Uno - The brains of the controller that will generate the PWM signal. Any Arduino board will work.
-
L293D Motor Driver - An integrated circuit chip that can control 2 DC motors by taking a PWM input signal.
-
DC motor - The motor you want to control the speed of. Small hobby motors are ideal.
-
Jumper wires - For making connections between components.
-
Breadboard - To neatly wire up the circuit. Not absolutely necessary but recommended.
-
9V Battery - For powering the motor.
This is the full list of required electronic components. As long as you have an Arduino and a motor driver like the L293D, you have the two main pieces for building your own variable speed motor controller.
How a Motor Speed Controller Works
The basic principle behind an Arduino based DIY motor speed controller is pulse width modulation (PWM). The Arduino can generate a PWM signal which is sent to the motor driver.
PWM is a method of digitally encoding analog signal levels. By switching the output to the motor on and off very quickly, the average voltage (and speed) can be changed.
The motor driver takes this PWM signal as an input and controls the power sent to the motor accordingly. Rapid pulses smoothly control the motor's rotation speed.
The PWM frequency remains constant. Only the duty cycle changes to vary motor speed.
Wiring up the Circuit
The wiring for the Arduino motor speed controller is very straightforward.
Follow these steps:
-
Connect the L293D motor driver module to the Arduino. The PWM pin on the module goes to a PWM capable pin on the Arduino such as pin 3, 5, 6, 9, 10, or 11.
-
Power the motor driver by connecting the 5V pin to the Arduino's 5V output. The motor driver also needs GND connected from the Arduino.
-
Connect the DC motor to the screw terminals on the motor driver module.
-
Power the motor with a separate power source such as a 9V battery. Connect positive (+) to the 5V pin and negative (-) to GND.
That completes the wiring for the hardware side. Now it's just a matter of uploading code to the Arduino to control the motor's speed.
Arduino Code for Motor Speed Control
Controlling the motor speed is simply a matter of setting the correct duty cycle on the PWM pin connected to the L293D driver.
Here is basic Arduino code to generate a PWM signal:
```cpp
int pwmPin = 3;
void setup() {
pinMode(pwmPin, OUTPUT);
}
void loop() {
analogWrite(pwmPin, 255); // Full speed
delay(2000);
analogWrite(pwmPin, 128); // Half speed
delay(2000);
}
```
This code sets pin 3 as the PWM output pin and first sets the duty cycle to 255 out of 255 for full speed. It then reduces to 128 out of 255 for half speed.
Change the 0-255 value in analogWrite() to anything in between to smoothly control the motor speed with PWM.
Controlling Speed with a Potentiometer
Instead of setting fixed speeds in code, you can add a potentiometer to your circuit to control speed by turning a dial.
Simply connect a 10k potentiometer to +5V and GND. The wiper pin goes to an analog input like A0. Then replace the analogWrite value with a reading from the pot:
```cpp
int potPin = A0; // Potentiometer on analog pin A0
int pwmPin = 3; // PWM output on digital pin 3
void setup() {
pinMode(pwmPin, OUTPUT);
}
void loop() {
int potValue = analogRead(potPin); // Read potentiometer value
analogWrite(pwmPin, potValue/4); // Write PWM value
}
```
Now when you turn the potentiometer knob, the motor speed will change in real time.
Conclusion
Building an Arduino PWM motor speed controller is an easy and inexpensive project. With just an Arduino, motor driver IC like the L293D, motor, and power supply, you can make your own variable speed controller for under $20.
The same concept can be applied to control the speed of any DC motor. PWM provides a simple method of digitally controlling an analog output level to vary motor RPM.