Bird
0
0
Arduinoprogramming~15 mins

Motor speed control with PWM in Arduino - Deep Dive

Choose your learning style9 modes available
Overview - Motor speed control with PWM
What is it?
Motor speed control with PWM means changing how fast a motor spins by turning its power on and off very quickly. PWM stands for Pulse Width Modulation, which is a way to send power in pulses instead of a steady flow. By changing the length of these pulses, we can control the motor speed smoothly. This method is common in electronics because it saves energy and reduces heat.
Why it matters
Without PWM, controlling motor speed would waste energy and cause motors to heat up quickly. Simple on/off switches only let motors run at full speed or stop, which is not useful for many projects like robots or fans. PWM allows precise speed control, making devices more efficient, longer-lasting, and quieter. It also helps save battery life in portable devices.
Where it fits
Before learning PWM motor control, you should understand basic electronics, how motors work, and simple Arduino programming. After mastering PWM, you can explore advanced motor drivers, feedback control systems, and robotics projects that require precise movement.
Mental Model
Core Idea
PWM controls motor speed by rapidly switching power on and off, adjusting the 'on' time to change how fast the motor spins.
Think of it like...
Imagine turning a faucet on and off very quickly to control how much water flows out. The longer you keep it on during each quick cycle, the more water flows, just like longer 'on' pulses make the motor spin faster.
PWM Cycle:
┌─────────────┐
│             │
│  ON  ██████ │  <-- Power ON (pulse width)
│             │
│  OFF ███    │  <-- Power OFF (rest of cycle)
└─────────────┘

