Introduction
This tutorial explores PWM (Pulse-Width Modulation) speed control of a DC motor using an Arduino, a powerful technique for achieving precise motor performance. PWM allows you to vary the average power delivered to a DC motor by rapidly switching it on and off, effectively controlling its speed without wasting energy. In this experiment, you’ll learn how to connect a DC motor to an Arduino with an H-Bridge, program PWM signals to adjust speed, and test the setup. This guide is ideal for hobbyists and engineers looking to enhance their motor control skills.
Unlike simple on/off control, PWM provides smooth speed transitions, making it perfect for applications like robotic wheels or variable-speed fans. By the end of this tutorial, you’ll have a functional setup to experiment with PWM-driven DC motor speed control.
Components Required
To perform PWM speed control of a DC motor, you’ll need the following components:
- Microcontroller (e.g., Arduino Uno): Generates PWM signals to control motor speed.
- DC Motor (e.g., 6V-12V): A small DC motor suitable for variable speed applications.
- H-Bridge Module (e.g., L298N): Enables PWM control and direction switching for the motor.
- Power Supply (e.g., 9V battery or 12V adapter): Powers the motor, separate from the Arduino’s 5V.
- Connecting Wires: Jumper wires for secure connections.
- Breadboard: For prototyping the circuit.
- Optional: Potentiometer (e.g., 10kΩ): Allows manual speed adjustment during testing.
Schematic
The schematic illustrates how to wire the DC motor to the Arduino using an H-Bridge for PWM control:
Basic connection overview: - Connect the DC motor’s two terminals to the H-Bridge output pins (e.g., OUT1 and OUT2 on L298N). - Wire the H-Bridge input pins (IN1 and IN2) to Arduino PWM pins (e.g., D9 and D10). - Attach the power supply (e.g., 9V) to the H-Bridge’s VCC and GND, ensuring a common ground with the Arduino. - If using a potentiometer, connect its wiper to an analog pin (e.g., A0), with the other pins to 5V and GND.
Note: Use PWM-capable pins (e.g., 3, 5, 6, 9, 10, 11 on Arduino Uno) for smooth speed control. Ensure the power supply matches the motor’s voltage and current needs.
Steps
Follow these steps to build and test your PWM speed control setup:
- Assemble the Circuit: Connect the DC motor to the H-Bridge outputs (OUT1, OUT2) and wire IN1 and IN2 to Arduino D9 and D10.
- Power Connections: Attach a 9V power supply to the H-Bridge VCC and GND, and power the Arduino via USB, sharing a common GND.
- Upload the Code: Load the Arduino sketch (see "Code Example" below) to apply PWM to the motor.
- Test Speed Variation: Run the code to cycle through different speeds, observing the motor’s response.
- Manual Control (Optional): Add a potentiometer to A0 and modify the code to adjust speed dynamically based on its input.
- Troubleshoot (if needed): Check connections and PWM values if the motor doesn’t respond smoothly.
Code Example
Below is an Arduino sketch demonstrating PWM speed control of a DC motor using an H-Bridge:
// Define H-Bridge pins
const int IN1 = 9; // PWM-capable pin for forward
const int IN2 = 10; // PWM-capable pin for reverse
void setup() {
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
Serial.begin(9600); // For debugging
}
void setMotorSpeed(int speed) { // Speed: 0-255
Serial.print("Speed: ");
Serial.println(speed);
analogWrite(IN1, speed); // Forward direction
analogWrite(IN2, 0); // No reverse
}
void stopMotor() {
Serial.println("Motor stopped");
analogWrite(IN1, 0);
analogWrite(IN2, 0);
}
void loop() {
setMotorSpeed(64); // 25% speed
delay(2000); // Run for 2 seconds
setMotorSpeed(128); // 50% speed
delay(2000);
setMotorSpeed(255); // Full speed
delay(2000);
stopMotor();
delay(2000);
}
This code ramps the motor through 25%, 50%, and 100% speeds using PWM, then stops, repeating every cycle. For dynamic control, replace fixed speeds with `map(analogRead(A0), 0, 1023, 0, 255)` using a potentiometer.
Applications
PWM speed control of a DC motor is useful in various real-world scenarios, including:
- Automation Systems: Adjusts fan or pump speeds in HVAC or fluid systems.
- Prototyping for Robotics: Controls wheel speeds for smooth robotic movement.
- Educational Experiments: Demonstrates PWM principles and motor dynamics.
- Hobby Projects: Powers variable-speed model vehicles or drones.
- Home Automation: Regulates motorized blinds or small appliances.