Longer ON time = faster motor
Shorter ON time = slower motor
Build-Up - 7 Steps
1
FoundationUnderstanding DC Motor Basics
🤔
Concept: Learn how a DC motor works and what affects its speed.
A DC motor spins when electricity flows through it. The speed depends on the voltage and current it receives. More voltage means faster spinning. Motors have coils and magnets inside that create motion when powered.
Result
You know that changing voltage changes motor speed.
Understanding that motor speed depends on power input is key to controlling it effectively.
2
FoundationWhat is PWM and How It Works
🤔
Concept: Introduce PWM as a method to control power by switching it on and off quickly.
PWM sends power in pulses instead of a steady flow. The 'duty cycle' is the percentage of time the power is ON during each cycle. For example, 50% duty cycle means power is ON half the time and OFF half the time. This tricks the motor into running at a speed between full on and off.
Result
You understand PWM changes average power without changing voltage.
Knowing PWM controls average power helps explain why motors respond smoothly to it.
3
IntermediateUsing Arduino PWM Pins
🤔
Concept: Learn which Arduino pins support PWM and how to use analogWrite() to set duty cycle.
Arduino boards have special pins that can output PWM signals. You use the function analogWrite(pin, value) where value is from 0 (always off) to 255 (always on). This sets the duty cycle. For example, analogWrite(9, 128) sets about 50% duty cycle on pin 9.
Result
You can generate PWM signals on Arduino pins to control devices.
Knowing how to generate PWM signals on Arduino is essential for motor speed control.
4
IntermediateConnecting Motor with a Transistor Driver
🤔
Concept: Learn how to safely connect a motor to Arduino using a transistor to handle current.
Arduino pins cannot supply enough current to run a motor directly. We use a transistor as a switch controlled by Arduino's PWM pin. The transistor turns the motor on and off rapidly according to PWM. A diode protects against voltage spikes from the motor.
Result
You can control motor speed without damaging Arduino.
Understanding hardware setup prevents common damage and ensures reliable motor control.
5
IntermediateWriting Arduino Code for Motor Speed
🤔Before reading on: Do you think analogWrite(9, 200) makes the motor spin faster or slower than analogWrite(9, 100)? Commit to your answer.
Concept: Write code to change motor speed by adjusting PWM duty cycle.
Use analogWrite(pin, speed) where speed is 0-255. Higher values mean faster motor. Example: void setup() { pinMode(9, OUTPUT); } void loop() { analogWrite(9, 128); // 50% speed delay(2000); analogWrite(9, 255); // full speed delay(2000); } This code switches motor speed every 2 seconds.
Result
Motor speed changes smoothly between half and full speed.
Knowing how duty cycle maps to speed helps you control motor precisely.
6
AdvancedImproving Control with Speed Feedback
🤔Before reading on: Can PWM alone keep motor speed constant under load changes? Commit to yes or no.
Concept: Use sensors to measure motor speed and adjust PWM to keep speed steady.
Motors slow down if load increases. To keep speed constant, use a sensor like an encoder to measure speed. Then adjust PWM duty cycle in code to correct speed. This is called closed-loop control or feedback control.
Result
Motor speed stays stable even if load changes.
Understanding feedback control is key for precise and reliable motor systems.
7
ExpertPWM Frequency and Motor Noise Effects
🤔Before reading on: Does changing PWM frequency affect motor noise and efficiency? Commit to yes or no.
Concept: Explore how PWM frequency affects motor sound, heat, and smoothness.
PWM frequency is how fast the power switches on and off per second. Low frequency can cause motor humming noise and rough motion. High frequency reduces noise and makes motor run smoother but can cause more heat in driver components. Choosing the right frequency balances noise, efficiency, and hardware limits.
Result
You can optimize motor control for quiet and efficient operation.
Knowing PWM frequency effects helps design better motor control systems.
Under the Hood
PWM works by switching the motor's power supply on and off very fast, so the motor's inertia smooths out the pulses into a steady speed. The motor coil and rotor act like a low-pass filter, averaging the power over time. The Arduino's analogWrite uses timers to generate these pulses automatically at a fixed frequency, changing the pulse width to adjust power.
Why designed this way?
PWM was designed to control power efficiently without wasting energy as heat. Before PWM, variable resistors or changing voltage were used, which wasted power and caused heat. PWM uses digital switching, which is more efficient and easier to control with microcontrollers like Arduino.
Arduino PWM Signal Generation:

┌───────────────┐
│ Arduino Timer │
└──────┬────────┘
       │ Generates fixed frequency pulses
       ▼
┌───────────────┐
│ PWM Output Pin│─────► Motor Driver (Transistor)
└───────────────┘       │
                        ▼
                   Motor Coil & Rotor
                        │
                        ▼
                   Motor Spins Smoothly
Myth Busters - 4 Common Misconceptions
Quick: Does PWM change the voltage level supplied to the motor? Commit to yes or no.
Common Belief:PWM changes the voltage supplied to the motor to control speed.
Tap to reveal reality
Reality:PWM switches the full voltage on and off rapidly; it does not change the voltage level itself.
Why it matters:Believing PWM changes voltage can lead to wrong circuit designs and confusion about how speed control works.
Quick: Can you connect a motor directly to an Arduino PWM pin safely? Commit to yes or no.
Common Belief:You can connect a motor directly to Arduino PWM pins for speed control.
Tap to reveal reality
Reality:Arduino pins cannot supply enough current and can be damaged; a transistor or motor driver is needed.
Why it matters:Connecting directly risks burning the Arduino pin and failing the project.
Quick: Does increasing PWM frequency always make the motor run better? Commit to yes or no.
Common Belief:Higher PWM frequency always improves motor performance and reduces noise.
Tap to reveal reality
Reality:Too high frequency can cause driver heating and inefficiency; there is an optimal frequency range.
Why it matters:Ignoring frequency effects can cause hardware damage or poor motor behavior.
Quick: Does PWM alone guarantee constant motor speed under varying loads? Commit to yes or no.
Common Belief:PWM control alone keeps motor speed constant regardless of load changes.
Tap to reveal reality
Reality:PWM sets power level but does not adjust for load changes; feedback control is needed for constant speed.
Why it matters:Assuming PWM alone is enough can cause unstable or incorrect motor speeds in real applications.
Expert Zone
1
PWM frequency choice depends on motor type, driver hardware, and noise sensitivity; experts tune frequency for each application.
2
Some motor drivers use complementary PWM signals and dead-time insertion to prevent short circuits, a detail often overlooked.
3
Advanced control uses PWM combined with current sensing to protect motors from overload and improve efficiency.
When NOT to use
PWM is not suitable for motors requiring very precise torque control or very low noise, where specialized drivers or analog control may be better. For very high power motors, industrial variable frequency drives (VFDs) are preferred.
Production Patterns
In real systems, PWM is combined with sensors and microcontrollers running PID control loops. Motor drivers with built-in PWM and protection features are used. Software often includes ramp-up and ramp-down to avoid mechanical stress.
Connections
Digital Signal Processing
PWM is a form of digital modulation used to represent analog levels with digital signals.
Understanding PWM as digital modulation links electronics control to communication systems and signal processing.
Human Pulse and Heartbeat
Both PWM and heartbeats use pulse timing to control flow—PWM controls power flow, heartbeat controls blood flow.
Recognizing pulse timing as a universal control method helps appreciate PWM beyond electronics.
Traffic Light Timing
PWM duty cycle is like the green light duration controlling traffic flow rate.
Seeing PWM as controlling flow by timing helps understand its effect on motor speed intuitively.
Common Pitfalls
#1Connecting motor directly to Arduino PWM pin without a driver.
Wrong approach:analogWrite(9, 128); // Motor connected directly to pin 9 and ground
Correct approach:// Use transistor driver circuit pinMode(9, OUTPUT); analogWrite(9, 128); // Motor connected through transistor controlled by pin 9
Root cause:Misunderstanding Arduino pin current limits and motor power needs.
#2Using analogWrite with values outside 0-255 range.
Wrong approach:analogWrite(9, 300); // Invalid value
Correct approach:analogWrite(9, 255); // Max valid value
Root cause:Not knowing PWM duty cycle range and Arduino function limits.
#3Ignoring PWM frequency effects causing noisy motor operation.
Wrong approach:// Using default PWM frequency without consideration analogWrite(9, 128);
Correct approach:// Adjust PWM frequency using timers for quieter motor // (Advanced, hardware-specific code)
Root cause:Lack of awareness about PWM frequency impact on motor noise and efficiency.
Key Takeaways
PWM controls motor speed by rapidly switching power on and off, adjusting the time power is on to change speed.
Arduino's analogWrite function generates PWM signals on specific pins with values from 0 to 255 representing duty cycle.
A transistor or motor driver is necessary to safely power motors from Arduino PWM pins.
PWM frequency affects motor noise and efficiency; choosing the right frequency is important for smooth operation.
For constant motor speed under varying loads, PWM must be combined with feedback control using sensors.